Class: Ribbon::Wrapper

Inherits:
Object
  • Object
show all
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!

See Also:

Author:

  • Matheus Afonso Martins Moreira

Since:

  • 0.2.0

Class Method Summary collapse

Instance Method Summary collapse

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.

See Also:

Since:

  • 0.2.0



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.

Since:

  • 0.2.0



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.

Returns:

See Also:

Since:

  • 0.4.4



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.

Parameters:

Yield Parameters:

  • key

    the key which identifies both values

  • old_value

    the value from this wrapped Ribbon

  • new_value

    the value from the given ribbon

Yield Returns:

  • the object that will be used as the new value

Returns:

  • (Ribbon::Wrapper)

    a new wrapped ribbon containing the results of the merge

See Also:

Since:

  • 0.4.5



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.

Parameters:

Yield Parameters:

  • key

    the key which identifies both values

  • old_value

    the value from this wrapped Ribbon

  • new_value

    the value from the given ribbon

Yield Returns:

  • the object that will be used as the new value

Returns:

  • (self)

    this Ribbon::Wrapper instance

See Also:

Since:

  • 0.4.5



115
116
117
# File 'lib/ribbon/wrapper.rb', line 115

def deep_merge!(ribbon, &block)
  Ribbon.deep_merge! self, ribbon, &block
end

#internal_hashHash

The hash used by the wrapped Ribbon.

Returns:

  • (Hash)

    the internal hash of the Ribbon wrapped by this instance

Since:

  • 0.5.0



74
75
76
# File 'lib/ribbon/wrapper.rb', line 74

def internal_hash
  ribbon.__hash__
end

#ribbonRibbon

The wrapped Ribbon.

Returns:

  • (Ribbon)

    the ribbon wrapped by this instance

Since:

  • 0.2.0



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.

Parameters:

Returns:

  • (Ribbon)

    the wrapped Ribbon

Since:

  • 0.2.0



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_hashHash

Converts the wrapped Ribbon and all ribbons inside into hashes.

Returns:

  • (Hash)

    the converted contents of this wrapped Ribbon

Since:

  • 0.2.0



138
139
140
# File 'lib/ribbon/wrapper.rb', line 138

def to_hash
  to_hash_recursive
end

#to_sString

Delegates to the wrapped Ribbon.

Returns:

  • (String)

    the string representation of this Ribbon::Wrapper

See Also:

Since:

  • 0.2.0



154
155
156
# File 'lib/ribbon/wrapper.rb', line 154

def to_s
  ribbon.to_s
end

#to_yamlString

Converts the wrapped ribbon to a hash and serializes it with YAML.

Returns:

  • (String)

    the YAML string that represents this Ribbon

See Also:

Since:

  • 0.2.0



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.

Returns:

  • (Ribbon)

    the Ribbon wrapped by this instance

Since:

  • 0.3.0



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.

Returns:

  • (self)

    this Ribbon::Wrapper instance

Since:

  • 0.3.0



123
124
125
# File 'lib/ribbon/wrapper.rb', line 123

def wrap_all!
  wrap_all_recursive!
end