blobject 0.4.1 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/blobject.rb +1 -31
- data/lib/blobject/version.rb +1 -1
- data/spec/blobject_spec.rb +0 -47
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8f3a8ab425b9c9b3de5ace8572a6c16843c0c560
|
4
|
+
data.tar.gz: 1bc87a2a366c478f06409997050a2a30bc0a2f82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2ae1597da2a534678f9a55362445f75efeafd06f9d1d41b8bbd8b57b24b314c7fdac92915ad58580456569a71bdc238f5c30678c2a4b76ffc98a4cc686b19482
|
7
|
+
data.tar.gz: ad3d8fd3a41908cc65e22349690cc0188c28132c4dfcf68a02f9b17ea7d1d5f418da4f449c85c5d07bb1864b031c4802d0ed1bcc2d3da9183b9ff5310ca5f1fd
|
data/Gemfile.lock
CHANGED
data/lib/blobject.rb
CHANGED
@@ -11,8 +11,7 @@ class Blobject
|
|
11
11
|
|
12
12
|
# pass an optional hash of values to preload
|
13
13
|
# you can also pass a block, the new Blobject will be yield
|
14
|
-
def initialize hash
|
15
|
-
|
14
|
+
def initialize hash={}
|
16
15
|
@hash = Hash.new
|
17
16
|
|
18
17
|
hash.each do |key, value|
|
@@ -34,7 +33,6 @@ class Blobject
|
|
34
33
|
|
35
34
|
# delegates to the internal Hash
|
36
35
|
def inspect
|
37
|
-
|
38
36
|
@hash.inspect
|
39
37
|
end
|
40
38
|
|
@@ -45,7 +43,6 @@ class Blobject
|
|
45
43
|
|
46
44
|
# creates a recursive copy of the internal hash
|
47
45
|
def to_hash
|
48
|
-
|
49
46
|
h = hash.dup
|
50
47
|
@hash.each do |name, node|
|
51
48
|
h[name] = node.to_hash if node.respond_to? :to_hash
|
@@ -56,7 +53,6 @@ class Blobject
|
|
56
53
|
# method_missing is only called the first time an attribute is used. successive calls use
|
57
54
|
# memoized getters, setters and checkers
|
58
55
|
def method_missing method, *params, &block
|
59
|
-
|
60
56
|
__tag_and_raise__ NoMethodError.new(method) if ProhibitedNames.include?(method)
|
61
57
|
|
62
58
|
case
|
@@ -115,22 +111,14 @@ class Blobject
|
|
115
111
|
|
116
112
|
# hash-like access to the Blobject's attributes
|
117
113
|
def [] name
|
118
|
-
|
119
114
|
send name
|
120
115
|
end
|
121
116
|
|
122
117
|
# hash-like attribute setter
|
123
118
|
def []= name, value
|
124
|
-
|
125
119
|
send "#{name.to_s}=", value
|
126
120
|
end
|
127
121
|
|
128
|
-
# freeze a Blobject to prevent it being modified
|
129
|
-
def freeze
|
130
|
-
self.class.send(:__freeze_r__, @hash) unless frozen?
|
131
|
-
super
|
132
|
-
end
|
133
|
-
|
134
122
|
# for rails: `render json: blobject`
|
135
123
|
def as_json *args
|
136
124
|
return hash.as_json(*args) if hash.respond_to? :as_json
|
@@ -144,19 +132,16 @@ class Blobject
|
|
144
132
|
|
145
133
|
# serialize the Blobject as a yaml string
|
146
134
|
def to_yaml
|
147
|
-
|
148
135
|
as_yaml.to_yaml
|
149
136
|
end
|
150
137
|
|
151
138
|
# get a Blobject from a json string, if the yaml string describes an array, an array will be returned
|
152
139
|
def self.from_json json
|
153
|
-
|
154
140
|
__blobjectify__(JSON.parse(json))
|
155
141
|
end
|
156
142
|
|
157
143
|
# get a Blobject from a yaml string, if the yaml string describes an array, an array will be returned
|
158
144
|
def self.from_yaml yaml
|
159
|
-
|
160
145
|
__blobjectify__(YAML.load(yaml))
|
161
146
|
end
|
162
147
|
|
@@ -189,21 +174,7 @@ class Blobject
|
|
189
174
|
|
190
175
|
protected
|
191
176
|
|
192
|
-
def __freeze_r__ object
|
193
|
-
|
194
|
-
if object.respond_to?(:each) && object.each.is_a?(Enumerator)
|
195
|
-
values = object.is_a?(Hash) ? object.values : object
|
196
|
-
values.each do |v|
|
197
|
-
v.freeze
|
198
|
-
__freeze_r__(v)
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
object.freeze
|
203
|
-
end
|
204
|
-
|
205
177
|
def __blobjectify__ object
|
206
|
-
|
207
178
|
array = object if object.is_a? Array
|
208
179
|
hash = object if object.is_a? Hash
|
209
180
|
|
@@ -213,7 +184,6 @@ class Blobject
|
|
213
184
|
end
|
214
185
|
|
215
186
|
def __define_attribute__ name
|
216
|
-
|
217
187
|
__tag_and_raise__ NameError.new("invalid attribute name #{name}") unless name =~ /^\w+$/
|
218
188
|
name = name.to_sym
|
219
189
|
|
data/lib/blobject/version.rb
CHANGED
data/spec/blobject_spec.rb
CHANGED
@@ -278,51 +278,4 @@ describe Blobject do
|
|
278
278
|
end
|
279
279
|
end
|
280
280
|
end
|
281
|
-
|
282
|
-
describe 'freeze' do
|
283
|
-
before :each do
|
284
|
-
list_element = Blobject.new
|
285
|
-
|
286
|
-
b.name.first = 'barry'
|
287
|
-
b.data.list = [1, 2, 3, list_element]
|
288
|
-
b.data.inner_hash = {:inner => {:one => 1}}
|
289
|
-
|
290
|
-
b.freeze
|
291
|
-
end
|
292
|
-
|
293
|
-
it 'freezes the internal hash' do
|
294
|
-
b.freeze
|
295
|
-
b.hash.must_be :frozen?
|
296
|
-
end
|
297
|
-
|
298
|
-
it 'still provides access' do
|
299
|
-
refute_nil b.name.first
|
300
|
-
end
|
301
|
-
|
302
|
-
it 'freezes the internal hash' do
|
303
|
-
assert b.hash.frozen?
|
304
|
-
end
|
305
|
-
|
306
|
-
it 'allows access to existing attributes' do
|
307
|
-
assert_equal b.name.first, 'barry'
|
308
|
-
end
|
309
|
-
|
310
|
-
it 'recursively freezes nested Blobjects' do
|
311
|
-
assert b.frozen?
|
312
|
-
assert b.name.frozen?
|
313
|
-
assert b.data.list[3].frozen?
|
314
|
-
assert b.data.inner_hash.frozen?
|
315
|
-
end
|
316
|
-
|
317
|
-
it 'raises an error when trying to set an attribute' do
|
318
|
-
proc { b.hello = 123 }.must_raise RuntimeError
|
319
|
-
end
|
320
|
-
|
321
|
-
it 'still returns a blobject when trying to get an attribute' do
|
322
|
-
b.meow_face.must_be_instance_of Blobject
|
323
|
-
# check again to test memoized method
|
324
|
-
b.meow_face.must_be_instance_of Blobject
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
281
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: blobject
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Taylor
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-06-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|