gorillib 0.1.5 → 0.1.6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.textile CHANGED
@@ -1,3 +1,12 @@
1
+ h3. 2011-08-21 - Version 0.1.6: Serialization and DeepHash
2
+
3
+ * Serialization with #to_wire -- like #to_hash, but hands #to_wire down the line to any element that contains it (as opposed to `#to_hash`, which should just do that)
4
+ * Hashlike#tree_merge: combined into the one version; gave it a block in the middle to do any fancy footwork
5
+ * deep_hash -- allows dotted (a.b.c) access to a nested hash
6
+ * Array#random_element -- gets a random member of the array.
7
+
8
+ Will soon be deprecating Receiver, in favor of the far more powerful Icss::ReceiverModel in the icss library.
9
+
1
10
  h3. 2011-06-29 - Version 0.1.3: Fancier receivers
2
11
 
3
12
  * can now mix activemodel into a receiver, getting all its validation and other awesomeness
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source "http://rubygems.org"
2
2
 
3
+ gem "json"
4
+
3
5
  # Add dependencies to develop your gem here.
4
6
  # Include everything needed to run rake, tests, features, etc.
5
7
  group :development do
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.5
1
+ 0.1.6
data/gorillib.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{gorillib}
8
- s.version = "0.1.5"
8
+ s.version = "0.1.6"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = [%q{Infochimps}]
12
- s.date = %q{2011-08-02}
12
+ s.date = %q{2011-08-22}
13
13
  s.description = %q{Gorillib: infochimps lightweight subset of ruby convenience methods}
14
14
  s.email = %q{coders@infochimps.org}
15
15
  s.extra_rdoc_files = [
@@ -30,6 +30,7 @@ Gem::Specification.new do |s|
30
30
  "lib/gorillib/array/compact_blank.rb",
31
31
  "lib/gorillib/array/deep_compact.rb",
32
32
  "lib/gorillib/array/extract_options.rb",
33
+ "lib/gorillib/array/random.rb",
33
34
  "lib/gorillib/base.rb",
34
35
  "lib/gorillib/datetime/flat.rb",
35
36
  "lib/gorillib/datetime/parse.rb",
@@ -75,6 +76,7 @@ Gem::Specification.new do |s|
75
76
  "lib/gorillib/receiver/tree_diff.rb",
76
77
  "lib/gorillib/receiver/validations.rb",
77
78
  "lib/gorillib/receiver_model.rb",
79
+ "lib/gorillib/serialization.rb",
78
80
  "lib/gorillib/some.rb",
79
81
  "lib/gorillib/string/constantize.rb",
80
82
  "lib/gorillib/string/human.rb",
@@ -193,6 +195,7 @@ Gem::Specification.new do |s|
193
195
  s.specification_version = 3
194
196
 
195
197
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
198
+ s.add_runtime_dependency(%q<json>, [">= 0"])
196
199
  s.add_development_dependency(%q<bundler>, ["~> 1.0.12"])
197
200
  s.add_development_dependency(%q<yard>, ["~> 0.6.7"])
198
201
  s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -209,6 +212,7 @@ Gem::Specification.new do |s|
209
212
  s.add_development_dependency(%q<spork>, ["~> 0.9.0.rc5"])
210
213
  s.add_development_dependency(%q<watchr>, [">= 0"])
211
214
  else
215
+ s.add_dependency(%q<json>, [">= 0"])
212
216
  s.add_dependency(%q<bundler>, ["~> 1.0.12"])
213
217
  s.add_dependency(%q<yard>, ["~> 0.6.7"])
214
218
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -226,6 +230,7 @@ Gem::Specification.new do |s|
226
230
  s.add_dependency(%q<watchr>, [">= 0"])
227
231
  end
228
232
  else
233
+ s.add_dependency(%q<json>, [">= 0"])
229
234
  s.add_dependency(%q<bundler>, ["~> 1.0.12"])
230
235
  s.add_dependency(%q<yard>, ["~> 0.6.7"])
231
236
  s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
@@ -0,0 +1,8 @@
1
+ class Array
2
+ #
3
+ # Choose a random element from the array
4
+ #
5
+ def random_element
6
+ self[rand(length)]
7
+ end unless method_defined?(:random_element)
8
+ end
@@ -2,38 +2,40 @@ module Gorillib
2
2
  module Hashlike
3
3
  module TreeMerge
4
4
 
5
- # Recursively merges using receive
6
- #
7
- # Modifies the full receiver chain in-place.
5
+ # Recursively merges hashlike objects
8
6
  #
9
7
  # For each key in keys,
10
- # * if self's value is nil, receive the attribute.
8
+ # * if block_given? and yield(key,self_val,other_val) returns non-nil, set that
9
+ # * if self is missing value for key, receive the attribute.
11
10
  # * if self's attribute is an Array, append to it.
12
- # * if self's value responds to tree_merge!, tree merge it.
11
+ # * if self's value responds to tree_merge!, deep merge it.
13
12
  # * if self's value responds_to merge!, merge! it.
14
13
  # * otherwise, receive the value from other_hash
15
14
  #
16
15
  def tree_merge!(other_hash)
17
- keys.each do |key|
16
+ return self if other_hash.blank?
17
+ [self.keys, other_hash.keys].flatten.uniq.each do |key|
18
18
  # get other's val if any
19
19
  if other_hash.has_key?(key.to_sym) then other_val = other_hash[key.to_sym]
20
20
  elsif other_hash.has_key?(key.to_s) then other_val = other_hash[key.to_s]
21
21
  else next ; end
22
- #
22
+ # get self val if any
23
23
  self_val = self[key]
24
- # p ['receiver tree_merge', key, self_val.respond_to?(:tree_merge!), self[key], other_val]
24
+ # get block resolved result if any
25
+ if block_given? && yield(key, self_val, other_val)
26
+ next
27
+ end
28
+ # p ['hash tree_merge', key, self_val.respond_to?(:tree_merge!), self_val, '***************', other_val]
29
+ #
25
30
  case
26
31
  when other_val.nil? then next
27
- when (not has_key?(key)) then _receive_attr(key, other_val)
28
- when receiver_attrs[key][:merge_as] == :hash_of_arrays
29
- self_val.merge!(other_val) do |k, v1, v2| case when v1.blank? then v2 when v2.blank? then v1 else v1 + v2 end end
32
+ when (not has_key?(key)) then self[key] = other_val
30
33
  when self_val.is_a?(Array) then self[key] += other_val
31
34
  when self_val.respond_to?(:tree_merge!) then self[key] = self_val.tree_merge!(other_val)
32
35
  when self_val.respond_to?(:merge!) then self[key] = self_val.merge!(other_val)
33
- else _receive_attr(key, other_val)
36
+ else self[key] = other_val
34
37
  end
35
38
  end
36
- run_after_receivers(other_hash)
37
39
  self
38
40
  end
39
41
 
@@ -41,36 +43,7 @@ module Gorillib
41
43
  end
42
44
  end
43
45
 
46
+
44
47
  class Hash
45
- # Recursively merges using receive
46
- #
47
- # Modifies the full receiver chain in-place.
48
- #
49
- # For each key in keys,
50
- # * if self's value is nil, receive the attribute.
51
- # * if self's attribute is an Array, append to it.
52
- # * if self's value responds to tree_merge!, deep merge it.
53
- # * if self's value responds_to merge!, merge! it.
54
- # * otherwise, receive the value from other_hash
55
- #
56
- def tree_merge!(other_hash)
57
- [self.keys, other_hash.keys].flatten.uniq.each do |key|
58
- # get other's val if any
59
- if other_hash.has_key?(key.to_sym) then other_val = other_hash[key.to_sym]
60
- elsif other_hash.has_key?(key.to_s) then other_val = other_hash[key.to_s]
61
- else next ; end
62
- #
63
- self_val = self[key]
64
- # p ['hash tree_merge', key, self_val.respond_to?(:tree_merge!), self_val, other_val]
65
- case
66
- when other_val.nil? then next
67
- when (not has_key?(key)) then self[key] = other_val
68
- when self_val.is_a?(Array) then self[key] += other_val
69
- when self_val.respond_to?(:tree_merge!) then self[key] = self_val.tree_merge!(other_val)
70
- when self_val.respond_to?(:merge!) then self[key] = self_val.merge!(other_val)
71
- else self[key] = other_val
72
- end
73
- end
74
- self
75
- end
48
+ include Gorillib::Hashlike::TreeMerge
76
49
  end
@@ -1,5 +1,6 @@
1
1
  require 'gorillib/object/blank'
2
2
  require 'gorillib/object/try'
3
+ require 'gorillib/object/try_dup'
3
4
  require 'gorillib/array/extract_options'
4
5
  require 'gorillib/metaprogramming/class_attribute'
5
6
 
@@ -169,7 +170,7 @@ protected
169
170
  def impose_defaults!(hsh)
170
171
  _receiver_defaults.each do |attr, val|
171
172
  next if attr_set?(attr)
172
- self.instance_variable_set "@#{attr}", val
173
+ self.instance_variable_set "@#{attr}", val.try_dup
173
174
  end
174
175
  end
175
176
 
@@ -181,7 +182,7 @@ protected
181
182
  #
182
183
  # f = Foo.receive({:attribute => 'bad'})
183
184
  # => #<Foo:0x10156c820 @attribute="good">
184
-
185
+ #
185
186
  def replace_options!(hsh)
186
187
  self.receiver_attrs.each do |attr, info|
187
188
  val = self.instance_variable_get("@#{attr}")
@@ -191,7 +192,6 @@ protected
191
192
  end
192
193
  end
193
194
 
194
-
195
195
  def run_after_receivers(hsh)
196
196
  _after_receivers.each do |after_receiver|
197
197
  self.instance_exec(hsh, &after_receiver)
@@ -0,0 +1,50 @@
1
+ module Gorillib
2
+
3
+ module Hashlike
4
+ module Serialization
5
+ #
6
+ # Returns a hash with each key set to its associated value
7
+ #
8
+ # @example
9
+ # my_hshlike = MyHashlike.new
10
+ # my_hshlike[:a] = 100; my_hshlike[:b] = 200
11
+ # my_hshlike.to_hash # => { :a => 100, :b => 200 }
12
+ #
13
+ # @return [Hash] a new Hash instance, with each key set to its associated value.
14
+ #
15
+ def to_wire(options={})
16
+ {}.tap do |hsh|
17
+ each do |attr,val|
18
+ hsh[attr] =
19
+ case
20
+ when val.respond_to?(:to_wire) then val.to_wire(options)
21
+ when val.respond_to?(:to_hash) then val.to_hash
22
+ else val ; end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ module Gorillib::Hashlike
31
+ include ::Gorillib::Hashlike::Serialization
32
+ end
33
+
34
+ class ::Array
35
+ def to_wire(options={})
36
+ map{|item| item.respond_to?(:to_wire) ? item.to_wire : item }
37
+ end
38
+ end
39
+
40
+ class ::Hash
41
+ include ::Gorillib::Hashlike::Serialization
42
+ end
43
+
44
+ class ::Time
45
+ def to_wire(options={})
46
+ self.iso8601
47
+ end
48
+ end
49
+
50
+
@@ -467,7 +467,7 @@ describe Receiver do
467
467
 
468
468
  # ---------------------------------------------------------------------------
469
469
 
470
- describe 'core class .receive method' do
470
+ it 'core class .receive method' do
471
471
  Symbol.receive('hi').should == :hi
472
472
  Integer.receive(3.4).should == 3
473
473
  Float.receive("4.5").should == 4.5
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: gorillib
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.5
5
+ version: 0.1.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Infochimps
@@ -10,11 +10,22 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-02 00:00:00 Z
13
+ date: 2011-08-22 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: bundler
16
+ name: json
17
17
  requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: bundler
28
+ requirement: &id002 !ruby/object:Gem::Requirement
18
29
  none: false
19
30
  requirements:
20
31
  - - ~>
@@ -22,10 +33,10 @@ dependencies:
22
33
  version: 1.0.12
23
34
  type: :development
24
35
  prerelease: false
25
- version_requirements: *id001
36
+ version_requirements: *id002
26
37
  - !ruby/object:Gem::Dependency
27
38
  name: yard
28
- requirement: &id002 !ruby/object:Gem::Requirement
39
+ requirement: &id003 !ruby/object:Gem::Requirement
29
40
  none: false
30
41
  requirements:
31
42
  - - ~>
@@ -33,10 +44,10 @@ dependencies:
33
44
  version: 0.6.7
34
45
  type: :development
35
46
  prerelease: false
36
- version_requirements: *id002
47
+ version_requirements: *id003
37
48
  - !ruby/object:Gem::Dependency
38
49
  name: jeweler
39
- requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirement: &id004 !ruby/object:Gem::Requirement
40
51
  none: false
41
52
  requirements:
42
53
  - - ~>
@@ -44,10 +55,10 @@ dependencies:
44
55
  version: 1.5.2
45
56
  type: :development
46
57
  prerelease: false
47
- version_requirements: *id003
58
+ version_requirements: *id004
48
59
  - !ruby/object:Gem::Dependency
49
60
  name: rspec
50
- requirement: &id004 !ruby/object:Gem::Requirement
61
+ requirement: &id005 !ruby/object:Gem::Requirement
51
62
  none: false
52
63
  requirements:
53
64
  - - ~>
@@ -55,10 +66,10 @@ dependencies:
55
66
  version: 2.5.0
56
67
  type: :development
57
68
  prerelease: false
58
- version_requirements: *id004
69
+ version_requirements: *id005
59
70
  - !ruby/object:Gem::Dependency
60
71
  name: spork
61
- requirement: &id005 !ruby/object:Gem::Requirement
72
+ requirement: &id006 !ruby/object:Gem::Requirement
62
73
  none: false
63
74
  requirements:
64
75
  - - ~>
@@ -66,10 +77,10 @@ dependencies:
66
77
  version: 0.9.0.rc5
67
78
  type: :development
68
79
  prerelease: false
69
- version_requirements: *id005
80
+ version_requirements: *id006
70
81
  - !ruby/object:Gem::Dependency
71
82
  name: RedCloth
72
- requirement: &id006 !ruby/object:Gem::Requirement
83
+ requirement: &id007 !ruby/object:Gem::Requirement
73
84
  none: false
74
85
  requirements:
75
86
  - - ">="
@@ -77,10 +88,10 @@ dependencies:
77
88
  version: "0"
78
89
  type: :development
79
90
  prerelease: false
80
- version_requirements: *id006
91
+ version_requirements: *id007
81
92
  - !ruby/object:Gem::Dependency
82
93
  name: rcov
83
- requirement: &id007 !ruby/object:Gem::Requirement
94
+ requirement: &id008 !ruby/object:Gem::Requirement
84
95
  none: false
85
96
  requirements:
86
97
  - - ">="
@@ -88,10 +99,10 @@ dependencies:
88
99
  version: 0.9.9
89
100
  type: :development
90
101
  prerelease: false
91
- version_requirements: *id007
102
+ version_requirements: *id008
92
103
  - !ruby/object:Gem::Dependency
93
104
  name: watchr
94
- requirement: &id008 !ruby/object:Gem::Requirement
105
+ requirement: &id009 !ruby/object:Gem::Requirement
95
106
  none: false
96
107
  requirements:
97
108
  - - ">="
@@ -99,10 +110,10 @@ dependencies:
99
110
  version: "0"
100
111
  type: :development
101
112
  prerelease: false
102
- version_requirements: *id008
113
+ version_requirements: *id009
103
114
  - !ruby/object:Gem::Dependency
104
115
  name: bundler
105
- requirement: &id009 !ruby/object:Gem::Requirement
116
+ requirement: &id010 !ruby/object:Gem::Requirement
106
117
  none: false
107
118
  requirements:
108
119
  - - ~>
@@ -110,10 +121,10 @@ dependencies:
110
121
  version: 1.0.12
111
122
  type: :development
112
123
  prerelease: false
113
- version_requirements: *id009
124
+ version_requirements: *id010
114
125
  - !ruby/object:Gem::Dependency
115
126
  name: yard
116
- requirement: &id010 !ruby/object:Gem::Requirement
127
+ requirement: &id011 !ruby/object:Gem::Requirement
117
128
  none: false
118
129
  requirements:
119
130
  - - ~>
@@ -121,10 +132,10 @@ dependencies:
121
132
  version: 0.6.7
122
133
  type: :development
123
134
  prerelease: false
124
- version_requirements: *id010
135
+ version_requirements: *id011
125
136
  - !ruby/object:Gem::Dependency
126
137
  name: jeweler
127
- requirement: &id011 !ruby/object:Gem::Requirement
138
+ requirement: &id012 !ruby/object:Gem::Requirement
128
139
  none: false
129
140
  requirements:
130
141
  - - ~>
@@ -132,10 +143,10 @@ dependencies:
132
143
  version: 1.5.2
133
144
  type: :development
134
145
  prerelease: false
135
- version_requirements: *id011
146
+ version_requirements: *id012
136
147
  - !ruby/object:Gem::Dependency
137
148
  name: rspec
138
- requirement: &id012 !ruby/object:Gem::Requirement
149
+ requirement: &id013 !ruby/object:Gem::Requirement
139
150
  none: false
140
151
  requirements:
141
152
  - - ~>
@@ -143,10 +154,10 @@ dependencies:
143
154
  version: 2.5.0
144
155
  type: :development
145
156
  prerelease: false
146
- version_requirements: *id012
157
+ version_requirements: *id013
147
158
  - !ruby/object:Gem::Dependency
148
159
  name: rcov
149
- requirement: &id013 !ruby/object:Gem::Requirement
160
+ requirement: &id014 !ruby/object:Gem::Requirement
150
161
  none: false
151
162
  requirements:
152
163
  - - ">="
@@ -154,10 +165,10 @@ dependencies:
154
165
  version: 0.9.9
155
166
  type: :development
156
167
  prerelease: false
157
- version_requirements: *id013
168
+ version_requirements: *id014
158
169
  - !ruby/object:Gem::Dependency
159
170
  name: spork
160
- requirement: &id014 !ruby/object:Gem::Requirement
171
+ requirement: &id015 !ruby/object:Gem::Requirement
161
172
  none: false
162
173
  requirements:
163
174
  - - ~>
@@ -165,10 +176,10 @@ dependencies:
165
176
  version: 0.9.0.rc5
166
177
  type: :development
167
178
  prerelease: false
168
- version_requirements: *id014
179
+ version_requirements: *id015
169
180
  - !ruby/object:Gem::Dependency
170
181
  name: watchr
171
- requirement: &id015 !ruby/object:Gem::Requirement
182
+ requirement: &id016 !ruby/object:Gem::Requirement
172
183
  none: false
173
184
  requirements:
174
185
  - - ">="
@@ -176,7 +187,7 @@ dependencies:
176
187
  version: "0"
177
188
  type: :development
178
189
  prerelease: false
179
- version_requirements: *id015
190
+ version_requirements: *id016
180
191
  description: "Gorillib: infochimps lightweight subset of ruby convenience methods"
181
192
  email: coders@infochimps.org
182
193
  executables: []
@@ -200,6 +211,7 @@ files:
200
211
  - lib/gorillib/array/compact_blank.rb
201
212
  - lib/gorillib/array/deep_compact.rb
202
213
  - lib/gorillib/array/extract_options.rb
214
+ - lib/gorillib/array/random.rb
203
215
  - lib/gorillib/base.rb
204
216
  - lib/gorillib/datetime/flat.rb
205
217
  - lib/gorillib/datetime/parse.rb
@@ -245,6 +257,7 @@ files:
245
257
  - lib/gorillib/receiver/tree_diff.rb
246
258
  - lib/gorillib/receiver/validations.rb
247
259
  - lib/gorillib/receiver_model.rb
260
+ - lib/gorillib/serialization.rb
248
261
  - lib/gorillib/some.rb
249
262
  - lib/gorillib/string/constantize.rb
250
263
  - lib/gorillib/string/human.rb
@@ -315,7 +328,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
315
328
  requirements:
316
329
  - - ">="
317
330
  - !ruby/object:Gem::Version
318
- hash: 168082518924887953
331
+ hash: 2753005765957482332
319
332
  segments:
320
333
  - 0
321
334
  version: "0"