ribbon 0.0.4 → 0.0.5
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/lib/ribbon.rb +5 -0
- data/lib/ribbon/core_ext.rb +2 -2
- data/lib/ribbon/methods.rb +73 -0
- data/lib/ribbon/object.rb +27 -62
- data/lib/ribbon/version.rb +1 -1
- metadata +5 -4
data/lib/ribbon.rb
CHANGED
data/lib/ribbon/core_ext.rb
CHANGED
@@ -0,0 +1,73 @@
|
|
1
|
+
module Ribbon
|
2
|
+
|
3
|
+
# Methods that operate on Ribbons. These should be included at the class level
|
4
|
+
# in order to keep as many names available for use with ribbons as possible.
|
5
|
+
module Methods
|
6
|
+
|
7
|
+
# Returns the hash keys of the given ribbon.
|
8
|
+
def keys(ribbon)
|
9
|
+
ribbon.__hash__.keys
|
10
|
+
end
|
11
|
+
|
12
|
+
# Yields a key, value pair to the given block.
|
13
|
+
def each(ribbon, &block)
|
14
|
+
ribbon.__hash__.each &block
|
15
|
+
end
|
16
|
+
|
17
|
+
# Yields a key, value pair to the given block and returns an array
|
18
|
+
# containing the values returned by the block on each iteration.
|
19
|
+
def map(ribbon, &block)
|
20
|
+
ribbon.__hash__.map &block
|
21
|
+
end
|
22
|
+
|
23
|
+
# Merges +old+'s hash with +new+'s. This is equivalent to calling
|
24
|
+
# <tt>merge!</tt> on +old+'s hash and passing it +new+'s hash and the given
|
25
|
+
# block.
|
26
|
+
def merge!(old, new, &block)
|
27
|
+
old_hash, new_hash = old.__hash__, new.__hash__
|
28
|
+
old_hash.merge! new_hash, &block
|
29
|
+
end
|
30
|
+
|
31
|
+
# Converts +ribbon+ and all Ribbons inside into hashes.
|
32
|
+
def to_hash(ribbon)
|
33
|
+
{}.tap do |hash|
|
34
|
+
each(ribbon) do |key, value|
|
35
|
+
hash[key] = case value
|
36
|
+
when ::Ribbon::Object then to_hash value
|
37
|
+
else value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# If <tt>object</tt> is a Hash, converts it to a Ribbon::Object. If it is
|
44
|
+
# an Array, converts any hashes inside.
|
45
|
+
def convert(object)
|
46
|
+
case object
|
47
|
+
when ::Hash then ::Ribbon::Object.new object
|
48
|
+
when ::Array then object.map { |element| convert element }
|
49
|
+
else object
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# Converts all values in the given ribbon.
|
54
|
+
def convert_all!(ribbon)
|
55
|
+
each(ribbon) do |key, value|
|
56
|
+
ribbon[key] = case value
|
57
|
+
when ::Ribbon::Object then convert_all! value
|
58
|
+
else convert value
|
59
|
+
end
|
60
|
+
end
|
61
|
+
ribbon
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns +true+ if the given +object+ is a Ribbon.
|
65
|
+
def instance?(object)
|
66
|
+
::Ribbon::Object === object
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
require 'ribbon/object'
|
data/lib/ribbon/object.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'ribbon/methods'
|
2
|
+
|
1
3
|
module Ribbon
|
2
4
|
|
3
5
|
# Contains a hash whose keys that are symbols or strings can be accessed via
|
@@ -8,6 +10,8 @@ module Ribbon
|
|
8
10
|
# class level.
|
9
11
|
class Object < BasicObject
|
10
12
|
|
13
|
+
extend Methods
|
14
|
+
|
11
15
|
# The internal Hash.
|
12
16
|
def __hash__
|
13
17
|
@hash ||= {}
|
@@ -16,7 +20,7 @@ module Ribbon
|
|
16
20
|
# Merges the internal hash with the given one.
|
17
21
|
def initialize(hash = {}, &block)
|
18
22
|
__hash__.merge! hash, &block
|
19
|
-
|
23
|
+
Object.convert_all! self
|
20
24
|
end
|
21
25
|
|
22
26
|
# Gets a value by key.
|
@@ -44,79 +48,40 @@ module Ribbon
|
|
44
48
|
self[m] ? true : false
|
45
49
|
else
|
46
50
|
self[method] = if __hash__.has_key? method
|
47
|
-
|
51
|
+
Object.convert self[method]
|
48
52
|
else
|
49
|
-
|
53
|
+
Object.new
|
50
54
|
end
|
51
55
|
end
|
52
56
|
end
|
53
57
|
|
54
58
|
# Computes a simple key:value string for easy visualization.
|
55
59
|
#
|
56
|
-
#
|
57
|
-
#
|
58
|
-
#
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
60
|
+
# In +opts+ can be specified several options that customize how the string
|
61
|
+
# is generated. Among those options:
|
62
|
+
#
|
63
|
+
# [:separator] Used to separate a key/value pair. Default is <tt>': '</tt>.
|
64
|
+
# [:key] Symbol that will be sent to the key in order to obtain its
|
65
|
+
# string representation. Defaults to <tt>:to_s</tt>.
|
66
|
+
# [:value] Symbol that will be sent to the value in order to obtain its
|
67
|
+
# string representation. Defaults to <tt>:inspect</tt>.
|
68
|
+
#
|
69
|
+
# No matter what is given as the key or value of a
|
70
|
+
def to_s(opts = {})
|
71
|
+
ksym = opts.fetch(:key, :to_s).to_sym
|
72
|
+
vsym = opts.fetch(:value, :inspect).to_sym
|
73
|
+
separator = opts.fetch(:separator, ': ').to_s
|
74
|
+
values = Object.map(self) do |k, v|
|
75
|
+
k = if Object.instance? k then k.to_s opts else k.send ksym end
|
76
|
+
v = if Object.instance? v then v.to_s opts else v.send vsym end
|
77
|
+
"#{k}#{separator}#{v}"
|
78
|
+
end.join ', '
|
79
|
+
"{Ribbon #{values}}"
|
65
80
|
end
|
66
81
|
|
67
82
|
# Same as #to_s.
|
68
83
|
alias :inspect :to_s
|
69
84
|
|
70
|
-
# If <tt>object</tt> is a Hash, converts it to a Ribbon::Object. If it is
|
71
|
-
# an Array, converts any hashes inside.
|
72
|
-
def self.convert(object)
|
73
|
-
case object
|
74
|
-
when ::Hash then self.new object
|
75
|
-
when ::Array then object.map { |element| convert element }
|
76
|
-
else object
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
# Converts all values in the given ribbon.
|
81
|
-
def self.convert_all!(ribbon)
|
82
|
-
each(ribbon) do |key, value|
|
83
|
-
ribbon[key] = case value
|
84
|
-
when ::Ribbon::Object then convert_all! value
|
85
|
-
else convert value
|
86
|
-
end
|
87
|
-
end
|
88
|
-
ribbon
|
89
|
-
end
|
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
|
-
|
120
85
|
end
|
121
86
|
|
122
87
|
end
|
data/lib/ribbon/version.rb
CHANGED
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.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-12-
|
12
|
+
date: 2011-12-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rookie
|
16
|
-
requirement: &
|
16
|
+
requirement: &21981980 !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: *21981980
|
25
25
|
description: Ruby Object Notation. Inspired by JSON and OpenStruct.
|
26
26
|
email: matheus.a.m.moreira@gmail.com
|
27
27
|
executables: []
|
@@ -37,6 +37,7 @@ files:
|
|
37
37
|
- lib/ribbon.rb
|
38
38
|
- lib/ribbon/core_ext.rb
|
39
39
|
- lib/ribbon/core_ext/hash.rb
|
40
|
+
- lib/ribbon/methods.rb
|
40
41
|
- lib/ribbon/object.rb
|
41
42
|
- lib/ribbon/version.rb
|
42
43
|
- ribbon.gemspec
|