From 5fe9beb1c68fddc5a58af2ba015bddd8e8e8386a Mon Sep 17 00:00:00 2001 From: Gavin Laking Date: Wed, 14 Dec 2016 13:19:02 +0000 Subject: [PATCH 1/2] Update to allow 'data' to be accessed with either string or symbol keys. --- lib/structural/model.rb | 6 +++-- spec/lib/structural/model_spec.rb | 44 ++++++++++++++++++++----------- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/lib/structural/model.rb b/lib/structural/model.rb index 011b323..e000ab9 100644 --- a/lib/structural/model.rb +++ b/lib/structural/model.rb @@ -1,7 +1,5 @@ module Structural module Model - attr_reader :data - def initialize(data = {}) @data = Hashifier.hashify(data) end @@ -23,6 +21,10 @@ def hash data.hash end + def data + @data.with_indifferent_access + end + private def self.included(base) diff --git a/spec/lib/structural/model_spec.rb b/spec/lib/structural/model_spec.rb index 32476f9..17d2a1e 100644 --- a/spec/lib/structural/model_spec.rb +++ b/spec/lib/structural/model_spec.rb @@ -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 @@ -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) From 238845da1e993704231298b1b6a37ca26da5e92a Mon Sep 17 00:00:00 2001 From: Gavin Laking Date: Wed, 14 Dec 2016 13:19:44 +0000 Subject: [PATCH 2/2] Bump gem version to 0.1.1. --- lib/structural/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/structural/version.rb b/lib/structural/version.rb index 72e1437..f102316 100644 --- a/lib/structural/version.rb +++ b/lib/structural/version.rb @@ -1,3 +1,3 @@ module Structural - VERSION = '0.1.0' + VERSION = '0.1.1' end