ribbon 0.4.6 → 0.4.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.markdown +15 -5
- data/lib/ribbon.rb +8 -1
- data/lib/ribbon/core_ext/array.rb +7 -4
- data/lib/ribbon/version.rb +1 -1
- data/lib/ribbon/wrapper.rb +24 -22
- metadata +5 -5
data/README.markdown
CHANGED
@@ -11,8 +11,9 @@ and natural to use. It allows the dynamic definition of arbitrary attributes,
|
|
11
11
|
which can easily be nested.
|
12
12
|
|
13
13
|
> r = Ribbon.new
|
14
|
-
> r.a.b.c = :d
|
15
14
|
=> {}
|
15
|
+
> r.a.b.c = :d
|
16
|
+
=> :d
|
16
17
|
> r
|
17
18
|
=> {a: {b: {c: :d}}}
|
18
19
|
|
@@ -32,7 +33,7 @@ You can also set the property if you give an argument to the method.
|
|
32
33
|
|
33
34
|
If you give it a block, the value of the option will be yielded to it.
|
34
35
|
|
35
|
-
> Ribbon.new
|
36
|
+
> Ribbon.new do |config|
|
36
37
|
config.music do |music|
|
37
38
|
music.file do |file|
|
38
39
|
file.extensions %w(flac mp3 ogg wma)
|
@@ -44,7 +45,7 @@ If you give it a block, the value of the option will be yielded to it.
|
|
44
45
|
If the block takes no arguments (arity of zero), it will be evaluated in the
|
45
46
|
context of the value instance. The above example could be rewritten as:
|
46
47
|
|
47
|
-
> Ribbon.new
|
48
|
+
> Ribbon.new do
|
48
49
|
config.music do
|
49
50
|
file do
|
50
51
|
extensions %w(flac mp3 ogg wma)
|
@@ -62,6 +63,15 @@ be created and stored in its place.
|
|
62
63
|
> r
|
63
64
|
=> {}
|
64
65
|
|
66
|
+
You may also provide a return value or a block:
|
67
|
+
|
68
|
+
> r.z? :no_value
|
69
|
+
=> :no_value
|
70
|
+
> r.z? { :value_from_block }
|
71
|
+
=> :value_from_block
|
72
|
+
> r.z? { raise 'Value not found' }
|
73
|
+
=> RuntimeError: Value not found
|
74
|
+
|
65
75
|
If you append a `!` to the name of the property and give it an argument, the
|
66
76
|
value of the property will be set to it and the receiver will be returned,
|
67
77
|
allowing you to chain multiple assignments in a single line.
|
@@ -78,8 +88,8 @@ They work just like the regular method calls, which means you can chain them.
|
|
78
88
|
### Ribbon Wrappers
|
79
89
|
|
80
90
|
Since Ribbons inherit from BasicObject, they don't include many general-purpose
|
81
|
-
methods. In order to solve that problem, `Ribbon::Wrapper` is provided.
|
82
|
-
wrapped
|
91
|
+
methods. In order to solve that problem, `Ribbon::Wrapper` is provided. You can
|
92
|
+
treat wrapped ribbons as if it were ordinary hashes.
|
83
93
|
|
84
94
|
> w = Ribbon::Wrapper.new
|
85
95
|
> w[:x]
|
data/lib/ribbon.rb
CHANGED
@@ -62,7 +62,8 @@ class Ribbon < BasicObject
|
|
62
62
|
# Initializes the new ribbon, merging the internal hash with the given one and
|
63
63
|
# converting all internal objects. See Ribbon::convert_all! for details.
|
64
64
|
def initialize(hash = {}, &block)
|
65
|
-
__hash__.merge! hash
|
65
|
+
__hash__.merge! hash
|
66
|
+
if block.arity.zero? then instance_eval &block else block.call self end if block
|
66
67
|
::Ribbon.convert_all! self
|
67
68
|
end
|
68
69
|
|
@@ -211,6 +212,12 @@ class Ribbon < BasicObject
|
|
211
212
|
end
|
212
213
|
end
|
213
214
|
|
215
|
+
# Deserializes the hash from the +string+ using YAML and uses it to
|
216
|
+
# construct a new ribbon.
|
217
|
+
def from_yaml(string)
|
218
|
+
Ribbon.new YAML.load(string)
|
219
|
+
end
|
220
|
+
|
214
221
|
# Wraps a ribbon instance in a Ribbon::Wrapper.
|
215
222
|
#
|
216
223
|
# Ribbon[ribbon].keys
|
@@ -10,9 +10,9 @@ class Ribbon < BasicObject
|
|
10
10
|
# otherwise returns an empty ribbon.
|
11
11
|
def extract_ribbon!
|
12
12
|
case last
|
13
|
-
when
|
14
|
-
when
|
15
|
-
when
|
13
|
+
when Hash then Ribbon.new pop
|
14
|
+
when Ribbon then pop
|
15
|
+
when Ribbon::Wrapper then pop.ribbon
|
16
16
|
else Ribbon.new
|
17
17
|
end
|
18
18
|
end
|
@@ -20,10 +20,13 @@ class Ribbon < BasicObject
|
|
20
20
|
# Extracts the last argument as a wrapped ribbon, or returns an empty one.
|
21
21
|
# See #extract_ribbon! for details.
|
22
22
|
def extract_wrapped_ribbon!
|
23
|
-
|
23
|
+
Ribbon.wrap extract_options_as_ribbon!
|
24
24
|
end
|
25
25
|
|
26
|
+
# Same as #extract_ribbon!
|
26
27
|
alias extract_options_as_ribbon! extract_ribbon!
|
28
|
+
|
29
|
+
# Same as #extract_wrapped_ribbon!
|
27
30
|
alias extract_options_as_wrapped_ribbon! extract_wrapped_ribbon!
|
28
31
|
|
29
32
|
end
|
data/lib/ribbon/version.rb
CHANGED
data/lib/ribbon/wrapper.rb
CHANGED
@@ -41,15 +41,6 @@ class Ribbon < BasicObject
|
|
41
41
|
# => 10
|
42
42
|
class Wrapper
|
43
43
|
|
44
|
-
class << self
|
45
|
-
|
46
|
-
# Wraps a Ribbon instance.
|
47
|
-
#
|
48
|
-
# Ribbon::Wrapper[ribbon]
|
49
|
-
alias [] new
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
44
|
# The wrapped Ribbon object.
|
54
45
|
attr :ribbon
|
55
46
|
|
@@ -67,8 +58,9 @@ class Ribbon < BasicObject
|
|
67
58
|
|
68
59
|
# Wraps a Ribbon object, providing many general-purpose methods that were
|
69
60
|
# not defined in the Ribbon itself.
|
70
|
-
def initialize(ribbon = Ribbon.new)
|
61
|
+
def initialize(ribbon = Ribbon.new, &block)
|
71
62
|
self.ribbon = ribbon
|
63
|
+
if block.arity.zero? then instance_eval &block else block.call self end if block
|
72
64
|
end
|
73
65
|
|
74
66
|
# Returns the hash of the wrapped ribbon.
|
@@ -84,11 +76,11 @@ class Ribbon < BasicObject
|
|
84
76
|
end
|
85
77
|
|
86
78
|
def deep_merge(ribbon)
|
87
|
-
|
79
|
+
Ribbon.deep_merge self, ribbon
|
88
80
|
end
|
89
81
|
|
90
82
|
def deep_merge!(ribbon)
|
91
|
-
|
83
|
+
Ribbon.deep_merge! self, ribbon
|
92
84
|
end
|
93
85
|
|
94
86
|
# Wraps all ribbons contained by this wrapper's ribbon.
|
@@ -123,6 +115,22 @@ class Ribbon < BasicObject
|
|
123
115
|
ribbon.to_s
|
124
116
|
end
|
125
117
|
|
118
|
+
# The class methods.
|
119
|
+
class << self
|
120
|
+
|
121
|
+
# Wraps a Ribbon instance.
|
122
|
+
#
|
123
|
+
# Ribbon::Wrapper[ribbon]
|
124
|
+
alias [] new
|
125
|
+
|
126
|
+
# Deserializes the hash from the +string+ using YAML and uses it to
|
127
|
+
# construct a new wrapped ribbon.
|
128
|
+
def from_yaml(string)
|
129
|
+
Ribbon::Wrapper.new YAML.load(string)
|
130
|
+
end
|
131
|
+
|
132
|
+
end
|
133
|
+
|
126
134
|
private
|
127
135
|
|
128
136
|
# Converts the wrapped ribbon and all ribbons inside into hashes using
|
@@ -132,8 +140,8 @@ class Ribbon < BasicObject
|
|
132
140
|
{}.tap do |hash|
|
133
141
|
ribbon.__hash__.each do |key, value|
|
134
142
|
hash[key] = case value
|
135
|
-
when
|
136
|
-
when
|
143
|
+
when Ribbon then to_hash_recursive value
|
144
|
+
when Ribbon::Wrapper then to_hash_recursive value.ribbon
|
137
145
|
else value
|
138
146
|
end
|
139
147
|
end
|
@@ -145,7 +153,7 @@ class Ribbon < BasicObject
|
|
145
153
|
def wrap_all_recursive!(wrapper = self)
|
146
154
|
wrapper.hash.each do |key, value|
|
147
155
|
wrapper.hash[key] = case value
|
148
|
-
when
|
156
|
+
when Ribbon then wrap_all_recursive! Ribbon::Wrapper[value]
|
149
157
|
else value
|
150
158
|
end
|
151
159
|
end
|
@@ -157,18 +165,12 @@ class Ribbon < BasicObject
|
|
157
165
|
def unwrap_all_recursive!(ribbon = self)
|
158
166
|
ribbon.__hash__.each do |key, value|
|
159
167
|
ribbon[key] = case value
|
160
|
-
when
|
168
|
+
when Ribbon::Wrapper then unwrap_all_recursive! value.ribbon
|
161
169
|
else value
|
162
170
|
end
|
163
171
|
end
|
164
172
|
ribbon
|
165
173
|
end
|
166
174
|
|
167
|
-
# Deserializes the hash from the +string+ using YAML and uses it to
|
168
|
-
# construct a new wrapped Ribbon.
|
169
|
-
def self.from_yaml(string)
|
170
|
-
::Ribbon::Wrapper.new YAML.load(string)
|
171
|
-
end
|
172
|
-
|
173
175
|
end
|
174
176
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ribbon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2012-01-16 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rookie
|
16
|
-
requirement: &
|
16
|
+
requirement: &15079400 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *15079400
|
25
25
|
description: Ruby Object Notation. Inspired by JSON and OpenStruct.
|
26
26
|
email: matheus.a.m.moreira@gmail.com
|
27
27
|
executables: []
|
@@ -55,7 +55,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
55
55
|
version: '0'
|
56
56
|
segments:
|
57
57
|
- 0
|
58
|
-
hash: -
|
58
|
+
hash: -3941054121051003876
|
59
59
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
60
|
none: false
|
61
61
|
requirements:
|
@@ -64,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
64
|
version: '0'
|
65
65
|
segments:
|
66
66
|
- 0
|
67
|
-
hash: -
|
67
|
+
hash: -3941054121051003876
|
68
68
|
requirements: []
|
69
69
|
rubyforge_project:
|
70
70
|
rubygems_version: 1.8.10
|