Class: Ribbon::Wrapper
- Inherits:
-
Object
- Object
- Ribbon::Wrapper
- Defined in:
- lib/ribbon/wrapper.rb
Overview
Wraps a Ribbon in order to provide general-purpose methods.
Ribbons are designed to use methods as hash keys. In order to maximize the number of possibilities, many useful methods, including methods from Object, were left out and included in this class instead.
You can use wrapped ribbons like an ordinary hash. Any undefined methods will be sent to the ribbon’s hash. If the hash doesn’t respond to the method, it will be sent to the ribbon itself.
wrapper = Ribbon::Wrapper.new
wrapper.a.b.c
=> {}
wrapper.keys
=> [:a]
Keep in mind that nested ribbons may or may not be wrapped:
wrapper.a.b.c.keys
=> {}
wrapper
=> {a: {b: {c: {keys: {}}}}}
You can wrap and unwrap all ribbons inside:
wrapper.wrap_all!
wrapper.unwrap_all!
Class Method Summary collapse
-
.from_yaml(string) ⇒ Ribbon::Wrapper
Deserializes the hash from the
string
using YAML and uses it to construct a new wrapped ribbon.
Instance Method Summary collapse
-
#deep_merge(ribbon) {|key, old_value, new_value| ... } ⇒ Ribbon::Wrapper
Merges the contents of this wrapped Ribbon with the contents of the given Ribbon into a new Ribbon::Wrapper instance.
-
#deep_merge!(ribbon) {|key, old_value, new_value| ... } ⇒ self
Merges this wrapped Ribbon with the given Ribbon.
-
#initialize(ribbon = Ribbon.new, &block) ⇒ Wrapper
constructor
Wraps the given Ribbon, another Wrapper’s Ribbon or a hash.
-
#internal_hash ⇒ Hash
The hash used by the wrapped Ribbon.
-
#method_missing(method, *args, &block) ⇒ Object
Forwards the method, arguments and block to the wrapped Ribbon’s hash, if it responds to the method, or to the ribbon itself otherwise.
-
#ribbon ⇒ Ribbon
The wrapped Ribbon.
-
#ribbon=(ribbon) ⇒ Ribbon
Wraps a Ribbon, another Wrapper’s Ribbon or a hash.
-
#to_hash ⇒ Hash
Converts the wrapped Ribbon and all ribbons inside into hashes.
-
#to_s ⇒ String
Delegates to the wrapped Ribbon.
-
#to_yaml ⇒ String
Converts the wrapped ribbon to a hash and serializes it with YAML.
-
#unwrap_all! ⇒ Ribbon
Unwraps all ribbons contained by this wrapper’s ribbon.
-
#wrap_all! ⇒ self
Wraps all ribbons contained by this wrapper’s ribbon.
Constructor Details
#initialize(ribbon = Ribbon.new, &block) ⇒ Wrapper
Wraps the given Ribbon, another Wrapper’s Ribbon or a hash.
If given a block, the wrapper will be yielded to it. If the block doesn’t take any arguments, it will be evaluated in the context of the wrapper.
65 66 67 68 |
# File 'lib/ribbon/wrapper.rb', line 65 def initialize(ribbon = Ribbon.new, &block) self.ribbon = ribbon __yield_or_eval__ &block end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Forwards the method, arguments and block to the wrapped Ribbon’s hash, if it responds to the method, or to the ribbon itself otherwise.
80 81 82 83 |
# File 'lib/ribbon/wrapper.rb', line 80 def method_missing(method, *args, &block) if (hash = internal_hash).respond_to? method then hash else ribbon end.__send__ method, *args, &block end |
Class Method Details
.from_yaml(string) ⇒ Ribbon::Wrapper
Deserializes the hash from the string
using YAML and uses it to construct a new wrapped ribbon.
219 220 221 |
# File 'lib/ribbon/wrapper.rb', line 219 def from_yaml(string) ::Ribbon::Wrapper.new YAML.load(string) end |
Instance Method Details
#deep_merge(ribbon) {|key, old_value, new_value| ... } ⇒ Ribbon::Wrapper
Merges the contents of this wrapped Ribbon with the contents of the given Ribbon into a new Ribbon::Wrapper instance.
99 100 101 |
# File 'lib/ribbon/wrapper.rb', line 99 def deep_merge(ribbon, &block) Ribbon.wrap Ribbon.deep_merge(self, ribbon, &block) end |
#deep_merge!(ribbon) {|key, old_value, new_value| ... } ⇒ self
Merges this wrapped Ribbon with the given Ribbon.
115 116 117 |
# File 'lib/ribbon/wrapper.rb', line 115 def deep_merge!(ribbon, &block) Ribbon.deep_merge! self, ribbon, &block end |
#internal_hash ⇒ Hash
The hash used by the wrapped Ribbon.
74 75 76 |
# File 'lib/ribbon/wrapper.rb', line 74 def internal_hash ribbon.__hash__ end |
#ribbon ⇒ Ribbon
The wrapped Ribbon.
41 42 43 |
# File 'lib/ribbon/wrapper.rb', line 41 def ribbon @ribbon ||= Ribbon.new end |
#ribbon=(ribbon) ⇒ Ribbon
Wraps a Ribbon, another Wrapper’s Ribbon or a hash.
50 51 52 53 54 55 56 |
# File 'lib/ribbon/wrapper.rb', line 50 def ribbon=(ribbon) @ribbon = case ribbon when Wrapper then ribbon.ribbon when Ribbon then ribbon else Ribbon.new ribbon.to_hash end end |
#to_hash ⇒ Hash
Converts the wrapped Ribbon and all ribbons inside into hashes.
138 139 140 |
# File 'lib/ribbon/wrapper.rb', line 138 def to_hash to_hash_recursive end |
#to_s ⇒ String
Delegates to the wrapped Ribbon.
154 155 156 |
# File 'lib/ribbon/wrapper.rb', line 154 def to_s ribbon.to_s end |
#to_yaml ⇒ String
Converts the wrapped ribbon to a hash and serializes it with YAML.
146 147 148 |
# File 'lib/ribbon/wrapper.rb', line 146 def to_yaml to_hash.to_yaml end |
#unwrap_all! ⇒ Ribbon
Unwraps all ribbons contained by this wrapper’s ribbon.
131 132 133 |
# File 'lib/ribbon/wrapper.rb', line 131 def unwrap_all! unwrap_all_recursive! end |
#wrap_all! ⇒ self
Wraps all ribbons contained by this wrapper’s ribbon.
123 124 125 |
# File 'lib/ribbon/wrapper.rb', line 123 def wrap_all! wrap_all_recursive! end |