hash_map 0.3.0 → 0.3.1
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.
- checksums.yaml +4 -4
- data/README.md +26 -4
- data/lib/hash_map.rb +1 -1
- data/lib/hash_map/base.rb +2 -2
- data/lib/hash_map/dsl.rb +2 -6
- data/lib/hash_map/mapper.rb +12 -12
- data/lib/hash_map/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eecd42833f732acc06ac6bb480e914a2e4410f79
|
4
|
+
data.tar.gz: 6a52704178d7f42c5dce703ded69da6dd1e8be7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 49a7ed40a89d7d78064eefe4d1e91d8efa4ce9b69e18ad0e012f1794922edbcb0da005f1e7580c8a61ea91bc384d9cb3717efa0b48e2fdbaf2568faa03598610
|
7
|
+
data.tar.gz: fe30f661e0fe6b52bb0e7bfa8930d4f70509e72a45287d83f89a59d4a5fb1949b7a3c08996122823135d869ff7f87ede894f34219220a456868db60fdd2761a0
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# HashMap
|
2
|
+
[ ](https://codeship.com/projects/117597)
|
2
3
|
|
3
4
|
HashMap is a small library that allow you to map hashes with style :).
|
4
5
|
It will remove from your code many of the ugly navigation inside hashes to
|
@@ -93,7 +94,7 @@ ProfileMapper.map(original)
|
|
93
94
|
Enjoy!
|
94
95
|
|
95
96
|
### Examples:
|
96
|
-
**
|
97
|
+
**No 'from' key needed:**
|
97
98
|
```ruby
|
98
99
|
class Clever < HashMap::Base
|
99
100
|
property :name # will get value from the key 'name'
|
@@ -101,16 +102,35 @@ class Clever < HashMap::Base
|
|
101
102
|
end
|
102
103
|
```
|
103
104
|
|
104
|
-
**
|
105
|
+
**Properties:**
|
105
106
|
```ruby
|
106
107
|
class Properties < HashMap::Base
|
107
108
|
properties :name, :address, :house
|
108
109
|
end
|
109
110
|
```
|
111
|
+
**Methods:**
|
110
112
|
|
111
|
-
|
113
|
+
You can create your helpers in the mapper and call them inside the block
|
112
114
|
|
113
|
-
|
115
|
+
```ruby
|
116
|
+
class Methods < HashMap::Base
|
117
|
+
property(:common_names) { names }
|
118
|
+
property(:date) { |original| parse_date original[:date] }
|
119
|
+
property(:class_name) { self.class.name } #=> "Methods"
|
120
|
+
|
121
|
+
def names
|
122
|
+
%w(John Morty)
|
123
|
+
end
|
124
|
+
|
125
|
+
def parse_date(date)
|
126
|
+
date.strftime('%H:%M')
|
127
|
+
end
|
128
|
+
end
|
129
|
+
```
|
130
|
+
|
131
|
+
**Blocks:**
|
132
|
+
|
133
|
+
In **from_child** block when you want to get the value with a block
|
114
134
|
the value of the child and original will be yielded in this order: child, original
|
115
135
|
|
116
136
|
```ruby
|
@@ -148,6 +168,8 @@ Blocks.map(hash)
|
|
148
168
|
|
149
169
|
```
|
150
170
|
|
171
|
+
|
172
|
+
|
151
173
|
### Motivation
|
152
174
|
I got bored of doing this:
|
153
175
|
```ruby
|
data/lib/hash_map.rb
CHANGED
data/lib/hash_map/base.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
module HashMap
|
2
|
-
|
2
|
+
Base = Struct.new(:original) do
|
3
3
|
include ToDSL
|
4
4
|
delegate :[], to: :output
|
5
5
|
|
@@ -8,7 +8,7 @@ module HashMap
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def mapper
|
11
|
-
@mapper ||= Mapper.new(original, self
|
11
|
+
@mapper ||= Mapper.new(original, self)
|
12
12
|
end
|
13
13
|
|
14
14
|
def output
|
data/lib/hash_map/dsl.rb
CHANGED
@@ -27,12 +27,8 @@ module HashMap
|
|
27
27
|
@attributes = []
|
28
28
|
end
|
29
29
|
|
30
|
-
def attributes
|
31
|
-
@attributes
|
32
|
-
end
|
33
|
-
|
34
30
|
def property(key, opts = {}, &block)
|
35
|
-
new_hash = {}.tap{ |h| h[:key] =
|
31
|
+
new_hash = {}.tap { |h| h[:key] = single_to_ary(key) }
|
36
32
|
new_hash[:proc] = block if block
|
37
33
|
new_hash[:from] = generate_from(new_hash, opts)
|
38
34
|
attributes << new_hash.merge!(opts.except(:from))
|
@@ -77,7 +73,7 @@ module HashMap
|
|
77
73
|
from ? single_to_ary(from) : hash[:key].dup
|
78
74
|
end
|
79
75
|
|
80
|
-
def _nested(
|
76
|
+
def _nested(_key, _opts = {}, &block)
|
81
77
|
klass = self.class.new
|
82
78
|
klass.instance_exec(&block)
|
83
79
|
klass.attributes.flatten
|
data/lib/hash_map/mapper.rb
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
module HashMap
|
2
2
|
class Mapper
|
3
|
-
attr_reader :original, :
|
4
|
-
def initialize(original,
|
3
|
+
attr_reader :original, :hash_map
|
4
|
+
def initialize(original, hash_map)
|
5
5
|
@original = HashWithIndifferentAccess.new(original)
|
6
|
-
@
|
6
|
+
@hash_map = hash_map
|
7
7
|
end
|
8
8
|
|
9
9
|
def output
|
10
10
|
new_hash = HashWithIndifferentAccess.new
|
11
|
-
|
11
|
+
hash_map.class.attributes.each do |struc|
|
12
12
|
value = get_value(struc)
|
13
13
|
new_hash.deep_merge! build_keys(struc[:key], value)
|
14
14
|
end
|
@@ -19,10 +19,10 @@ module HashMap
|
|
19
19
|
|
20
20
|
def get_value(struct)
|
21
21
|
value = if struct[:proc]
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
execute_block(struct)
|
23
|
+
elsif struct[:from]
|
24
|
+
get_value_from_key(struct)
|
25
|
+
end
|
26
26
|
nil_to_default(value, struct)
|
27
27
|
end
|
28
28
|
|
@@ -37,20 +37,20 @@ module HashMap
|
|
37
37
|
block = struct[:proc]
|
38
38
|
if struct[:from_child]
|
39
39
|
nested = get_value_from_key(struct, :from_child)
|
40
|
-
|
40
|
+
hash_map.instance_exec nested, original, &block
|
41
41
|
else
|
42
|
-
|
42
|
+
hash_map.instance_exec original, original, &block
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
46
|
def build_keys(ary, value)
|
47
47
|
ary.reverse.inject(value) do |a, n|
|
48
|
-
HashWithIndifferentAccess.new(
|
48
|
+
HashWithIndifferentAccess.new(n => a)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
52
52
|
def nil_to_default(value, struct)
|
53
|
-
value
|
53
|
+
value.nil? ? struct[:default] : value
|
54
54
|
end
|
55
55
|
end
|
56
56
|
end
|
data/lib/hash_map/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Pañach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|