model_pack 0.9.1 → 0.9.2

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: a566b021fe29847c5ec63d781de237c037f693b9
4
- data.tar.gz: fd18beaa67efba4dc52f43488c494ecf489f4e2a
3
+ metadata.gz: dabb01b01889b40d35d86225715aa392b26c9998
4
+ data.tar.gz: 85c4e83fb4e2fc2af86d31f864c0277e04d0db54
5
5
  SHA512:
6
- metadata.gz: 512ef1c65902e3e0eeb14f53e4c69152853c9b7c0353a9a58c3a5294edd36fd786e50a8b9b8ad1d657975f5d661356d3954cb124b0cf4d10a70b1586397d52d4
7
- data.tar.gz: d9e615aef19f355b001e65a6a5a85527b0330d6805597e5a5ef43bdc5213a22bf762297844737d3fe7b32a1b7a278fe96433c17850a1babc45171f1a3d6b88ff
6
+ metadata.gz: cf57987290bb914c78f5cfa6d62bb00ebc26f2676cace86713feba5d9ef2a2504a8a2803558e4f1811550daadd9b6c461286aeb49d86c185c98e434e934fabb3
7
+ data.tar.gz: 7f587c452db113574c71c11612781e0001c1db57f8e0387ade3d3677d3754ce0b10d34a6d57473453d63da0ae82901579af5c12d8326d9ea5f9148162376d98c
data/README.md CHANGED
@@ -60,9 +60,9 @@ puts line.length
60
60
  ```ruby
61
61
  class Polygon
62
62
  include ModelPack::Document
63
-
63
+
64
64
  array :points, class_name: Point
65
-
65
+
66
66
  def sides
67
67
  return [] if points.size < 2
68
68
  [[points.first]].tap do |sides|
@@ -73,7 +73,7 @@ class Polygon
73
73
  sides.last.push(points.first)
74
74
  end
75
75
  end
76
-
76
+
77
77
  def perimeter
78
78
  sides.inject(0) { |sum, side| sum + Math.sqrt((side[0].x-side[1].x)**2 + (side[0].y-side[1].y)**2) }
79
79
  end
@@ -86,7 +86,7 @@ puts polygon.perimeter
86
86
 
87
87
  ```ruby
88
88
  polygon = Polygon.new(points: [{x: 0, y: 0}, {x:5, y:0}, {x:5, y:5}])
89
- json = polygon.as_json() # same serializable_hash
89
+ json = polygon.as_json # same serializable_hash
90
90
  ```
91
91
 
92
92
  ### Копирование моделей
@@ -94,7 +94,7 @@ json = polygon.as_json() # same serializable_hash
94
94
  ```ruby
95
95
  polygon = Polygon.new(points: [{x: 3, y: 3}, {x:2, y:1}, {x:4, y:2}])
96
96
  polygon_copy = polygon.copy # same polygon_copy = Polygon.new(polygon.serializable_hash)
97
- puts polygon_copy
97
+ puts polygon_copy.serializable_hash
98
98
  ```
99
99
 
100
100
  ### Выборочная сериализация
@@ -102,9 +102,9 @@ puts polygon_copy
102
102
  ```ruby
103
103
  class SecureData
104
104
  include ModelPack::Document
105
- attribute :hidden_field, writer: lambda { |v| nil }
106
- attribute :const_field, writer: lambda { |v| :always_this }
107
- attribute :always_string, writer: lambda { |v| v.to_s }
105
+ attribute :hidden_field, serialize: lambda { |v| nil }
106
+ attribute :const_field, serialize: lambda { |v| :always_this }
107
+ attribute :always_string, serialize: lambda { |v| v.to_s }
108
108
  end
109
109
  secure_data = SecureData.new( hidden_field: "secured text", const_field: :some_value, always_string: 55)
110
110
  unsecure_hash = secure_data.serializable_hash
@@ -18,14 +18,14 @@ module ModelPack
18
18
  self.class.attribute_names.inject({}) { |h, name| h[name] = send(name); h }
19
19
  end
20
20
 
21
- def update_attributes(*attributes)
21
+ def update_attributes(attributes)
22
22
  attributes.each do |name, attribute|
23
23
  key = "#{name}="
24
24
  send(key, attribute) if respond_to?(key)
25
25
  end
26
26
  end
27
27
 
28
- def update_attributes!(*attributes)
28
+ def update_attributes!(attributes)
29
29
  # check present first
30
30
  attributes.each do |name, attribute|
31
31
  raise ArgumentError, "undefined attribute `#{name}`" unless attribute_names.include?(method)
@@ -1,8 +1,8 @@
1
1
  module ModelPack
2
2
  module ClassMethods
3
3
 
4
- def attribute(name, writer: lambda { |v| v }, default: nil, as: nil, serialize: nil)
5
- attribute_reader(name, default: default, as: as, serialize: serialize)
4
+ def attribute(name, writer: lambda { |v| v }, default: nil, as: nil, serialize: nil, predicate: nil)
5
+ attribute_reader(name, default: default, as: as, serialize: serialize, predicate: predicate)
6
6
  attribute_writer(name, writer: writer)
7
7
  register_attribute(name)
8
8
  end
@@ -13,7 +13,7 @@ module ModelPack
13
13
  end
14
14
  end
15
15
 
16
- def attribute_reader(name, default: nil, as: nil, serialize: nil)
16
+ def attribute_reader(name, default: nil, as: nil, serialize: nil, predicate: nil)
17
17
  default_dup = default.dup rescue default
18
18
  default_value = default_dup || (as && as.new)
19
19
 
@@ -21,6 +21,12 @@ module ModelPack
21
21
  instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", default_value)
22
22
  end
23
23
 
24
+ # define predicate method if required
25
+ define_method "#{name}?" do
26
+ value = (instance_variable_defined?("@#{name}") ? instance_variable_get("@#{name}") : instance_variable_set("@#{name}", default_value))
27
+ predicate.is_a?(Proc) ? predicate.call(value) : !!value # false for nil or false
28
+ end if predicate
29
+
24
30
  define_method "#{name}_hash" do
25
31
  instance_exec(send(name), &serialize)
26
32
  end if serialize
@@ -38,13 +44,14 @@ module ModelPack
38
44
  default: default,
39
45
  serialize: serialize,
40
46
  as: Array,
41
- writer: writer || lambda { |array| array.collect { |v| v.is_a?(Hash) && class_name ? class_name.new(v) : v } })
47
+ writer: writer || lambda { |array| array.is_a?(Array) ? (array.collect { |v| v.is_a?(Hash) && class_name ? class_name.new(v) : v }) : [] })
42
48
  end
43
49
 
44
- def hashable(name, default: nil, serialize: nil)
50
+ def dictionary(name, class_name: nil, default: nil, serialize: nil, writer: nil)
45
51
  attribute(name,
46
52
  default: default,
47
53
  serialize: serialize,
54
+ writer: writer || lambda { |dictionary| Hash[dictionary.map { |k,v| [k, v.is_a?(Hash) && class_name ? class_name.new(v) : v]}] },
48
55
  as: Hash)
49
56
  end
50
57
  end
@@ -2,7 +2,7 @@ module ModelPack
2
2
  module Constructor
3
3
  def initialize(*a)
4
4
  attributes = a.pop
5
- update_attributes(*attributes) if attributes
5
+ update_attributes(attributes) if attributes
6
6
  super(*a)
7
7
  end
8
8
  end
@@ -53,7 +53,8 @@ module ModelPack
53
53
  when Hash
54
54
  enumerable_value[value, opts, global]
55
55
  end
56
- hash[n] = data if data
56
+
57
+ hash[n] = data unless data.nil?
57
58
  end
58
59
  hash
59
60
  end
@@ -1,3 +1,3 @@
1
1
  module ModelPack
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
@@ -23,6 +23,13 @@ describe ModelPack::ClassMethods do
23
23
  expect(point.attributes).to include({x: 3, y: 5})
24
24
  end
25
25
 
26
+ it "should update_attributes change values" do
27
+ point = Point.new(x: 4, y: 6)
28
+ point.update_attributes(x:3, y:2)
29
+ expect(point.x).to be(3)
30
+ expect(point.y).to be(2)
31
+ end
32
+
26
33
  it "should create embedded models" do
27
34
  class Line
28
35
  include ModelPack::Document
@@ -115,25 +122,111 @@ describe ModelPack::ClassMethods do
115
122
  end
116
123
  end
117
124
 
118
- it "should model have hashable field" do
125
+ it "should model have nary field" do
119
126
  class Options
120
127
  include ModelPack::Document
121
128
 
122
- hashable :options
129
+ dictionary :options
130
+ dictionary :points, class_name: Point
123
131
 
124
132
  def method_missing(name, *a)
125
133
  options[name]
126
134
  end
127
135
  end
128
136
 
129
- options = Options.new(options: { a: 5, b: 6 })
137
+ options = Options.new(
138
+ options: { a: 5, b: 6 },
139
+ points: {
140
+ "a" => {x:0, y:1},
141
+ "b" => {x:1, y:3},
142
+ "c" => {x:3, y:2}
143
+ }
144
+ )
130
145
 
131
146
  expect(options.a).to be(5)
132
147
  expect(options.b).to be(6)
148
+ expect(options.points['a'].x).to be(0)
149
+ expect(options.points['b'].y).to be(3)
150
+ expect(options.points['c'].y).to be(2)
151
+ end
152
+
153
+ it "should initizalize with string attributes" do
154
+ point = Point.new({"x" => 3, "y" => 5})
155
+ expect(point.x).to be(3)
156
+ expect(point.y).to be(5)
157
+ expect(point.attributes).to include({x: 3, y: 5})
133
158
  end
134
159
 
135
160
  it "should serialize model with custom serializer" do
161
+ class IntData
162
+ include ModelPack::Document
163
+ attribute :integer, serialize: lambda { |v| 5 }
164
+ end
165
+
166
+ int_data = IntData.new(integer: 13)
167
+ expect(int_data.serializable_hash[:integer]).to be(5)
168
+ end
169
+
170
+ it "should have initialize" do
171
+ class StringBuffer
172
+ include ModelPack::Document
173
+
174
+ attribute :buffer, default: ''
175
+ attribute :position
176
+
177
+ def initialize
178
+ @position = buffer.size
179
+ end
180
+ end
136
181
 
182
+ buffer = StringBuffer.new(buffer: 'Lorem Ipsum')
183
+ expect(buffer.position).to be(11)
137
184
  end
138
185
 
186
+ it "should have peredicate method" do
187
+ class OptionsWithPredicate
188
+ include ModelPack::Document
189
+
190
+ attribute :save, predicate: true
191
+ attribute :load, predicate: lambda { |v| !!v ? 'YES' : 'NO' }
192
+ end
193
+
194
+ owp = OptionsWithPredicate.new(
195
+ save: true,
196
+ load: true
197
+ )
198
+ expect(owp.save?).to be true
199
+ expect(owp.load?).to eq('YES')
200
+
201
+ owp = OptionsWithPredicate.new(
202
+ save: false,
203
+ load: false
204
+ )
205
+ expect(owp.save?).to be false
206
+ expect(owp.load?).to eq('NO')
207
+
208
+ owp = OptionsWithPredicate.new
209
+
210
+ expect(owp.save?).to be false
211
+ expect(owp.load?).to eq('NO')
212
+ end
213
+
214
+
215
+ it "should boolean type works well" do
216
+ class BooleanData
217
+ include ModelPack::Document
218
+
219
+ attribute :bit
220
+ end
221
+
222
+ true_data = BooleanData.new(bit: true)
223
+ false_data = BooleanData.new(bit: false)
224
+ copy_true_data = true_data.copy
225
+ copy_false_data = false_data.copy
226
+
227
+ expect(true_data.serializable_hash[:bit]).to be true
228
+ expect(false_data.serializable_hash[:bit]).to be false
229
+ expect(copy_true_data.serializable_hash[:bit]).to be true
230
+ expect(copy_false_data.serializable_hash[:bit]).to be false
231
+ end
139
232
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: model_pack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - che
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-10 00:00:00.000000000 Z
11
+ date: 2014-09-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler