Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/structural/model.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Structural
module Model
attr_reader :data

def initialize(data = {})
@data = Hashifier.hashify(data)
end
Expand All @@ -23,6 +21,10 @@ def hash
data.hash
end

def data
@data.with_indifferent_access
end

private

def self.included(base)
Expand Down
2 changes: 1 addition & 1 deletion lib/structural/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Structural
VERSION = '0.1.0'
VERSION = '0.1.1'
end
44 changes: 29 additions & 15 deletions spec/lib/structural/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ class ExtraNestedModel
end

describe Structural::Model do
let(:model) do
TestModel.new(
:foo => 3,
:baz => 6,
:quxx => 8,
:nested_hash => {1 => :one, 2 => :two},
:test_model => {},
:date_of_birth => '06-06-1983',
:aliased_model => {'yak' => 11},
:nested_models => [{'yak' => 11}, {:yak => 14}],
:extra_nested_model => { :cats => "MIAOW" },
:time_stamp => '2015-01-27T13:13:13+00:00'
)
end

let(:model) { TestModel.new(data) }
let(:data) {
{
'foo' => 3,
'baz' => 6,
'quxx' => 8,
'nested_hash' => {1 => :one, 2 => :two},
'test_model' => {},
'date_of_birth' => '06-06-1983',
'aliased_model' => {'yak' => 11},
'nested_models' => [{'yak' => 11}, {'yak' => 14}],
'extra_nested_model' => { 'cats' => "MIAOW" },
'time_stamp' => '2015-01-27T13:13:13+00:00'
}
}

describe ".new" do
it 'converts any passed Structural models to their hash representations' do
Expand Down Expand Up @@ -167,6 +167,20 @@ class ExtraNestedModel
end
end

describe "#data" do
it 'returns the model as a hash' do
model.data.should eq data
end

it 'allows access to values via string keys' do
model.data['foo'].should eq 3
end

it 'allows access to values via symbol keys' do
model.data[:foo].should eq 3
end
end

describe "#to_proc" do
it 'eta expands the model class into its constructor' do
[{},{}].map(&TestModel).all? { |m| m.is_a? TestModel }.should eql(true)
Expand Down