bogo 0.1.2 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +45 -0
- data/lib/bogo.rb +1 -0
- data/lib/bogo/constants.rb +43 -0
- data/lib/bogo/smash.rb +27 -3
- data/lib/bogo/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 495624d0b24d9c6511abe8044be96c32506c473d
|
4
|
+
data.tar.gz: b3e80eeaf01de11a50e34c723ff574affdcba142
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3e57145c2f42b320bb64b2324ad407ad3362e98038d17bf28f4764432c0da0679fc456bcd2cd4cde994db89a5358c7749b611d8788a01542f413b52076a5abc1
|
7
|
+
data.tar.gz: d1109277e24b9ac77f74366c925f8136755371f321bbd04bb427ce13f3d4dfe5f31275575ecc793f32640c4fc4f633097828389105daeb8509c99de9b679c4a1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -109,6 +109,50 @@ puts Smash.new(:a => 1, :b => 2, :c => 3).checksum
|
|
109
109
|
puts Smash.new(:c => 3, :b => 2, :a => 1).checksum
|
110
110
|
```
|
111
111
|
|
112
|
+
### Freezing
|
113
|
+
|
114
|
+
Freeze entire smash structure:
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
require 'bogo'
|
118
|
+
|
119
|
+
inst = {:a => 'hi', :b => {:c => 'bye'}}.to_smash(:freeze)
|
120
|
+
puts inst[:a].frozen? 'Frozen' : 'Thawed'
|
121
|
+
puts inst[:b].frozen? 'Frozen' : 'Thawed'
|
122
|
+
puts inst[:b][:c].frozen? 'Frozen' : 'Thawed'
|
123
|
+
```
|
124
|
+
|
125
|
+
### Arrays
|
126
|
+
|
127
|
+
The `#to_smash` helper is also attached to `Array` to
|
128
|
+
fully convert an Arrays internals to Smashes
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
require 'bogo'
|
132
|
+
|
133
|
+
puts [{:a => 1}].to_smash.first.class.name
|
134
|
+
```
|
135
|
+
|
136
|
+
## Constants
|
137
|
+
|
138
|
+
Turn a string into a constant:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
require 'bogo'
|
142
|
+
|
143
|
+
const = Object.new.extend(Bogo::Constants)
|
144
|
+
p const.constantize('Bogo::Constants')
|
145
|
+
```
|
146
|
+
|
147
|
+
Get the namespace of a constant:
|
148
|
+
|
149
|
+
```ruby
|
150
|
+
require 'bogo'
|
151
|
+
|
152
|
+
const = Object.new.extend(Bogo::Constants)
|
153
|
+
p const.namespace('Bogo::Constants')
|
154
|
+
```
|
155
|
+
|
112
156
|
## Memoization
|
113
157
|
|
114
158
|
Memoization helpers for thread and object local, thread local,
|
@@ -150,6 +194,7 @@ puts obj2.object_value('fubar')
|
|
150
194
|
puts obj2.thread_value('fubar')
|
151
195
|
puts obj2.global_value('fubar')
|
152
196
|
puts '--- obj2 end'
|
197
|
+
```
|
153
198
|
|
154
199
|
## Lazy
|
155
200
|
|
data/lib/bogo.rb
CHANGED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'bogo'
|
2
|
+
|
3
|
+
module Bogo
|
4
|
+
|
5
|
+
# Constant helper
|
6
|
+
module Constants
|
7
|
+
|
8
|
+
# Convert string to constant
|
9
|
+
#
|
10
|
+
# @param string [String] full constant name
|
11
|
+
# @return [Object]
|
12
|
+
def constantize(string)
|
13
|
+
string.split('::').inject(ObjectSpace) do |memo, key|
|
14
|
+
break unless memo.const_defined?(key)
|
15
|
+
memo.const_get(key)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Return constant value localized to calling instance class
|
20
|
+
#
|
21
|
+
# @param name [String, Symbol] constant name
|
22
|
+
# @return [Object]
|
23
|
+
def const_val(name)
|
24
|
+
self.class.const_get(name)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Provides namespace constant
|
28
|
+
#
|
29
|
+
# @param inst [Object]
|
30
|
+
# @return [Class, Module]
|
31
|
+
def namespace(inst = self)
|
32
|
+
klass = inst.class.name.split('::')
|
33
|
+
klass.pop
|
34
|
+
if(klass.empty?)
|
35
|
+
ObjectSpace
|
36
|
+
else
|
37
|
+
constantize(klass.join('::'))
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
data/lib/bogo/smash.rb
CHANGED
@@ -112,7 +112,7 @@ class Hash
|
|
112
112
|
# @param convert_call [Symbol] builtin hash convert
|
113
113
|
# @return [Smash]
|
114
114
|
def to_type_converter(type, convert_call, *args)
|
115
|
-
type.new.tap do |smash|
|
115
|
+
result = type.new.tap do |smash|
|
116
116
|
if(args.include?(:sorted))
|
117
117
|
process = self.sort_by do |entry|
|
118
118
|
entry.first.to_s
|
@@ -124,6 +124,12 @@ class Hash
|
|
124
124
|
smash[k.is_a?(Symbol) ? k.to_s : k] = smash_conversion(v, convert_call, *args)
|
125
125
|
end
|
126
126
|
end
|
127
|
+
if(args.include?(:freeze))
|
128
|
+
result.values.map(&:freeze)
|
129
|
+
result.freeze
|
130
|
+
else
|
131
|
+
result
|
132
|
+
end
|
127
133
|
end
|
128
134
|
|
129
135
|
# Convert object to smash if applicable
|
@@ -137,10 +143,28 @@ class Hash
|
|
137
143
|
obj.send(convert_call, *args)
|
138
144
|
when Array
|
139
145
|
obj.map do |i|
|
140
|
-
smash_conversion(i, convert_call, *args)
|
146
|
+
result = smash_conversion(i, convert_call, *args)
|
147
|
+
args.include?(:freeze) ? result.freeze : result
|
141
148
|
end
|
142
149
|
else
|
143
|
-
obj
|
150
|
+
args.include?(:freeze) ? obj.freeze : obj
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
class Array
|
157
|
+
|
158
|
+
# Iterates searching for Hash types to auto convert
|
159
|
+
#
|
160
|
+
# @return [Array]
|
161
|
+
def to_smash(*args)
|
162
|
+
self.map do |item|
|
163
|
+
if(item.respond_to?(:to_smash))
|
164
|
+
item.to_smash(*args)
|
165
|
+
else
|
166
|
+
args.include?(:freeze) ? item.freeze : item
|
167
|
+
end
|
144
168
|
end
|
145
169
|
end
|
146
170
|
|
data/lib/bogo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bogo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Roberts
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: hashie
|
@@ -51,6 +51,7 @@ files:
|
|
51
51
|
- bogo.gemspec
|
52
52
|
- lib/bogo.rb
|
53
53
|
- lib/bogo/animal_strings.rb
|
54
|
+
- lib/bogo/constants.rb
|
54
55
|
- lib/bogo/lazy.rb
|
55
56
|
- lib/bogo/memoization.rb
|
56
57
|
- lib/bogo/smash.rb
|