structural 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,7 @@ require 'money'
3
3
 
4
4
  require 'structural/version'
5
5
  require 'structural/missing_attribute_error'
6
+ require 'structural/invalid_default_type_error'
6
7
  require 'structural/hashifier'
7
8
  require 'structural/model/definer'
8
9
  require 'structural/model/descriptor'
@@ -0,0 +1,4 @@
1
+ module Structural
2
+ class InvalidDefaultTypeError < StandardError
3
+ end
4
+ end
@@ -2,9 +2,22 @@ module Structural
2
2
  module Model
3
3
  class HasOne < Association
4
4
  def value_of(data)
5
- child = data.fetch(key) { raise MissingAttributeError, key }
5
+ child = data.fetch(key, &default_value)
6
6
  type.new(child) unless child.nil?
7
7
  end
8
+
9
+ def default
10
+ valid_type_check(super)
11
+ end
12
+
13
+ private
14
+
15
+ def valid_type_check(v)
16
+ case v
17
+ when Hash then v
18
+ when Proc then valid_type_check(v.call)
19
+ else raise Structural::InvalidDefaultTypeError end
20
+ end
8
21
  end
9
22
  end
10
23
  end
@@ -1,3 +1,3 @@
1
1
  module Structural
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -3,6 +3,14 @@ require 'spec_helper'
3
3
  class NestedModel
4
4
  include Structural::Model
5
5
  field :yak
6
+ field :name, :default => nil
7
+ field :surname, :default => nil
8
+ end
9
+
10
+ class SeparateClass
11
+ def self.as_hash
12
+ { :name => 'Michael' }
13
+ end
6
14
  end
7
15
 
8
16
  class TestModel
@@ -15,10 +23,16 @@ class TestModel
15
23
  field :date_of_birth, :type => Date
16
24
  field :empty_date, :type => Date
17
25
  field :christmas, :default => Date.new(2014,12,25), :type => Date
26
+ field :time_stamp, :type => Time
18
27
 
19
28
  has_one :aliased_model, :type => NestedModel
20
29
  has_one :nested_model, :key => 'aliased_model'
21
30
  has_one :extra_nested_model
31
+ has_one :nested_with_invalid_default, :default => nil
32
+ has_one :nested_with_hash_default, :default => { name: 'Michael' }, :type => NestedModel
33
+ has_one :nested_with_hash_proc_default, :default => Proc.new { SeparateClass.as_hash }, :type => NestedModel
34
+ has_one :nested_with_invalid_proc_default, :default => Proc.new { SeparateClass.new }, :type => NestedModel
35
+ has_one :missing_nested_without_default
22
36
  has_one :test_model
23
37
  has_many :nested_models
24
38
 
@@ -39,7 +53,8 @@ describe Structural::Model do
39
53
  :date_of_birth => '06-06-1983',
40
54
  :aliased_model => {'yak' => 11},
41
55
  :nested_models => [{'yak' => 11}, {:yak => 14}],
42
- :extra_nested_model => { :cats => "MIAOW" }
56
+ :extra_nested_model => { :cats => "MIAOW" },
57
+ :time_stamp => '2015-01-27T13:13:13+00:00'
43
58
  )
44
59
  end
45
60
 
@@ -73,24 +88,67 @@ describe Structural::Model do
73
88
  model.date_of_birth.should be_a Date
74
89
  model.christmas.should be_a Date
75
90
  end
91
+
92
+ it 'parses the date correctly' do
93
+ model.date_of_birth.should eql Date.new(1983, 6, 6)
94
+ end
95
+
96
+ it 'parses the time correctly' do
97
+ model.time_stamp.should eql Time.new(2015, 1, 27, 13, 13, 13, '+00:00')
98
+ end
76
99
  end
77
100
  end
78
101
 
79
102
  describe ".has_one" do
80
- it "allows nested models" do
103
+ it 'allows nested models' do
81
104
  model.aliased_model.should be_a NestedModel
82
105
  end
83
- it "allows nested models" do
106
+
107
+ it 'allows nested models' do
84
108
  model.nested_model.should be_a NestedModel
85
109
  model.nested_model.yak.should eq 11
86
110
  end
87
- it "allows associations to be nested within the class" do
111
+
112
+ it 'allows associations to be nested within the class' do
88
113
  model.extra_nested_model.should be_a TestModel::ExtraNestedModel
89
114
  model.extra_nested_model.cats.should eq 'MIAOW'
90
115
  end
91
- it "allows recursively defined models" do
116
+
117
+ it 'allows recursively defined models' do
92
118
  model.test_model.should be_a TestModel
93
119
  end
120
+
121
+ it 'allows default values as a hash' do
122
+ model.nested_with_hash_default.name.should eq 'Michael'
123
+ model.nested_with_hash_default.surname.should be_nil
124
+ end
125
+
126
+ context 'when passing a Proc as a default' do
127
+ it 'allows the default' do
128
+ model.nested_with_hash_proc_default.name.should eq 'Michael'
129
+ model.nested_with_hash_default.surname.should be_nil
130
+ end
131
+
132
+ context 'when the Proc does not return a Hash' do
133
+ it 'raises an error' do
134
+ expect {
135
+ model.nested_with_invalid_proc_default
136
+ }.to raise_error(Structural::InvalidDefaultTypeError)
137
+ end
138
+ end
139
+ end
140
+
141
+ it 'fails if passed a non-hash as default' do
142
+ expect {
143
+ model.nested_with_invalid_default
144
+ }.to raise_error(Structural::InvalidDefaultTypeError)
145
+ end
146
+
147
+ it 'fails for missing associations without defaults' do
148
+ expect {
149
+ model.missing_nested_without_default
150
+ }.to raise_error(Structural::MissingAttributeError)
151
+ end
94
152
  end
95
153
 
96
154
  describe ".has_many" do
metadata CHANGED
@@ -1,125 +1,137 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: structural
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
- authors:
12
+ authors:
7
13
  - Russell Dunphy
8
14
  - Radek Molenda
9
15
  - Jon Doveston
10
16
  autorequire:
11
17
  bindir: bin
12
18
  cert_chain: []
13
- date: 2016-11-07 00:00:00.000000000 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
19
+
20
+ date: 2017-02-20 00:00:00 Z
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
16
23
  name: money
17
- requirement: !ruby/object:Gem::Requirement
18
- requirements:
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
24
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- requirements:
25
+ requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
26
28
  - - ">="
27
- - !ruby/object:Gem::Version
28
- version: '0'
29
- - !ruby/object:Gem::Dependency
30
- name: activesupport
31
- requirement: !ruby/object:Gem::Requirement
32
- requirements:
33
- - - ">="
34
- - !ruby/object:Gem::Version
35
- version: '0'
29
+ - !ruby/object:Gem::Version
30
+ hash: 3
31
+ segments:
32
+ - 0
33
+ version: "0"
36
34
  type: :runtime
35
+ version_requirements: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ name: activesupport
37
38
  prerelease: false
38
- version_requirements: !ruby/object:Gem::Requirement
39
- requirements:
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
40
42
  - - ">="
41
- - !ruby/object:Gem::Version
42
- version: '0'
43
- - !ruby/object:Gem::Dependency
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ type: :runtime
49
+ version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
44
51
  name: rspec
45
- requirement: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '3.0'
50
- type: :development
51
52
  prerelease: false
52
- version_requirements: !ruby/object:Gem::Requirement
53
- requirements:
54
- - - "~>"
55
- - !ruby/object:Gem::Version
56
- version: '3.0'
57
- - !ruby/object:Gem::Dependency
58
- name: yard
59
- requirement: !ruby/object:Gem::Requirement
60
- requirements:
61
- - - ">="
62
- - !ruby/object:Gem::Version
63
- version: '0'
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ~>
57
+ - !ruby/object:Gem::Version
58
+ hash: 7
59
+ segments:
60
+ - 3
61
+ - 0
62
+ version: "3.0"
64
63
  type: :development
64
+ version_requirements: *id003
65
+ - !ruby/object:Gem::Dependency
66
+ name: yard
65
67
  prerelease: false
66
- version_requirements: !ruby/object:Gem::Requirement
67
- requirements:
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
68
71
  - - ">="
69
- - !ruby/object:Gem::Version
70
- version: '0'
71
- - !ruby/object:Gem::Dependency
72
- name: redcarpet
73
- requirement: !ruby/object:Gem::Requirement
74
- requirements:
75
- - - ">="
76
- - !ruby/object:Gem::Version
77
- version: '0'
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
78
77
  type: :development
78
+ version_requirements: *id004
79
+ - !ruby/object:Gem::Dependency
80
+ name: redcarpet
79
81
  prerelease: false
80
- version_requirements: !ruby/object:Gem::Requirement
81
- requirements:
82
+ requirement: &id005 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
82
85
  - - ">="
83
- - !ruby/object:Gem::Version
84
- version: '0'
85
- - !ruby/object:Gem::Dependency
86
- name: bundler
87
- requirement: !ruby/object:Gem::Requirement
88
- requirements:
89
- - - "~>"
90
- - !ruby/object:Gem::Version
91
- version: '1.6'
86
+ - !ruby/object:Gem::Version
87
+ hash: 3
88
+ segments:
89
+ - 0
90
+ version: "0"
92
91
  type: :development
92
+ version_requirements: *id005
93
+ - !ruby/object:Gem::Dependency
94
+ name: bundler
93
95
  prerelease: false
94
- version_requirements: !ruby/object:Gem::Requirement
95
- requirements:
96
- - - "~>"
97
- - !ruby/object:Gem::Version
98
- version: '1.6'
99
- - !ruby/object:Gem::Dependency
100
- name: rake
101
- requirement: !ruby/object:Gem::Requirement
102
- requirements:
103
- - - ">="
104
- - !ruby/object:Gem::Version
105
- version: '0'
96
+ requirement: &id006 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ hash: 3
102
+ segments:
103
+ - 1
104
+ - 6
105
+ version: "1.6"
106
106
  type: :development
107
+ version_requirements: *id006
108
+ - !ruby/object:Gem::Dependency
109
+ name: rake
107
110
  prerelease: false
108
- version_requirements: !ruby/object:Gem::Requirement
109
- requirements:
111
+ requirement: &id007 !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
110
114
  - - ">="
111
- - !ruby/object:Gem::Version
112
- version: '0'
115
+ - !ruby/object:Gem::Version
116
+ hash: 3
117
+ segments:
118
+ - 0
119
+ version: "0"
120
+ type: :development
121
+ version_requirements: *id007
113
122
  description: A cut down fork of the Id gem developed at On The Beach Ltd.
114
- email:
123
+ email:
115
124
  - jon.doveston@onthebeach.co.uk
116
125
  executables: []
126
+
117
127
  extensions: []
128
+
118
129
  extra_rdoc_files: []
119
- files:
120
- - ".gitignore"
121
- - ".ruby-version"
122
- - ".travis.yml"
130
+
131
+ files:
132
+ - .gitignore
133
+ - .ruby-version
134
+ - .travis.yml
123
135
  - CONTRIBUTING.md
124
136
  - Gemfile
125
137
  - LICENSE.md
@@ -127,6 +139,7 @@ files:
127
139
  - Rakefile
128
140
  - lib/structural.rb
129
141
  - lib/structural/hashifier.rb
142
+ - lib/structural/invalid_default_type_error.rb
130
143
  - lib/structural/missing_attribute_error.rb
131
144
  - lib/structural/model.rb
132
145
  - lib/structural/model/association.rb
@@ -146,33 +159,43 @@ files:
146
159
  - spec/spec_helper.rb
147
160
  - structural.gemspec
148
161
  homepage: https://github.com/onthebeach/structural
149
- licenses:
162
+ licenses:
150
163
  - MIT
151
- metadata: {}
152
164
  post_install_message:
153
165
  rdoc_options: []
154
- require_paths:
166
+
167
+ require_paths:
155
168
  - lib
156
- required_ruby_version: !ruby/object:Gem::Requirement
157
- requirements:
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ none: false
171
+ requirements:
158
172
  - - ">="
159
- - !ruby/object:Gem::Version
160
- version: '0'
161
- required_rubygems_version: !ruby/object:Gem::Requirement
162
- requirements:
173
+ - !ruby/object:Gem::Version
174
+ hash: 3
175
+ segments:
176
+ - 0
177
+ version: "0"
178
+ required_rubygems_version: !ruby/object:Gem::Requirement
179
+ none: false
180
+ requirements:
163
181
  - - ">="
164
- - !ruby/object:Gem::Version
165
- version: '0'
182
+ - !ruby/object:Gem::Version
183
+ hash: 3
184
+ segments:
185
+ - 0
186
+ version: "0"
166
187
  requirements: []
188
+
167
189
  rubyforge_project:
168
- rubygems_version: 2.5.1
190
+ rubygems_version: 1.8.25
169
191
  signing_key:
170
- specification_version: 4
192
+ specification_version: 3
171
193
  summary: Simple models based on hashes
172
- test_files:
194
+ test_files:
173
195
  - spec/lib/structural/model/association_spec.rb
174
196
  - spec/lib/structural/model/field_spec.rb
175
197
  - spec/lib/structural/model/type_casts_spec.rb
176
198
  - spec/lib/structural/model_spec.rb
177
199
  - spec/lib/structural/timestamps_spec.rb
178
200
  - spec/spec_helper.rb
201
+ has_rdoc:
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: d2e683ae4dc0a1839b52352576db3fc726201321
4
- data.tar.gz: 947df1932b578c7c5e786b2e6baf4463f2d10312
5
- SHA512:
6
- metadata.gz: c4da802d16a95c7d4b5516697fb9c6c76687032686e693830c6c3f8b44a3aa47c3f9adb3ca9341ccf378afafb0e75b118ac186ed612d9ab7fa274efefffcd4a7
7
- data.tar.gz: a1f58451da825b7bc071958150407011f743d2ab4411692095b6197a0a3377a749ca6de810170b1098dfa525457e00ff83e398ece251d3d2d7b1088e9dbc5e26