setsumei 0.0.10 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +1 -0
  4. data/.travis.yml +7 -0
  5. data/README.md +1 -0
  6. data/Rakefile +7 -0
  7. data/lib/setsumei/describable.rb +17 -18
  8. data/lib/setsumei/describable/attribute.rb +43 -0
  9. data/lib/setsumei/describable/boolean_attribute.rb +4 -29
  10. data/lib/setsumei/describable/date_time_attribute.rb +21 -0
  11. data/lib/setsumei/describable/float_attribute.rb +4 -29
  12. data/lib/setsumei/describable/int_attribute.rb +4 -28
  13. data/lib/setsumei/describable/object_attribute.rb +9 -35
  14. data/lib/setsumei/describable/string_attribute.rb +4 -28
  15. data/lib/setsumei/version.rb +1 -1
  16. data/setsumei.gemspec +3 -2
  17. data/spec/setsumei/describable/attribute_spec.rb +94 -0
  18. data/spec/setsumei/describable/boolean_attribute_spec.rb +13 -71
  19. data/spec/setsumei/describable/date_attribute_spec.rb +18 -79
  20. data/spec/setsumei/describable/float_attribute_spec.rb +8 -69
  21. data/spec/setsumei/describable/int_attribute_spec.rb +10 -69
  22. data/spec/setsumei/describable/object_attribute_spec.rb +20 -75
  23. data/spec/setsumei/describable/string_attribute_spec.rb +7 -66
  24. data/spec/setsumei/describable/time_attribute_spec.rb +20 -78
  25. data/spec/setsumei/describable_spec.rb +2 -2
  26. data/spec/spec_helper.rb +0 -1
  27. data/spec/support/shared_examples/attribute_options.rb +4 -4
  28. data/spec/support/shared_examples/attribute_type_creation.rb +1 -1
  29. metadata +65 -35
  30. metadata.gz.sig +3 -0
  31. data/lib/setsumei/describable/date_attribute.rb +0 -40
  32. data/lib/setsumei/describable/time_attribute.rb +0 -40
  33. data/spec/support/shared_examples/attribute_lookup_key_support.rb +0 -15
@@ -3,89 +3,31 @@ require 'spec_helper'
3
3
  module Setsumei
4
4
  module Describable
5
5
  describe BooleanAttribute do
6
- its(:name) { should == nil }
7
6
 
8
- describe ".named(name)" do
9
- let(:name) { :my_boolean_field }
7
+ describe "#== other" do
8
+ subject { BooleanAttribute.new }
10
9
 
11
- subject { BooleanAttribute.named(name) }
12
-
13
- it { should be_a BooleanAttribute }
14
- its(:name) { should == name }
15
-
16
- it_should_behave_like "it handles options properly"
10
+ it { should == :boolean }
11
+ it { should == BooleanAttribute }
12
+ it { should_not eq double }
17
13
  end
18
14
 
19
- describe "#value_for(pre_type_cast_value)" do
15
+ describe "#cast value" do
20
16
  let(:boolean_attribute) { BooleanAttribute.new }
21
17
 
22
18
  context "where the value is a boolean" do
23
- specify { boolean_attribute.value_for(true).should === true }
24
- specify { boolean_attribute.value_for(false).should === false }
19
+ specify { boolean_attribute.cast(true).should === true }
20
+ specify { boolean_attribute.cast(false).should === false }
25
21
  end
26
22
  context "where the value isn't a boolean" do
27
- specify { boolean_attribute.value_for("false").should === false }
28
- specify { boolean_attribute.value_for("true").should === true }
29
- specify { boolean_attribute.value_for("True").should === true }
30
- specify { boolean_attribute.value_for("1").should === true }
31
- end
32
- end
33
-
34
- describe "#is_an_attribute_of_type?" do
35
- let(:boolean_attribute) { BooleanAttribute.new }
36
-
37
- subject { boolean_attribute.is_an_attribute_of_type? type }
38
-
39
- context "where type is :boolean" do
40
- let(:type) { :boolean }
41
- it { should be_true }
42
- end
43
-
44
- context "where type is BooleanAttribute" do
45
- let(:type) { BooleanAttribute }
46
- it { should be_true }
47
- end
48
-
49
- context "where type is unknown" do
50
- let(:type) { mock "an unknown type" }
51
- it { should be_false }
23
+ specify { boolean_attribute.cast("false").should === false }
24
+ specify { boolean_attribute.cast("true").should === true }
25
+ specify { boolean_attribute.cast("True").should === true }
26
+ specify { boolean_attribute.cast("1").should === true }
52
27
  end
53
28
  end
54
29
 
55
- describe "set_value_on object, from_value_in: hash" do
56
- let(:hash) { Hash.new }
57
- let(:key) { "key" }
58
- let(:hash_keys) { mock "hash_keys" }
59
- let(:value_in_hash) { mock "value_in_hash" }
60
-
61
- let(:object) { mock "object", :my_boolean_attribute= => nil }
62
-
63
- let(:boolean_attribute) { BooleanAttribute.named :my_boolean_attribute }
64
- let(:converted_value) { mock "converted_value" }
65
-
66
- before do
67
- Build::Key.stub(:for).and_return(key)
68
- hash[key] = value_in_hash
69
- boolean_attribute.stub(:value_for).and_return(converted_value)
70
- end
71
-
72
- subject { boolean_attribute.set_value_on object, from_value_in: hash }
73
-
74
- it "should detect the key it should use to retrieve the value from the hash" do
75
- hash.should_receive(:keys).and_return(hash_keys)
76
- Build::Key.should_receive(:for).with(:my_boolean_attribute, given: hash_keys ).and_return(key)
77
- subject
78
- end
79
- it "should convert the value" do
80
- boolean_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
81
- subject
82
- end
83
- it "should pass object a value to the attribute described by this class" do
84
- object.should_receive(:my_boolean_attribute=).with(converted_value)
85
- subject
86
- end
87
- it_should_behave_like "it handles lookup keys properly"
88
- end
89
30
  end
90
31
  end
91
32
  end
33
+
@@ -2,100 +2,39 @@ require 'spec_helper'
2
2
 
3
3
  module Setsumei
4
4
  module Describable
5
- describe DateAttribute do
6
- its(:name) { should == nil }
5
+ describe DateTimeAttribute do
7
6
 
8
- describe ".named(name)" do
9
- let(:name) { :my_date_field }
10
-
11
- subject { DateAttribute.named(name) }
12
-
13
- it { should be_a DateAttribute }
14
- its(:name) { should == name }
15
- its(:format) { should == '%Y-%m-%d' }
16
-
17
- context 'format specified' do
18
- let(:format) { mock "format" }
19
-
20
- subject { DateAttribute.named name, format: format }
21
-
22
- its(:format) { should == format }
7
+ describe "#initialize(format)" do
8
+ it 'is configurable' do
9
+ format = double
10
+ DateTimeAttribute.new(:date, format, Date).format.should == format
23
11
  end
12
+ end
13
+
14
+ describe "#== other" do
15
+ subject { DateTimeAttribute.new :date, '%Y-%m-%d', Date }
24
16
 
25
- it_should_behave_like "it handles options properly"
17
+ it { should == :date }
18
+ it { should == DateTimeAttribute }
19
+ it { should_not eq double }
26
20
  end
27
21
 
28
- describe "#value_for(pre_type_cast_value)" do
29
- let(:attribute) { DateAttribute.new }
30
22
 
31
- subject { attribute.value_for pre_type_cast_value }
23
+ describe "#cast value" do
24
+ let(:attribute) { DateTimeAttribute.new :date, '%Y-%m-%d', Date }
25
+
26
+ subject { attribute.cast value }
32
27
 
33
28
  context "where pre_type_cast_value is a string" do
34
- let(:pre_type_cast_value) { "time" }
29
+ let(:value) { "time" }
35
30
  let(:formatted_value) { mock "formatted_value" }
36
31
 
37
32
  it "should parse it into a time with format" do
38
- Date.should_receive(:strptime).with(pre_type_cast_value.to_s,attribute.format).and_return(formatted_value)
33
+ Date.should_receive(:strptime).with(value,'%Y-%m-%d').and_return(formatted_value)
39
34
  subject.should == formatted_value
40
35
  end
41
36
  end
42
37
  end
43
-
44
- describe "#is_an_attribute_of_type?" do
45
- let(:attribute) { DateAttribute.new }
46
-
47
- subject { attribute.is_an_attribute_of_type? type }
48
-
49
- context "where type is :date" do
50
- let(:type) { :date }
51
- it { should be_true }
52
- end
53
-
54
- context "where type is DateAttribute" do
55
- let(:type) { DateAttribute }
56
- it { should be_true }
57
- end
58
-
59
- context "where type is unknown" do
60
- let(:type) { mock "an unknown type" }
61
- it { should be_false }
62
- end
63
- end
64
-
65
- describe "set_value_on object, from_value_in: hash" do
66
- let(:hash) { Hash.new }
67
- let(:key) { "key" }
68
- let(:hash_keys) { mock "hash_keys" }
69
- let(:value_in_hash) { mock "value_in_hash" }
70
- let(:converted_value) { mock "converted_value" }
71
-
72
- let(:object) { mock "object", :my_date_attribute= => nil }
73
-
74
- let(:date_attribute) { DateAttribute.named :my_date_attribute }
75
-
76
- before do
77
- Build::Key.stub(:for).and_return(key)
78
- hash[key] = value_in_hash
79
- date_attribute.stub(:value_for).and_return(converted_value)
80
- end
81
-
82
- subject { date_attribute.set_value_on object, from_value_in: hash }
83
-
84
- it "should detect the key it should use to retrieve the value from the hash" do
85
- hash.should_receive(:keys).and_return(hash_keys)
86
- Build::Key.should_receive(:for).with(:my_date_attribute, given: hash_keys ).and_return(key)
87
- subject
88
- end
89
- it "should convert the value" do
90
- date_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
91
- subject
92
- end
93
- it "should pass object a value to the attribute described by this class" do
94
- object.should_receive(:my_date_attribute=).with(converted_value)
95
- subject
96
- end
97
- it_should_behave_like "it handles lookup keys properly"
98
- end
99
38
  end
100
39
  end
101
40
  end
@@ -3,87 +3,26 @@ require 'spec_helper'
3
3
  module Setsumei
4
4
  module Describable
5
5
  describe FloatAttribute do
6
- its(:name) { should == nil }
7
6
 
8
- describe ".named(name)" do
9
- let(:name) { :my_float_field }
7
+ describe "#== other" do
8
+ subject { FloatAttribute.new }
10
9
 
11
- subject { FloatAttribute.named(name) }
12
-
13
- it { should be_a FloatAttribute }
14
- its(:name) { should == name }
15
-
16
- it_should_behave_like "it handles options properly"
10
+ it { should == :float }
11
+ it { should == FloatAttribute }
12
+ it { should_not eq double }
17
13
  end
18
14
 
19
- describe "#value_for(pre_type_cast_value)" do
15
+ describe "#cast value" do
20
16
  let(:float_attribute) { FloatAttribute.new }
21
17
 
22
- subject { float_attribute.value_for pre_type_cast_value }
23
-
24
18
  context "where the value is a float" do
25
- specify { float_attribute.value_for(10.2).should == 10.2 }
19
+ specify { float_attribute.cast(10.2).should == 10.2 }
26
20
  end
27
21
  context "where the value isn't a float" do
28
- specify { float_attribute.value_for("10.2").should == 10.2 }
22
+ specify { float_attribute.cast("10.2").should == 10.2 }
29
23
  end
30
24
  end
31
25
 
32
- describe "#is_an_attribute_of_type?" do
33
- let(:float_attribute) { FloatAttribute.new }
34
-
35
- subject { float_attribute.is_an_attribute_of_type? type }
36
-
37
- context "where type is :float" do
38
- let(:type) { :float }
39
- it { should be_true }
40
- end
41
-
42
- context "where type is FloatAttribute" do
43
- let(:type) { FloatAttribute }
44
- it { should be_true }
45
- end
46
-
47
- context "where type is unknown" do
48
- let(:type) { mock "an unknown type" }
49
- it { should be_false }
50
- end
51
- end
52
-
53
- describe "set_value_on object, from_value_in: hash" do
54
- let(:hash) { Hash.new }
55
- let(:key) { "key" }
56
- let(:hash_keys) { mock "hash_keys" }
57
- let(:value_in_hash) { mock "value_in_hash" }
58
-
59
- let(:object) { mock "object", :my_float_attribute= => nil }
60
-
61
- let(:float_attribute) { FloatAttribute.named :my_float_attribute }
62
- let(:converted_value) { mock "converted_value" }
63
-
64
- before do
65
- Build::Key.stub(:for).and_return(key)
66
- hash[key] = value_in_hash
67
- float_attribute.stub(:value_for).and_return(converted_value)
68
- end
69
-
70
- subject { float_attribute.set_value_on object, from_value_in: hash }
71
-
72
- it "should detect the key it should use to retrieve the value from the hash" do
73
- hash.should_receive(:keys).and_return(hash_keys)
74
- Build::Key.should_receive(:for).with(:my_float_attribute, given: hash_keys ).and_return(key)
75
- subject
76
- end
77
- it "should convert the value" do
78
- float_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
79
- subject
80
- end
81
- it "should pass object a value to the attribute described by this class" do
82
- object.should_receive(:my_float_attribute=).with(converted_value)
83
- subject
84
- end
85
- it_should_behave_like "it handles lookup keys properly"
86
- end
87
26
  end
88
27
  end
89
28
  end
@@ -3,88 +3,29 @@ require 'spec_helper'
3
3
  module Setsumei
4
4
  module Describable
5
5
  describe IntAttribute do
6
- its(:name) { should == nil }
7
6
 
8
- describe ".named(name)" do
9
- let(:name) { :my_int_field }
7
+ describe "#== other" do
8
+ subject { IntAttribute.new }
10
9
 
11
- subject { IntAttribute.named(name) }
12
-
13
- it { should be_a IntAttribute }
14
- its(:name) { should == name }
15
-
16
- it_should_behave_like "it handles options properly"
10
+ it { should == :int }
11
+ it { should == IntAttribute }
12
+ it { should_not eq double }
17
13
  end
18
14
 
19
- describe "#value_for(pre_type_cast_value)" do
15
+ describe "#cast value" do
20
16
  let(:int_attribute) { IntAttribute.new }
21
17
 
22
- subject { int_attribute.value_for pre_type_cast_value }
18
+ subject { int_attribute.cast pre_type_cast_value }
23
19
 
24
20
  context "where the value is a int" do
25
- specify { int_attribute.value_for(42).should == 42 }
21
+ specify { int_attribute.cast(42).should == 42 }
26
22
  end
27
23
  context "where the value isn't a int" do
28
- specify { int_attribute.value_for("10.6").should == 11 }
29
- specify { int_attribute.value_for("10.4").should == 10 }
30
- end
31
- end
32
-
33
- describe "#is_an_attribute_of_type?" do
34
- let(:int_attribute) { IntAttribute.new }
35
-
36
- subject { int_attribute.is_an_attribute_of_type? type }
37
-
38
- context "where type is :int" do
39
- let(:type) { :int }
40
- it { should be_true }
41
- end
42
-
43
- context "where type is IntAttribute" do
44
- let(:type) { IntAttribute }
45
- it { should be_true }
46
- end
47
-
48
- context "where type is unknown" do
49
- let(:type) { mock "an unknown type" }
50
- it { should be_false }
24
+ specify { int_attribute.cast("10.6").should == 11 }
25
+ specify { int_attribute.cast("10.4").should == 10 }
51
26
  end
52
27
  end
53
28
 
54
- describe "set_value_on object, from_value_in: hash" do
55
- let(:hash) { Hash.new }
56
- let(:key) { "key" }
57
- let(:hash_keys) { mock "hash_keys" }
58
- let(:value_in_hash) { mock "value_in_hash" }
59
-
60
- let(:object) { mock "object", :my_int_attribute= => nil }
61
-
62
- let(:int_attribute) { IntAttribute.named :my_int_attribute }
63
- let(:converted_value) { mock "converted_value" }
64
-
65
- before do
66
- Build::Key.stub(:for).and_return(key)
67
- hash[key] = value_in_hash
68
- int_attribute.stub(:value_for).and_return(converted_value)
69
- end
70
-
71
- subject { int_attribute.set_value_on object, from_value_in: hash }
72
-
73
- it "should detect the key it should use to retrieve the value from the hash" do
74
- hash.should_receive(:keys).and_return(hash_keys)
75
- Build::Key.should_receive(:for).with(:my_int_attribute, given: hash_keys ).and_return(key)
76
- subject
77
- end
78
- it "should convert the value" do
79
- int_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
80
- subject
81
- end
82
- it "should pass object a value to the attribute described by this class" do
83
- object.should_receive(:my_int_attribute=).with(converted_value)
84
- subject
85
- end
86
- it_should_behave_like "it handles lookup keys properly"
87
- end
88
29
  end
89
30
  end
90
31
  end
@@ -3,122 +3,67 @@ require 'spec_helper'
3
3
  module Setsumei
4
4
  module Describable
5
5
  describe ObjectAttribute do
6
- its(:name) { should == nil }
7
- its(:klass) { should == Object }
8
6
 
9
- describe ".named(name)" do
10
- let(:name) { :my_object_field }
7
+ describe "#initialize(klass)" do
11
8
  let(:my_klass) { Class.new }
12
9
 
13
- subject { ObjectAttribute.named name, as_a: my_klass }
10
+ subject { ObjectAttribute.new my_klass }
14
11
 
15
- it { should be_a ObjectAttribute }
16
- its(:name) { should == name }
17
- its(:klass) { should == my_klass }
18
-
19
- context "klass is left off" do
20
- subject { ObjectAttribute.named name }
21
- specify { expect { subject }.to raise_error ArgumentError }
22
- end
23
-
24
- it_should_behave_like "it handles options properly"
12
+ specify { subject.klass.should == my_klass }
25
13
  end
26
14
 
27
- describe "#value_for(pre_type_cast_value)" do
15
+ describe "#cast data" do
28
16
  let(:klass) { mock "klass" }
29
- let(:pre_type_cast_value) { { hash: "with_values" } }
30
- let(:object_attribute) { ObjectAttribute.named :name, as_a: klass }
17
+ let(:data) { { hash: "with_values" } }
18
+ let(:object_attribute) { ObjectAttribute.new(klass) }
31
19
 
32
20
  before do
33
21
  klass.stub(:create_from).and_raise(NoMethodError)
34
22
  end
35
23
 
36
- subject { object_attribute.value_for pre_type_cast_value }
24
+ subject { object_attribute.cast data }
37
25
 
38
26
  it "should use the Builder to produce a object with klass" do
39
- Build.should_receive(:a).with(klass, from: pre_type_cast_value)
27
+ Build.should_receive(:a).with(klass, from: data)
40
28
  subject
41
29
  end
42
30
 
43
31
  context "there is no data for this object" do
44
32
  context "empty hash" do
45
- let(:pre_type_cast_value) { {} }
33
+ let(:data) { {} }
46
34
  it { should be_nil }
47
35
  end
48
36
  context "nil" do
49
- let(:pre_type_cast_value) { nil }
37
+ let(:data) { nil }
50
38
  it { should be_nil }
51
39
  end
52
40
  end
41
+
53
42
  context "klass responds to create_from" do
54
- let(:pre_fab_object) { mock "pre_fab_object" }
43
+ let(:pre_fab_object) { double "pre_fab_object" }
55
44
 
56
45
  before do
57
46
  klass.unstub(:create_from)
58
47
  end
59
48
 
60
49
  it "should build the klass itself" do
61
- klass.should_receive(:create_from).with(pre_type_cast_value).and_return(pre_fab_object)
50
+ klass.should_receive(:create_from).with(data).and_return(pre_fab_object)
62
51
  subject.should == pre_fab_object
63
52
  end
64
53
  end
65
54
  end
66
55
 
67
- describe "#is_an_attribute_of_type?" do
68
- let(:object_attribute) { ObjectAttribute.new }
69
-
70
- subject { object_attribute.is_an_attribute_of_type? type }
71
-
72
- context "where type is :object" do
73
- let(:type) { :object }
74
- it { should be_true }
75
- end
56
+ describe "#== other" do
57
+ let(:klass) { double }
76
58
 
77
- context "where type is ObjectAttribute" do
78
- let(:type) { ObjectAttribute }
79
- it { should be_true }
80
- end
59
+ subject { ObjectAttribute.new klass }
81
60
 
82
- context "where type is unknown" do
83
- let(:type) { mock "an unknown type" }
84
- it { should be_false }
85
- end
61
+ it { should eq :object }
62
+ it { should eq klass }
63
+ it { should eq ObjectAttribute }
64
+ it { should_not eq double("an unknown type") }
86
65
  end
87
66
 
88
- describe "set_value_on object, from_value_in: hash" do
89
- let(:hash) { Hash.new }
90
- let(:key) { "key" }
91
- let(:hash_keys) { mock "hash_keys" }
92
- let(:value_in_hash) { mock "value_in_hash" }
93
-
94
- let(:object) { mock "object", :my_object_attribute= => nil }
95
-
96
- let(:object_attribute) { ObjectAttribute.named :my_object_attribute, as_a: mock("object_klass") }
97
- let(:converted_value) { mock "converted_value" }
98
-
99
- before do
100
- Build::Key.stub(:for).and_return(key)
101
- hash[key] = value_in_hash
102
- object_attribute.stub(:value_for).and_return(converted_value)
103
- end
104
-
105
- subject { object_attribute.set_value_on object, from_value_in: hash }
106
-
107
- it "should detect the key it should use to retrieve the value from the hash" do
108
- hash.should_receive(:keys).and_return(hash_keys)
109
- Build::Key.should_receive(:for).with(:my_object_attribute, given: hash_keys ).and_return(key)
110
- subject
111
- end
112
- it "should convert the value" do
113
- object_attribute.should_receive(:value_for).with(value_in_hash).and_return(converted_value)
114
- subject
115
- end
116
- it "should pass object a value to the attribute described by this class" do
117
- object.should_receive(:my_object_attribute=).with(converted_value)
118
- subject
119
- end
120
- it_should_behave_like "it handles lookup keys properly"
121
- end
122
67
  end
123
68
  end
124
69
  end