hash_map 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 94701f9bddaba8235a48d0340f4c900c0d73cf1a
4
- data.tar.gz: 827a696b1b3194e1b83857795772cc4e78c4d7b0
3
+ metadata.gz: eecd42833f732acc06ac6bb480e914a2e4410f79
4
+ data.tar.gz: 6a52704178d7f42c5dce703ded69da6dd1e8be7e
5
5
  SHA512:
6
- metadata.gz: 6507b7366edc76cb9f072c6eecab282a8abaf8e18759dbb77d54766f4546946eafee13491ba7b6137a74f54c289900ce7f1bb36ed38123b905a94a282926b216
7
- data.tar.gz: 8897e48ef5d0272feb79db75fee7390bda74ed9287139c226022a802b3b81757d33178349669e05b2b009db333bb77985a4eecb4af396be1f258d23fd02ebad4
6
+ metadata.gz: 49a7ed40a89d7d78064eefe4d1e91d8efa4ce9b69e18ad0e012f1794922edbcb0da005f1e7580c8a61ea91bc384d9cb3717efa0b48e2fdbaf2568faa03598610
7
+ data.tar.gz: fe30f661e0fe6b52bb0e7bfa8930d4f70509e72a45287d83f89a59d4a5fb1949b7a3c08996122823135d869ff7f87ede894f34219220a456868db60fdd2761a0
data/README.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # HashMap
2
+ [ ![Codeship Status for arturictus/hash_map](https://codeship.com/projects/5900adb0-7452-0133-84a1-52f3970f70f1/status?branch=master)](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
- **no from needed**
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
- **properties**
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
- **blocks:**
113
+ You can create your helpers in the mapper and call them inside the block
112
114
 
113
- If in **fron_child** block when you want to get the value with a block
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
@@ -1,4 +1,4 @@
1
- require "hash_map/version"
1
+ require 'hash_map/version'
2
2
  require 'active_support/all'
3
3
  module HashMap
4
4
  def self.root
data/lib/hash_map/base.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module HashMap
2
- class Base < Struct.new(:original)
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.class.attributes)
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] = single_to_ary(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(key, opts = {}, &block)
76
+ def _nested(_key, _opts = {}, &block)
81
77
  klass = self.class.new
82
78
  klass.instance_exec(&block)
83
79
  klass.attributes.flatten
@@ -1,14 +1,14 @@
1
1
  module HashMap
2
2
  class Mapper
3
- attr_reader :original, :data_structure
4
- def initialize(original, data_structure)
3
+ attr_reader :original, :hash_map
4
+ def initialize(original, hash_map)
5
5
  @original = HashWithIndifferentAccess.new(original)
6
- @data_structure = data_structure
6
+ @hash_map = hash_map
7
7
  end
8
8
 
9
9
  def output
10
10
  new_hash = HashWithIndifferentAccess.new
11
- data_structure.each do |struc|
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
- execute_block(struct)
23
- elsif struct[:from]
24
- get_value_from_key(struct)
25
- end
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
- block.call(nested, original)
40
+ hash_map.instance_exec nested, original, &block
41
41
  else
42
- block.call(original, original)
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({ n => a })
48
+ HashWithIndifferentAccess.new(n => a)
49
49
  end
50
50
  end
51
51
 
52
52
  def nil_to_default(value, struct)
53
- value || struct[:default]
53
+ value.nil? ? struct[:default] : value
54
54
  end
55
55
  end
56
56
  end
@@ -1,3 +1,3 @@
1
1
  module HashMap
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
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.0
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-19 00:00:00.000000000 Z
11
+ date: 2015-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler