setsumei 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,12 +6,12 @@ module Setsumei
6
6
  options = options.dup
7
7
  new.tap do |attribute|
8
8
  attribute.name = name
9
- options.delete(:as_a)
10
- attribute.options = options
9
+ attribute.lookup_key = options.delete(:from_within)
10
+ attribute.options = options.tap { |o| o.delete :as_a }
11
11
  end
12
12
  end
13
13
 
14
- attr_accessor :name, :options
14
+ attr_accessor :name, :options, :lookup_key
15
15
 
16
16
  def is_an_attribute_of_type?(type)
17
17
  type == :boolean || type == self.class
@@ -30,7 +30,10 @@ module Setsumei
30
30
  :"#{name}="
31
31
  end
32
32
  def value_from_hash(hash)
33
- value_for hash[ Build::Key.for name, given: hash.keys ]
33
+ value_for hash[ key_for(hash)]
34
+ end
35
+ def key_for(hash)
36
+ lookup_key || Build::Key.for(name, given: hash.keys)
34
37
  end
35
38
  end
36
39
  end
@@ -3,13 +3,15 @@ module Setsumei
3
3
  class FloatAttribute
4
4
 
5
5
  def FloatAttribute.named(name, options = {})
6
+ options = options.dup
6
7
  new.tap do |attribute|
7
8
  attribute.name = name
8
- attribute.options = options.dup.tap { |o| o.delete :as_a }
9
+ attribute.lookup_key = options.delete(:from_within)
10
+ attribute.options = options.tap { |o| o.delete :as_a }
9
11
  end
10
12
  end
11
13
 
12
- attr_accessor :name, :options
14
+ attr_accessor :name, :options, :lookup_key
13
15
 
14
16
  def is_an_attribute_of_type?(type)
15
17
  type == :float || type == self.class
@@ -28,7 +30,10 @@ module Setsumei
28
30
  :"#{name}="
29
31
  end
30
32
  def value_from_hash(hash)
31
- value_for hash[ Build::Key.for name, given: hash.keys ]
33
+ value_for hash[ key_for(hash)]
34
+ end
35
+ def key_for(hash)
36
+ lookup_key || Build::Key.for(name, given: hash.keys)
32
37
  end
33
38
  end
34
39
  end
@@ -5,12 +5,12 @@ module Setsumei
5
5
  options = options.dup
6
6
  new.tap do |attribute|
7
7
  attribute.name = name
8
- options.delete(:as_a)
9
- attribute.options = options
8
+ attribute.lookup_key = options.delete(:from_within)
9
+ attribute.options = options.tap { |o| o.delete :as_a }
10
10
  end
11
11
  end
12
12
 
13
- attr_accessor :name, :options
13
+ attr_accessor :name, :options, :lookup_key
14
14
 
15
15
  def is_an_attribute_of_type?(type)
16
16
  type == :int || type == self.class
@@ -29,7 +29,10 @@ module Setsumei
29
29
  :"#{name}="
30
30
  end
31
31
  def value_from_hash(hash)
32
- value_for hash[ Build::Key.for name, given: hash.keys ]
32
+ value_for hash[ key_for(hash)]
33
+ end
34
+ def key_for(hash)
35
+ lookup_key || Build::Key.for(name, given: hash.keys)
33
36
  end
34
37
  end
35
38
  end
@@ -8,11 +8,12 @@ module Setsumei
8
8
  new.tap do |attribute|
9
9
  attribute.name = name
10
10
  attribute.klass = options.delete(:as_a)
11
+ attribute.lookup_key = options.delete(:from_within)
11
12
  attribute.options = options
12
13
  end
13
14
  end
14
15
 
15
- attr_accessor :name, :klass, :options
16
+ attr_accessor :name, :klass, :options, :lookup_key
16
17
 
17
18
  def initialize
18
19
  self.klass = Object
@@ -41,7 +42,10 @@ module Setsumei
41
42
  :"#{name}="
42
43
  end
43
44
  def value_from_hash(hash)
44
- value_for hash[ Build::Key.for name, given: hash.keys ]
45
+ value_for hash[ key_for(hash)]
46
+ end
47
+ def key_for(hash)
48
+ lookup_key || Build::Key.for(name, given: hash.keys)
45
49
  end
46
50
  end
47
51
  end
@@ -2,13 +2,15 @@ module Setsumei
2
2
  module Describable
3
3
  class StringAttribute
4
4
  def StringAttribute.named(name,options = {})
5
+ options = options.dup
5
6
  new.tap do |attribute|
6
7
  attribute.name = name
7
- attribute.options = options.dup.tap { |o| o.delete :as_a }
8
+ attribute.lookup_key = options.delete(:from_within)
9
+ attribute.options = options.tap { |o| o.delete :as_a }
8
10
  end
9
11
  end
10
12
 
11
- attr_accessor :name, :options
13
+ attr_accessor :name, :options, :lookup_key
12
14
 
13
15
  def is_an_attribute_of_type?(type)
14
16
  type == :string || type == self.class
@@ -27,7 +29,10 @@ module Setsumei
27
29
  :"#{name}="
28
30
  end
29
31
  def value_from_hash(hash)
30
- value_for hash[ Build::Key.for name, given: hash.keys ]
32
+ value_for hash[ key_for(hash)]
33
+ end
34
+ def key_for(hash)
35
+ lookup_key || Build::Key.for(name, given: hash.keys)
31
36
  end
32
37
  end
33
38
  end
@@ -1,3 +1,3 @@
1
1
  module Setsumei
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -84,6 +84,7 @@ module Setsumei
84
84
  object.should_receive(:my_boolean_attribute=).with(converted_value)
85
85
  subject
86
86
  end
87
+ it_should_behave_like "it handles lookup keys properly"
87
88
  end
88
89
  end
89
90
  end
@@ -82,6 +82,7 @@ module Setsumei
82
82
  object.should_receive(:my_float_attribute=).with(converted_value)
83
83
  subject
84
84
  end
85
+ it_should_behave_like "it handles lookup keys properly"
85
86
  end
86
87
  end
87
88
  end
@@ -83,6 +83,7 @@ module Setsumei
83
83
  object.should_receive(:my_int_attribute=).with(converted_value)
84
84
  subject
85
85
  end
86
+ it_should_behave_like "it handles lookup keys properly"
86
87
  end
87
88
  end
88
89
  end
@@ -117,6 +117,7 @@ module Setsumei
117
117
  object.should_receive(:my_object_attribute=).with(converted_value)
118
118
  subject
119
119
  end
120
+ it_should_behave_like "it handles lookup keys properly"
120
121
  end
121
122
  end
122
123
  end
@@ -88,6 +88,7 @@ module Setsumei
88
88
  object.should_receive(:my_string_attribute=).with(converted_value)
89
89
  subject
90
90
  end
91
+ it_should_behave_like "it handles lookup keys properly"
91
92
  end
92
93
  end
93
94
  end
@@ -0,0 +1,15 @@
1
+ shared_examples_for "it handles lookup keys properly" do
2
+ let(:attribute) { send described_class.to_s.slice(/::([^:]*)$/,1).gsub(/[a-z][A-Z]/) { |s| s[0]+'_'+s[1] }.downcase }
3
+ context "where a specific key has been specified" do
4
+ before do
5
+ hash["MySpecialKey"] = hash.delete key
6
+ attribute.lookup_key = "MySpecialKey"
7
+ end
8
+
9
+ it "should use this key for the hash lookup instead" do
10
+ Setsumei::Build::Key.should_not_receive(:for)
11
+ attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
12
+ subject
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,24 @@
1
1
  shared_examples_for "it handles options properly" do
2
+ its(:lookup_key) { should be_nil }
2
3
  its(:options) { should == {} }
4
+
3
5
  context "when options are specified" do
4
6
  subject { described_class.named name, as_a: described_class.to_s.downcase.gsub(/(.*::|attribute)/,'').to_sym, with_additional: :options }
5
7
  its(:options) { should == { with_additional: :options } }
6
8
  end
9
+
10
+ context "when from_within: is specified" do
11
+ let(:key) { "specialKey" }
12
+
13
+ subject { described_class.named name, as_a: described_class.to_s.downcase.gsub(/(.*::|attribute)/,'').to_sym, from_within: key }
14
+
15
+ its(:lookup_key) { should == key }
16
+
17
+ context "with addition options" do
18
+ subject { described_class.named name, as_a: described_class.to_s.downcase.gsub(/(.*::|attribute)/,'').to_sym, from_within: key, with_additional: :options }
19
+
20
+ its(:lookup_key) { should == key }
21
+ its(:options) { should == { with_additional: :options } }
22
+ end
23
+ end
7
24
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: setsumei
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jon Rowe
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-24 00:00:00 +01:00
13
+ date: 2011-05-27 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -95,6 +95,7 @@ files:
95
95
  - spec/setsumei/describable_spec.rb
96
96
  - spec/spec_helper.rb
97
97
  - spec/support/be_an_attribute_of_type_matcher.rb
98
+ - spec/support/shared_examples/attribute_lookup_key_support.rb
98
99
  - spec/support/shared_examples/attribute_options.rb
99
100
  - spec/support/shared_examples/attribute_type_creation.rb
100
101
  has_rdoc: true
@@ -137,5 +138,6 @@ test_files:
137
138
  - spec/setsumei/describable_spec.rb
138
139
  - spec/spec_helper.rb
139
140
  - spec/support/be_an_attribute_of_type_matcher.rb
141
+ - spec/support/shared_examples/attribute_lookup_key_support.rb
140
142
  - spec/support/shared_examples/attribute_options.rb
141
143
  - spec/support/shared_examples/attribute_type_creation.rb