bound 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ecdc27f8c70cbf71b740d8c1d49cace05dbc6fa6
4
- data.tar.gz: 596dbd67da344d3ab3503755e1c6fe4f7c1c46f6
3
+ metadata.gz: 55ee3700c0f6aff1e212a4b05a2d1c7eab87b9f7
4
+ data.tar.gz: 2a50137771a1b061051f14c46dd7b1ec8cd3ec95
5
5
  SHA512:
6
- metadata.gz: 1a133d2ca72cab2dcf05425ec5442b26a617cb93230785d3008ce6261c4b9f628a42120dcd531e00fa3d1ef7a422eab3dc115daa84aed40a1a28951a56b27e61
7
- data.tar.gz: a328984a7a01c4419f56e030b9c98e54c576a512ca59db953a2bf0f4780f4018dd320c035416dd0d331b4aae947a1e3f107e33b126bc3154f6dc306c1401370b
6
+ metadata.gz: aa4817e8a7046bb2d5e56da17a466968061497b036109057037e8f747c32332b25b4f247a3e0202ada60dc4555d78a92561adec5564b88c63294977da5f50f62
7
+ data.tar.gz: 804361979bd44ff9a1a42cb18e08300ae6169a518f9b3e5f2be387b7f1a9169f80356386afb800bb44c692f9a55e7cb36e9199be45add2dae642a9d1384cdff6
@@ -2,11 +2,7 @@ require "bound/version"
2
2
 
3
3
  class Bound
4
4
  def self.new(*args)
5
- new_bound_class.set_attributes(*args)
6
- end
7
-
8
- def self.nested(*args)
9
- new_bound_class.nested(*args)
5
+ new_bound_class.required(*args)
10
6
  end
11
7
 
12
8
  def self.optional(*args)
@@ -14,13 +10,7 @@ class Bound
14
10
  end
15
11
 
16
12
  def self.required(*args)
17
- bound = new_bound_class
18
-
19
- if args.last.kind_of? Hash
20
- bound.nested(args.pop)
21
- end
22
-
23
- bound.set_attributes(*args)
13
+ new_bound_class.required(*args)
24
14
  end
25
15
 
26
16
  private
@@ -132,51 +122,57 @@ class Bound
132
122
  self.nested_attr_classes = {}
133
123
  end
134
124
 
135
- def set_attributes(*attributes)
136
- if attributes.any? { |a| !a.kind_of? Symbol }
137
- raise ArgumentError.new("Invalid list of attributes: #{attributes.inspect}")
138
- end
139
-
140
- attributes.each do |attribute|
141
- self.attrs[attribute] = RequiredAttribute
125
+ def optional(*attributes)
126
+ if attributes.last.kind_of? Hash
127
+ nested_attributes = attributes.pop
128
+ else
129
+ nested_attributes = {}
142
130
  end
143
131
 
144
- define_attribute_accessors attributes
132
+ set_attributes :optional, attributes, nested_attributes
145
133
 
146
134
  self
147
135
  end
148
136
 
149
- def optional(*optionals)
150
- if optionals.any? { |a| !a.kind_of? Symbol }
151
- raise ArgumentError.new("Invalid list of optional attributes: #{optionals.inspect}")
152
- end
153
-
154
- optionals.each do |attribute|
155
- self.attrs[attribute] = Attribute
137
+ def required(*attributes)
138
+ if attributes.last.kind_of? Hash
139
+ nested_attributes = attributes.pop
140
+ else
141
+ nested_attributes = {}
156
142
  end
157
143
 
158
- define_attribute_accessors optionals
144
+ set_attributes :required, attributes, nested_attributes
159
145
 
160
146
  self
161
147
  end
162
148
 
163
- def nested(nested_attributes)
164
- attributes = nested_attributes.keys
149
+ private
150
+
151
+ def set_attributes(flag, attributes, nested_attributes = {})
152
+ is_optional = flag == :optional
153
+
154
+ if attributes.any? { |a| !a.kind_of? Symbol }
155
+ raise ArgumentError.new("Invalid list of attributes: #{attributes.inspect}")
156
+ end
165
157
 
166
158
  attributes.each do |attribute|
167
- self.attrs[attribute] = RequiredAttribute
168
- self.nested_attr_classes[attribute] = nested_attributes[attribute]
159
+ if is_optional
160
+ self.attrs[attribute] = Attribute
161
+ else
162
+ self.attrs[attribute] = RequiredAttribute
163
+ end
169
164
  end
170
165
 
171
166
  define_attribute_accessors attributes
172
167
 
173
- self
168
+ if nested_attributes.any?
169
+ set_attributes flag, nested_attributes.keys
170
+ nested_attributes.each do |attribute_name, attribute_class|
171
+ self.nested_attr_classes[attribute_name] = attribute_class
172
+ end
173
+ end
174
174
  end
175
175
 
176
- alias :build :new
177
-
178
- private
179
-
180
176
  def define_attribute_accessors(attributes)
181
177
  define_attribute_readers attributes
182
178
  define_attribute_writers attributes
@@ -1,3 +1,3 @@
1
1
  class Bound
2
- VERSION = "0.2.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -24,15 +24,6 @@ describe Bound do
24
24
  end
25
25
  end
26
26
 
27
- it 'also sets all attributes with new instead of build' do
28
- [hash, object].each do |subject|
29
- user = User.new(subject)
30
-
31
- assert_equal hash[:name], user.name
32
- assert_equal hash[:age], user.age
33
- end
34
- end
35
-
36
27
  it 'fails if attribute is missing' do
37
28
  hash.delete :age
38
29
 
@@ -130,7 +121,7 @@ describe Bound do
130
121
  end
131
122
 
132
123
  it 'are also included in attributes' do
133
- user = UserWithoutAge.build(hash)
124
+ user = UserWithoutAge.new(hash)
134
125
 
135
126
  assert_equal 2, user.get_attributes.size
136
127
  assert_includes user.get_attributes.map(&:name), :name
@@ -138,6 +129,44 @@ describe Bound do
138
129
  end
139
130
  end
140
131
 
132
+ describe 'optional nested attributes' do
133
+ UserWithProfile = Bound.required(:id).optional(
134
+ :profile => Bound.required(:age)
135
+ )
136
+ let(:hash) do
137
+ {
138
+ :id => 12,
139
+ :profile => {
140
+ :age => 23
141
+ }
142
+ }
143
+ end
144
+
145
+ it 'sets optional attributes' do
146
+ [hash, object].each do |subject|
147
+ user = UserWithProfile.new(subject)
148
+
149
+ assert_equal hash[:profile][:age], user.profile.age
150
+ end
151
+ end
152
+
153
+ it 'works if optional attribute is missing' do
154
+ hash.delete :profile
155
+
156
+ [hash, object].each do |subject|
157
+ UserWithProfile.new(subject)
158
+ end
159
+ end
160
+
161
+ it 'are also included in attributes' do
162
+ user = UserWithProfile.new(hash)
163
+
164
+ assert_equal 2, user.get_attributes.size
165
+ assert_includes user.get_attributes.map(&:name), :id
166
+ assert_includes user.get_attributes.map(&:name), :profile
167
+ end
168
+ end
169
+
141
170
  describe 'no attributes' do
142
171
  UserWithoutAttributes = Bound.new
143
172
  let(:hash) { Hash.new }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bound
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jakob Holderbaum
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-01-20 00:00:00.000000000 Z
12
+ date: 2014-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -109,7 +109,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
109
  version: '0'
110
110
  requirements: []
111
111
  rubyforge_project:
112
- rubygems_version: 2.0.3
112
+ rubygems_version: 2.1.11
113
113
  signing_key:
114
114
  specification_version: 4
115
115
  summary: Implements a nice helper for fast boundary definitions