ribbon 0.0.5 → 0.1.0
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 +80 -5
- data/lib/ribbon/core_ext.rb +1 -1
- data/lib/ribbon/core_ext/hash.rb +3 -3
- data/lib/ribbon/methods.rb +15 -6
- data/lib/ribbon/version.rb +3 -3
- metadata +3 -4
- data/lib/ribbon/object.rb +0 -87
data/lib/ribbon.rb
CHANGED
@@ -1,13 +1,88 @@
|
|
1
1
|
require 'ribbon/methods'
|
2
|
+
require 'ribbon/version'
|
2
3
|
|
3
4
|
# Ruby Object Notation.
|
4
5
|
#
|
5
6
|
# Inspired by JSON and OpenStruct.
|
6
|
-
|
7
|
+
#
|
8
|
+
# Contains a hash whose keys that are symbols can be accessed via method calls.
|
9
|
+
# This is done via <tt>method_missing</tt>.
|
10
|
+
#
|
11
|
+
# In order to make room for as many method names as possible, Ribbon inherits
|
12
|
+
# from BasicObject and implements as many methods as possible at the class
|
13
|
+
# level.
|
14
|
+
class Ribbon < BasicObject
|
7
15
|
|
8
|
-
extend
|
16
|
+
extend Methods
|
9
17
|
|
10
|
-
|
18
|
+
# The internal Hash.
|
19
|
+
def __hash__
|
20
|
+
@hash ||= {}
|
21
|
+
end
|
11
22
|
|
12
|
-
|
13
|
-
|
23
|
+
# Merges the internal hash with the given one.
|
24
|
+
def initialize(hash = {}, &block)
|
25
|
+
__hash__.merge! hash, &block
|
26
|
+
::Ribbon.convert_all! self
|
27
|
+
end
|
28
|
+
|
29
|
+
# Gets a value by key.
|
30
|
+
def [](key)
|
31
|
+
__hash__[key]
|
32
|
+
end
|
33
|
+
|
34
|
+
# Sets a value by key.
|
35
|
+
def []=(key, value)
|
36
|
+
__hash__[key] = value
|
37
|
+
end
|
38
|
+
|
39
|
+
# Handles the following cases:
|
40
|
+
#
|
41
|
+
# ribbon.method = value => ribbon[method] = value
|
42
|
+
# ribbon.method! value => ribbon[method] = value
|
43
|
+
# ribbon.method? => ribbon[method] ? true : false
|
44
|
+
# ribbon.method => ribbon[method]
|
45
|
+
def method_missing(method, *args, &block)
|
46
|
+
m = method.to_s.strip.chop.strip.to_sym
|
47
|
+
case method.to_s[-1]
|
48
|
+
when '=', '!'
|
49
|
+
self[m] = args.first
|
50
|
+
when '?'
|
51
|
+
self[m] ? true : false
|
52
|
+
else
|
53
|
+
self[method] = if __hash__.has_key? method
|
54
|
+
::Ribbon.convert self[method]
|
55
|
+
else
|
56
|
+
::Ribbon.new
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
# Computes a simple key:value string for easy visualization.
|
62
|
+
#
|
63
|
+
# In +opts+ can be specified several options that customize how the string
|
64
|
+
# is generated. Among those options:
|
65
|
+
#
|
66
|
+
# [:separator] Used to separate a key/value pair. Default is <tt>': '</tt>.
|
67
|
+
# [:key] Symbol that will be sent to the key in order to obtain its
|
68
|
+
# string representation. Defaults to <tt>:to_s</tt>.
|
69
|
+
# [:value] Symbol that will be sent to the value in order to obtain its
|
70
|
+
# string representation. Defaults to <tt>:inspect</tt>.
|
71
|
+
#
|
72
|
+
# No matter what is given as the key or value of a
|
73
|
+
def to_s(opts = {})
|
74
|
+
ksym = opts.fetch(:key, :to_s).to_sym
|
75
|
+
vsym = opts.fetch(:value, :inspect).to_sym
|
76
|
+
separator = opts.fetch(:separator, ': ').to_s
|
77
|
+
values = ::Ribbon.map(self) do |k, v|
|
78
|
+
k = if ::Ribbon.instance? k then k.to_s opts else k.send ksym end
|
79
|
+
v = if ::Ribbon.instance? v then v.to_s opts else v.send vsym end
|
80
|
+
"#{k}#{separator}#{v}"
|
81
|
+
end.join ', '
|
82
|
+
"{Ribbon #{values}}"
|
83
|
+
end
|
84
|
+
|
85
|
+
# Same as #to_s.
|
86
|
+
alias :inspect :to_s
|
87
|
+
|
88
|
+
end
|
data/lib/ribbon/core_ext.rb
CHANGED
data/lib/ribbon/core_ext/hash.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require 'ribbon
|
1
|
+
require 'ribbon'
|
2
2
|
|
3
|
-
|
3
|
+
class Ribbon < BasicObject
|
4
4
|
module CoreExt
|
5
5
|
|
6
6
|
# Includes a method to convert hashes to ribbons.
|
@@ -8,7 +8,7 @@ module Ribbon
|
|
8
8
|
|
9
9
|
# Converts this hash to a Ribbon::Object.
|
10
10
|
def to_ribbon
|
11
|
-
|
11
|
+
Ribbon.new self
|
12
12
|
end
|
13
13
|
|
14
14
|
# Same as #to_ribbon.
|
data/lib/ribbon/methods.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class Ribbon < BasicObject
|
2
2
|
|
3
3
|
# Methods that operate on Ribbons. These should be included at the class level
|
4
4
|
# in order to keep as many names available for use with ribbons as possible.
|
@@ -33,7 +33,7 @@ module Ribbon
|
|
33
33
|
{}.tap do |hash|
|
34
34
|
each(ribbon) do |key, value|
|
35
35
|
hash[key] = case value
|
36
|
-
when
|
36
|
+
when Ribbon then to_hash value
|
37
37
|
else value
|
38
38
|
end
|
39
39
|
end
|
@@ -44,7 +44,7 @@ module Ribbon
|
|
44
44
|
# an Array, converts any hashes inside.
|
45
45
|
def convert(object)
|
46
46
|
case object
|
47
|
-
when ::Hash then
|
47
|
+
when ::Hash then Ribbon.new object
|
48
48
|
when ::Array then object.map { |element| convert element }
|
49
49
|
else object
|
50
50
|
end
|
@@ -54,7 +54,7 @@ module Ribbon
|
|
54
54
|
def convert_all!(ribbon)
|
55
55
|
each(ribbon) do |key, value|
|
56
56
|
ribbon[key] = case value
|
57
|
-
when
|
57
|
+
when Ribbon then convert_all! value
|
58
58
|
else convert value
|
59
59
|
end
|
60
60
|
end
|
@@ -63,11 +63,20 @@ module Ribbon
|
|
63
63
|
|
64
64
|
# Returns +true+ if the given +object+ is a Ribbon.
|
65
65
|
def instance?(object)
|
66
|
-
|
66
|
+
Ribbon === object
|
67
|
+
end
|
68
|
+
|
69
|
+
# Converts the ribbon to a hash and serializes it with YAML. To get a ribbon
|
70
|
+
# back from the serialized hash, you can simply load the hash and pass it to
|
71
|
+
# the Ribbon::Object constructor:
|
72
|
+
#
|
73
|
+
# ribbon = Ribbon::Object.new YAML.load(str)
|
74
|
+
def to_yaml(ribbon)
|
75
|
+
to_hash(ribbon).to_yaml
|
67
76
|
end
|
68
77
|
|
69
78
|
end
|
70
79
|
|
71
80
|
end
|
72
81
|
|
73
|
-
require 'ribbon
|
82
|
+
require 'ribbon'
|
data/lib/ribbon/version.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
class Ribbon < BasicObject
|
2
2
|
|
3
3
|
# Ribbon's version.
|
4
4
|
module Version
|
@@ -11,12 +11,12 @@ module Ribbon
|
|
11
11
|
# Minor version.
|
12
12
|
#
|
13
13
|
# Increments denote backward-compatible changes and additions.
|
14
|
-
MINOR =
|
14
|
+
MINOR = 1
|
15
15
|
|
16
16
|
# Patch version.
|
17
17
|
#
|
18
18
|
# Increments denote changes in implementation.
|
19
|
-
PATCH =
|
19
|
+
PATCH = 0
|
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
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ 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: &15229760 !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: *15229760
|
25
25
|
description: Ruby Object Notation. Inspired by JSON and OpenStruct.
|
26
26
|
email: matheus.a.m.moreira@gmail.com
|
27
27
|
executables: []
|
@@ -38,7 +38,6 @@ files:
|
|
38
38
|
- lib/ribbon/core_ext.rb
|
39
39
|
- lib/ribbon/core_ext/hash.rb
|
40
40
|
- lib/ribbon/methods.rb
|
41
|
-
- lib/ribbon/object.rb
|
42
41
|
- lib/ribbon/version.rb
|
43
42
|
- ribbon.gemspec
|
44
43
|
homepage: https://github.com/matheusmoreira/ribbon
|
data/lib/ribbon/object.rb
DELETED
@@ -1,87 +0,0 @@
|
|
1
|
-
require 'ribbon/methods'
|
2
|
-
|
3
|
-
module Ribbon
|
4
|
-
|
5
|
-
# Contains a hash whose keys that are symbols or strings can be accessed via
|
6
|
-
# method calls. This is done via <tt>method_missing</tt>.
|
7
|
-
#
|
8
|
-
# In order to make room for as many method names as possible, Ribbon::Object
|
9
|
-
# inherits from BasicObject and implements as many methods as possible at the
|
10
|
-
# class level.
|
11
|
-
class Object < BasicObject
|
12
|
-
|
13
|
-
extend Methods
|
14
|
-
|
15
|
-
# The internal Hash.
|
16
|
-
def __hash__
|
17
|
-
@hash ||= {}
|
18
|
-
end
|
19
|
-
|
20
|
-
# Merges the internal hash with the given one.
|
21
|
-
def initialize(hash = {}, &block)
|
22
|
-
__hash__.merge! hash, &block
|
23
|
-
Object.convert_all! self
|
24
|
-
end
|
25
|
-
|
26
|
-
# Gets a value by key.
|
27
|
-
def [](key)
|
28
|
-
__hash__[key]
|
29
|
-
end
|
30
|
-
|
31
|
-
# Sets a value by key.
|
32
|
-
def []=(key, value)
|
33
|
-
__hash__[key] = value
|
34
|
-
end
|
35
|
-
|
36
|
-
# Handles the following cases:
|
37
|
-
#
|
38
|
-
# ribbon.method = value => ribbon[method] = value
|
39
|
-
# ribbon.method! value => ribbon[method] = value
|
40
|
-
# ribbon.method? => ribbon[method] ? true : false
|
41
|
-
# ribbon.method => ribbon[method]
|
42
|
-
def method_missing(method, *args, &block)
|
43
|
-
m = method.to_s.strip.chop.strip.to_sym
|
44
|
-
case method.to_s[-1]
|
45
|
-
when '=', '!'
|
46
|
-
self[m] = args.first
|
47
|
-
when '?'
|
48
|
-
self[m] ? true : false
|
49
|
-
else
|
50
|
-
self[method] = if __hash__.has_key? method
|
51
|
-
Object.convert self[method]
|
52
|
-
else
|
53
|
-
Object.new
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# Computes a simple key:value string for easy visualization.
|
59
|
-
#
|
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}}"
|
80
|
-
end
|
81
|
-
|
82
|
-
# Same as #to_s.
|
83
|
-
alias :inspect :to_s
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
end
|