ribbon 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,10 +31,10 @@ module Ribbon
31
31
 
32
32
  # Handles the following cases:
33
33
  #
34
- # options.method = value => rbon[method] = value
35
- # options.method! value => rbon[method] = value
36
- # options.method? => rbon[method] ? true : false
37
- # options.method => rbon[method]
34
+ # ribbon.method = value => ribbon[method] = value
35
+ # ribbon.method! value => ribbon[method] = value
36
+ # ribbon.method? => ribbon[method] ? true : false
37
+ # ribbon.method => ribbon[method]
38
38
  def method_missing(method, *args, &block)
39
39
  m = method.to_s.strip.chop.strip.to_sym
40
40
  case method.to_s[-1]
@@ -59,7 +59,7 @@ module Ribbon
59
59
  def to_s(&block)
60
60
  values = __hash__.map do |key, value|
61
61
  value = value.to_s &block if ::Ribbon::Object === value
62
- block ? block.call(key, value) : "#{key}:#{value}"
62
+ block ? block.call(key, value) : "#{key.to_s}: #{value.inspect}"
63
63
  end
64
64
  "{ Ribbon #{values.join ', '} }"
65
65
  end
@@ -79,7 +79,7 @@ module Ribbon
79
79
 
80
80
  # Converts all values in the given ribbon.
81
81
  def self.convert_all!(ribbon)
82
- ribbon.__hash__.each do |key, value|
82
+ each(ribbon) do |key, value|
83
83
  ribbon[key] = case value
84
84
  when ::Ribbon::Object then convert_all! value
85
85
  else convert value
@@ -88,6 +88,35 @@ module Ribbon
88
88
  ribbon
89
89
  end
90
90
 
91
+ # Converts +ribbon+ and all Ribbons inside into hashes.
92
+ def self.to_hash(ribbon)
93
+ {}.tap do |hash|
94
+ each(ribbon) do |key, value|
95
+ hash[key] = case value
96
+ when ::Ribbon::Object then to_hash value
97
+ else value
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ # Merges +old+'s hash with +new+'s. This is equivalent to calling
104
+ # <tt>merge!</tt> on +old+'s hash and passing it +new+'s hash and the given
105
+ # block.
106
+ def self.merge!(old, new, &block)
107
+ old_hash, new_hash = old.__hash__, new.__hash__
108
+ old_hash.merge! new_hash, &block
109
+ end
110
+
111
+ # Returns the hash keys of the given ribbon.
112
+ def self.keys(ribbon)
113
+ ribbon.__hash__.keys
114
+ end
115
+
116
+ def self.each(ribbon, &block)
117
+ ribbon.__hash__.each &block
118
+ end
119
+
91
120
  end
92
121
 
93
122
  end
@@ -16,7 +16,7 @@ module Ribbon
16
16
  # Patch version.
17
17
  #
18
18
  # Increments denote changes in implementation.
19
- PATCH = 3
19
+ PATCH = 4
20
20
 
21
21
  # Build version.
22
22
  #
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.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-12-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rookie
16
- requirement: &21805340 !ruby/object:Gem::Requirement
16
+ requirement: &9983820 !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: *21805340
24
+ version_requirements: *9983820
25
25
  description: Ruby Object Notation. Inspired by JSON and OpenStruct.
26
26
  email: matheus.a.m.moreira@gmail.com
27
27
  executables: []