id 0.0.12 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +0 -3
  3. data/Gemfile.lock +25 -10
  4. data/LICENSE.md +1 -1
  5. data/README.md +173 -35
  6. data/id.gemspec +8 -3
  7. data/lib/id.rb +21 -15
  8. data/lib/id/active_model.rb +30 -0
  9. data/lib/id/association.rb +26 -0
  10. data/lib/id/boolean.rb +8 -0
  11. data/lib/id/coercion.rb +38 -0
  12. data/lib/id/eta_expansion.rb +5 -0
  13. data/lib/id/field.rb +46 -0
  14. data/lib/id/field/definition.rb +44 -0
  15. data/lib/id/field/summary.rb +35 -0
  16. data/lib/id/form.rb +41 -13
  17. data/lib/id/form_backwards_compatibility.rb +6 -0
  18. data/lib/id/hashifier.rb +13 -24
  19. data/lib/id/model.rb +29 -25
  20. data/lib/id/timestamps.rb +15 -15
  21. data/lib/id/validations.rb +8 -0
  22. data/spec/examples/cat_spec.rb +37 -0
  23. data/spec/lib/id/active_model_spec.rb +40 -0
  24. data/spec/lib/id/association_spec.rb +73 -0
  25. data/spec/lib/id/boolean_spec.rb +38 -0
  26. data/spec/lib/id/coercion_spec.rb +53 -0
  27. data/spec/lib/id/eta_expansion_spec.rb +13 -0
  28. data/spec/lib/id/field/definition_spec.rb +37 -0
  29. data/spec/lib/id/field/summary_spec.rb +26 -0
  30. data/spec/lib/id/field_spec.rb +62 -0
  31. data/spec/lib/id/form_spec.rb +84 -0
  32. data/spec/lib/id/hashifier_spec.rb +19 -0
  33. data/spec/lib/id/model_spec.rb +30 -180
  34. data/spec/lib/id/timestamps_spec.rb +22 -20
  35. data/spec/lib/id/validations_spec.rb +18 -0
  36. data/spec/spec_helper.rb +11 -5
  37. data/spec/support/dummy_rails_form_builder.rb +9 -0
  38. metadata +84 -26
  39. data/lib/id/form/active_model_form.rb +0 -41
  40. data/lib/id/form/descriptor.rb +0 -27
  41. data/lib/id/form/field_form.rb +0 -14
  42. data/lib/id/form/field_with_form_support.rb +0 -12
  43. data/lib/id/missing_attribute_error.rb +0 -4
  44. data/lib/id/model/association.rb +0 -49
  45. data/lib/id/model/definer.rb +0 -23
  46. data/lib/id/model/descriptor.rb +0 -23
  47. data/lib/id/model/field.rb +0 -61
  48. data/lib/id/model/has_many.rb +0 -11
  49. data/lib/id/model/has_one.rb +0 -17
  50. data/lib/id/model/type_casts.rb +0 -96
  51. data/spec/lib/id/model/association_spec.rb +0 -27
  52. data/spec/lib/id/model/field_spec.rb +0 -0
  53. data/spec/lib/id/model/form_spec.rb +0 -56
  54. data/spec/lib/id/model/type_casts_spec.rb +0 -44
  55. data/spec/lib/mike_spec.rb +0 -20
@@ -1,196 +1,46 @@
1
1
  require 'spec_helper'
2
- require 'date'
3
-
4
- class OptionalModel
5
- include Id::Model
6
- field :buzz
7
- end
8
-
9
- class NestedModel
10
- include Id::Model
11
- field :yak
12
- end
13
-
14
- class TestModel
15
- include Id::Model
16
-
17
- field :foo
18
- field :bar, :key => 'baz'
19
- field :baz
20
- field :qux, :optional => true
21
- field :quux, :default => false
22
- field :date_of_birth, :optional => true, :type => Date
23
- field :empty_date, :optional => true, :type => Date
24
- field :christmas, :default => Date.new(2014,12,25), :type => Date
25
- field :quxx, :optional => true
26
-
27
- has_one :aliased_model, :type => NestedModel
28
- has_one :nested_model, :key => 'aliased_model'
29
- has_one :extra_nested_model
30
- has_one :test_model
31
- has_one :optional_model, :optional => true
32
- has_many :nested_models
33
-
34
- class ExtraNestedModel
35
- include Id::Model
36
- field :cats
37
- end
38
- end
39
2
 
40
3
  describe Id::Model do
41
- let (:model) { TestModel.new(:foo => 3,
42
- :baz => 6,
43
- :quxx => 8,
44
- :test_model => {},
45
- :date_of_birth => '06-06-1983',
46
- :optional_model => { 'yak' => 11},
47
- :aliased_model => { 'yak' => 11},
48
- :nested_models => [{ 'yak' => 11}, { :yak => 14 }],
49
- :extra_nested_model => { :cats => "MIAOW" }) }
50
-
51
-
52
- describe ".new" do
53
- it 'converts any passed id models to their hash representations' do
54
- new_model = TestModel.new(:test_model => model)
55
- new_model.test_model.data.should eq model.data
56
- end
57
- end
58
-
59
- describe ".field" do
60
- it 'defines an accessor on the model' do
61
- model.foo.should eq 3
62
- end
63
-
64
- it 'allows key aliases' do
65
- model.bar.should eq 6
66
- end
67
-
68
- it 'allows default values' do
69
- model.quux.should be_false
70
- end
71
4
 
72
- describe "optional flag" do
73
- context 'when field is not in hash' do
74
- it 'is None' do
75
- expect(model.qux).to eq None
76
- end
77
-
78
- end
79
- context 'when field is in hash' do
80
- it 'is Some' do
81
- expect(model.quxx).to eq Some[8]
82
- end
83
-
84
- end
85
- end
86
-
87
- describe "typecast option" do
88
- it 'typecasts to the provided type if a cast exists' do
89
- model.date_of_birth.should be_some Date
90
- model.christmas.should be_a Date
91
- end
92
-
93
- it 'should work for empty optional fields' do
94
- model.empty_date.should be_none
95
- end
96
- end
97
-
98
- end
99
-
100
- describe ".has_one" do
101
- it "allows nested models" do
102
- model.aliased_model.should be_a NestedModel
103
- end
104
- it "allows nested models" do
105
- model.nested_model.should be_a NestedModel
106
- model.nested_model.yak.should eq 11
107
- end
108
- it "allows associations to be nested within the class" do
109
- model.extra_nested_model.should be_a TestModel::ExtraNestedModel
110
- model.extra_nested_model.cats.should eq 'MIAOW'
111
- end
112
- it "allows recursively defined models" do
113
- model.test_model.should be_a TestModel
114
- end
115
-
116
- it "allows optional models" do
117
- model.optional_model.should be_some
118
- end
119
-
120
- it "allows optional models with correct values" do
121
- model.optional_model.should eq Some[OptionalModel.new(:yak => 11)]
122
- end
123
- end
124
-
125
- describe ".has_many" do
126
- it 'creates an array of nested models' do
127
- model.nested_models.should be_a Array
128
- model.nested_models.first.should be_a NestedModel
129
- model.nested_models.first.yak.should eq 11
130
- model.nested_models.last.yak.should eq 14
131
- end
132
- end
133
-
134
- describe "#set" do
135
- it "creates a new model with the provided values changed" do
136
- model.set(:foo => 999).should be_a TestModel
137
- model.set(:foo => 999).foo.should eq 999
138
- end
5
+ it 'is used by including the module' do
6
+ c = Class.new { include Id::Model ; field :cats }.new(cats: 3)
7
+ expect(c.cats).to eq 3
139
8
  end
140
9
 
141
- describe "#unset" do
142
- it 'returns a new basket minus the passed key' do
143
- expect { model.set(:foo => 999, :bar => 555).unset(:foo, :bar).foo }.to raise_error Id::MissingAttributeError, "foo"
144
- end
145
-
146
- it 'does not error if the key to be removed does not exist' do
147
- expect { model.unset(:not_in_hash) }.to_not raise_error
148
- end
10
+ it 'can have fields set on it after creation - creating a new instance' do
11
+ c = Class.new { include Id::Model ; field :cats }.new(cats: 3)
12
+ expect(c.cats).to eq 3
13
+ d = c.set(cats: 4)
14
+ expect(c.cats).to eq 3
15
+ expect(d.cats).to eq 4
149
16
  end
150
17
 
151
- describe "#fields are present methods" do
152
- it 'allows you to check if fields are present' do
153
- model = TestModel.new(:foo => 1, :baz => false)
154
- model.foo?.should be_true
155
- model.bar?.should be_false
156
- model.baz?.should be_false
157
- end
18
+ it 'can have fields removed from it after creation - creating a new instance' do
19
+ c = Class.new { include Id::Model ; field :cats }.new(cats: 3)
20
+ expect(c.cats).to eq 3
21
+ d = c.unset(:cats)
22
+ expect(c.cats).to eq 3
23
+ expect { d.cats }.to raise_error Id::MissingAttributeError
158
24
  end
159
25
 
160
- describe "#==" do
161
- it 'is equal to another id model with the same data' do
162
- one = TestModel.new(:foo => 1)
163
- two = TestModel.new(:foo => 1)
164
- one.should eq two
165
- end
166
-
167
- it 'is not equal to two models with different data' do
168
- one = TestModel.new(:foo => 1)
169
- two = TestModel.new(:foo => 2)
170
- one.should_not eq two
171
- end
26
+ it 'is equal to models with the same data' do
27
+ c = Class.new { include Id::Model ; field :cats }.new(cats: 3)
28
+ d = Class.new { include Id::Model ; field :cats }.new(cats: 3)
29
+ expect(c).to eq d
172
30
  end
173
31
 
174
- describe "#hash" do
175
- it 'allows id models to be used as hash keys' do
176
- one = TestModel.new(:foo => 1)
177
- two = TestModel.new(:foo => 1)
178
- hash = { one => :found }
179
- hash[two].should eq :found
180
- end
181
- it 'they are different keys if the data is different' do
182
- one = TestModel.new(:foo => 1)
183
- two = TestModel.new(:foo => 2)
184
- hash = { one => :found }
185
- hash[two].should be_nil
186
- hash[one].should eq :found
187
- end
32
+ it 'is not equal to models with different data' do
33
+ c = Class.new { include Id::Model ; field :cats }.new(cats: 3)
34
+ d = Class.new { include Id::Model ; field :cats }.new(cats: 4)
35
+ expect(c).not_to eq d
188
36
  end
189
37
 
190
- describe "#to_proc" do
191
- it 'eta expands the model class into its constructor' do
192
- [{},{}].map(&TestModel).all? { |m| m.is_a? TestModel }.should be_true
193
- end
38
+ it 'can be used as keys in a hash' do
39
+ c = Class.new { include Id::Model ; field :cats }.new(cats: 3)
40
+ d = Class.new { include Id::Model ; field :cats }.new(cats: 3)
41
+ e = Class.new { include Id::Model ; field :cats }.new(cats: 4)
42
+ hash = { c => 10, e => 12 }
43
+ expect(hash[d]).to eq 10
44
+ expect(hash[e]).to eq 12
194
45
  end
195
-
196
46
  end
@@ -1,31 +1,33 @@
1
1
  require 'spec_helper'
2
2
 
3
- class TimeStampedModel
4
- include Id::Model
5
- include Id::Timestamps
3
+ describe Id::Timestamps do
4
+ let (:c) { Class.new { include Id::Model; include Id::Timestamps; field :foo } }
6
5
 
7
- field :foo
8
- field :bar
9
- end
6
+ let (:created) { Time.parse('2013-10-18 09:52:05 +0100') }
7
+ let (:updated) { Time.parse('2013-10-18 09:57:35 +0100') }
10
8
 
11
- module Id
12
- describe Timestamps do
9
+ before do
10
+ Time.stubs(:now).returns(created, updated)
11
+ end
13
12
 
14
- let(:model) { TimeStampedModel.new(:foo => 999, :bar => 666) }
13
+ it 'sets the created_at at creation' do
14
+ expect(c.new.created_at).to be created
15
+ end
15
16
 
16
- it 'should have a created_at date' do
17
- model.created_at.should be_a Time
18
- end
17
+ it 'sets the updated_at at creation' do
18
+ expect(c.new.updated_at).to be created
19
+ end
19
20
 
20
- it 'should update the updated at when set is called' do
21
- updated = model.set(:foo => 123)
22
- expect(updated.created_at).to be < updated.updated_at
23
- end
21
+ it 'sets the updated_at after set is called' do
22
+ expect(c.new.set(foo: 3).updated_at).to be updated
23
+ end
24
24
 
25
- it 'should update the updated at when unset is called' do
26
- updated = model.unset(:foo)
27
- expect(updated.created_at).to be < updated.updated_at
28
- end
25
+ it 'sets the updated_at after unset is called' do
26
+ expect(c.new(foo: 3).unset(:foo).updated_at).to be updated
27
+ end
29
28
 
29
+ it 'does not re-set the created_at when the model is updated' do
30
+ expect(c.new.set(foo: 3).created_at).to be created
30
31
  end
32
+
31
33
  end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe Id::Validations do
4
+
5
+ class ValidationModel
6
+ include Id::Model
7
+ include Id::Form
8
+
9
+ field :cats
10
+ validates_presence_of :cats
11
+ end
12
+
13
+ it 'delegates validations to the active model class' do
14
+ model = ValidationModel.new
15
+ expect(model).not_to be_valid
16
+ expect(model.errors.messages).to eq ({ cats: ["can't be blank"] })
17
+ end
18
+ end
@@ -1,9 +1,15 @@
1
1
  require 'rspec'
2
- require 'lib/id'
3
- require 'support/active_model_lint'
2
+ require 'simplecov'
3
+ require 'coveralls'
4
4
 
5
+ SimpleCov.start do
6
+ add_filter 'spec'
7
+ end
8
+
9
+ require 'spec/support/dummy_rails_form_builder'
10
+ require 'id'
5
11
 
6
- RSpec.configure do |config|
7
- config.order = :rand
8
- config.color_enabled = true
12
+ RSpec.configure do |c|
13
+ c.order = :rand
14
+ c.mock_with :mocha
9
15
  end
@@ -0,0 +1,9 @@
1
+ module ActionView
2
+ module Helpers
3
+ class FormBuilder
4
+ def initialize(object_name, object, template, options)
5
+ :initialized
6
+ end
7
+ end
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: id
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: '0.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russell Dunphy
@@ -81,6 +81,20 @@ dependencies:
81
81
  - - '>='
82
82
  - !ruby/object:Gem::Version
83
83
  version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: mocha
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
84
98
  - !ruby/object:Gem::Dependency
85
99
  name: simplecov
86
100
  requirement: !ruby/object:Gem::Requirement
@@ -95,7 +109,35 @@ dependencies:
95
109
  - - '>='
96
110
  - !ruby/object:Gem::Version
97
111
  version: '0'
98
- description: Developed at On The Beach Ltd. Contact russell.dunphy@onthebeach.co.uk
112
+ - !ruby/object:Gem::Dependency
113
+ name: coveralls
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: test-unit
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - '>='
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ description: Easily convert hashes to Ruby objects. Originally developed at www.onthebeach.co.uk.
99
141
  email:
100
142
  - rssll@rsslldnphy.com
101
143
  - radek.molenda@gmail.com
@@ -112,33 +154,40 @@ files:
112
154
  - README.md
113
155
  - id.gemspec
114
156
  - lib/id.rb
157
+ - lib/id/active_model.rb
158
+ - lib/id/association.rb
159
+ - lib/id/boolean.rb
160
+ - lib/id/coercion.rb
161
+ - lib/id/eta_expansion.rb
162
+ - lib/id/field.rb
163
+ - lib/id/field/definition.rb
164
+ - lib/id/field/summary.rb
115
165
  - lib/id/form.rb
116
- - lib/id/form/active_model_form.rb
117
- - lib/id/form/descriptor.rb
118
- - lib/id/form/field_form.rb
119
- - lib/id/form/field_with_form_support.rb
166
+ - lib/id/form_backwards_compatibility.rb
120
167
  - lib/id/hashifier.rb
121
- - lib/id/missing_attribute_error.rb
122
168
  - lib/id/model.rb
123
- - lib/id/model/association.rb
124
- - lib/id/model/definer.rb
125
- - lib/id/model/descriptor.rb
126
- - lib/id/model/field.rb
127
- - lib/id/model/has_many.rb
128
- - lib/id/model/has_one.rb
129
- - lib/id/model/type_casts.rb
130
169
  - lib/id/timestamps.rb
131
- - spec/lib/id/model/association_spec.rb
132
- - spec/lib/id/model/field_spec.rb
133
- - spec/lib/id/model/form_spec.rb
134
- - spec/lib/id/model/type_casts_spec.rb
170
+ - lib/id/validations.rb
171
+ - spec/examples/cat_spec.rb
172
+ - spec/lib/id/active_model_spec.rb
173
+ - spec/lib/id/association_spec.rb
174
+ - spec/lib/id/boolean_spec.rb
175
+ - spec/lib/id/coercion_spec.rb
176
+ - spec/lib/id/eta_expansion_spec.rb
177
+ - spec/lib/id/field/definition_spec.rb
178
+ - spec/lib/id/field/summary_spec.rb
179
+ - spec/lib/id/field_spec.rb
180
+ - spec/lib/id/form_spec.rb
181
+ - spec/lib/id/hashifier_spec.rb
135
182
  - spec/lib/id/model_spec.rb
136
183
  - spec/lib/id/timestamps_spec.rb
137
- - spec/lib/mike_spec.rb
184
+ - spec/lib/id/validations_spec.rb
138
185
  - spec/spec_helper.rb
139
186
  - spec/support/active_model_lint.rb
140
- homepage: http://github.com/onthebeach/id
141
- licenses: []
187
+ - spec/support/dummy_rails_form_builder.rb
188
+ homepage: http://github.com/rsslldnphy/id
189
+ licenses:
190
+ - MIT
142
191
  metadata: {}
143
192
  post_install_message:
144
193
  rdoc_options: []
@@ -161,12 +210,21 @@ signing_key:
161
210
  specification_version: 4
162
211
  summary: Simple models based on hashes
163
212
  test_files:
164
- - spec/lib/id/model/association_spec.rb
165
- - spec/lib/id/model/field_spec.rb
166
- - spec/lib/id/model/form_spec.rb
167
- - spec/lib/id/model/type_casts_spec.rb
213
+ - spec/examples/cat_spec.rb
214
+ - spec/lib/id/active_model_spec.rb
215
+ - spec/lib/id/association_spec.rb
216
+ - spec/lib/id/boolean_spec.rb
217
+ - spec/lib/id/coercion_spec.rb
218
+ - spec/lib/id/eta_expansion_spec.rb
219
+ - spec/lib/id/field/definition_spec.rb
220
+ - spec/lib/id/field/summary_spec.rb
221
+ - spec/lib/id/field_spec.rb
222
+ - spec/lib/id/form_spec.rb
223
+ - spec/lib/id/hashifier_spec.rb
168
224
  - spec/lib/id/model_spec.rb
169
225
  - spec/lib/id/timestamps_spec.rb
170
- - spec/lib/mike_spec.rb
226
+ - spec/lib/id/validations_spec.rb
171
227
  - spec/spec_helper.rb
172
228
  - spec/support/active_model_lint.rb
229
+ - spec/support/dummy_rails_form_builder.rb
230
+ has_rdoc: