hash_builder 1.0.0 → 1.1.0

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: 0181ed3016674e142aa3efe8ff727194c394c3fe
4
- data.tar.gz: 0bcc8c1e899ea0696870307faa69f57227ab51af
3
+ metadata.gz: 67a080df4b9cd63c6e1e026072c9128fc6396289
4
+ data.tar.gz: 3c3c120bb626517d61512f320a817d7561bb042e
5
5
  SHA512:
6
- metadata.gz: 77a57e377b0adea23a01c092ad1c3f9bfc468a11086c211a037d079f298209f56aeb4bf95c7cb1d781910f89dd1dd7f6cd88221b1eb1ce2741b2dacec0e5b328
7
- data.tar.gz: 6381812932f6ced0ea0e07cba1fffdbf5138cd472083304ddb3d3c576a39591fdcee9c2ff3f7e6bedc8600552d94079e05d0f1e744cec5fc12a26c9264263a30
6
+ metadata.gz: da200597f2b50ca5d7104c1d4766e985669d88c082334e094adcfa5cecf46e62f83dddaee08f637c6051d763835af30fe26ed3be862fc29c645a56764403a833
7
+ data.tar.gz: 4dac864215e01026deb7f9823e5703aad9be3cfc61427915eee24d7814e2aab1ffddf7067653cef48cab65528ea4079bdccbf8b0777de9fe732cf8f7ae24723a
data/.gitignore CHANGED
@@ -3,3 +3,6 @@
3
3
 
4
4
  # Bundle config
5
5
  /.bundle
6
+
7
+ # Lock file
8
+ /Gemfile.lock
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.1.0
4
+
5
+ - Iterate over hashes as well, if a block is given. Previously hashes were not
6
+ iterated over, so that they could be used as values. But they can actually be
7
+ safely iterated over when a block is given.
8
+
3
9
  ## 1.0.0
4
10
 
5
11
  No changes. This release is just to mark the gem as stable. It has
data/README.md CHANGED
@@ -1,14 +1,14 @@
1
1
  # HashBuilder
2
2
 
3
3
  This gem allows you to build hashes in a way, that is totally copied from
4
- [json_builder](https://github.com/dewski/json_builder). I created this,
5
- because json_builder does not allow extraction of partials, which I rely
6
- heavily on, to keep my JSON generation DRY.
7
- And while I was at it, I found it a good idea to increase the abstraction
8
- level and build hashes instead of JSON because, this allows for easier
9
- manipulation of the results, application in more different circumstances
10
- and you can generate JSON and YAML with the well known `to_json` and
11
- `to_yaml` methods from it.
4
+ [json_builder](https://github.com/dewski/json_builder). I created this, because
5
+ json_builder does not allow extraction of partials, which I rely heavily on, to
6
+ keep my JSON generation DRY.
7
+
8
+ And while I was at it, I found it a good idea to increase the abstraction level
9
+ and build hashes instead of JSON because, this allows for easier manipulation of
10
+ the results, application in more different circumstances and you can generate
11
+ JSON and YAML with the well known `to_json` and `to_yaml` methods from it.
12
12
 
13
13
  ## Usage
14
14
 
data/lib/hash_builder.rb CHANGED
@@ -21,19 +21,19 @@ module HashBuilder
21
21
  env = ExecEnv::Env.new
22
22
  env.scope = scope
23
23
  env.locals = locals
24
-
24
+
25
25
  block_result = env.exec(*args, &block)
26
26
 
27
27
  messages = env.messages
28
28
 
29
29
  if messages.size > 0
30
30
  hash = {}
31
-
31
+
32
32
  messages.each do |(name, args, block)|
33
33
  arg = args.first
34
34
 
35
35
  # ActiveRecord relations respond to :map but are no Enumerable
36
- if args.size == 1 && block && arg.respond_to?(:map) && !arg.is_a?(Hash)
36
+ if args.size == 1 && block && arg.respond_to?(:map)
37
37
  hash[name] = arg.map do |*objects|
38
38
  HashBuilder.build(args: objects, scope: scope, locals: locals, &block)
39
39
  end
@@ -1,3 +1,3 @@
1
1
  module HashBuilder
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -21,7 +21,7 @@ describe HashBuilder do
21
21
  expect(hash).to eq({ level1: "Easy", container: { level2: "Harder" } })
22
22
  end
23
23
 
24
- it "should iterate over enumerables if arg and block is given" do
24
+ it "should iterate over enumerables if a block is given" do
25
25
  hash = HashBuilder.build do
26
26
  numbers (0..3).to_a do |num|
27
27
  i num
@@ -31,6 +31,17 @@ describe HashBuilder do
31
31
  expect(hash).to eq({ numbers: [{ i: 0 }, { i: 1 }, { i: 2 }, { i: 3 }] })
32
32
  end
33
33
 
34
+ it "should iterate over hashes if a block is given" do
35
+ hash = HashBuilder.build do
36
+ moods({ happy: 10, sad: 1 }) do |n, r|
37
+ name n
38
+ rating r
39
+ end
40
+ end
41
+
42
+ expect(hash).to eq({ moods: [{ name: :happy, rating: 10 }, { name: :sad, rating: 1 }] })
43
+ end
44
+
34
45
  it "should pass the environment into nested blocks" do
35
46
  scope = Object.new
36
47
  scope.instance_variable_set(:@id, 13)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hash_builder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Lienen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-28 00:00:00.000000000 Z
11
+ date: 2014-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -77,7 +77,6 @@ files:
77
77
  - ".rspec"
78
78
  - CHANGELOG.md
79
79
  - Gemfile
80
- - Gemfile.lock
81
80
  - LICENSE.txt
82
81
  - README.md
83
82
  - Rakefile
@@ -109,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
109
108
  version: '0'
110
109
  requirements: []
111
110
  rubyforge_project:
112
- rubygems_version: 2.2.0.rc.1
111
+ rubygems_version: 2.2.0
113
112
  signing_key:
114
113
  specification_version: 4
115
114
  summary: The readme has examples
data/Gemfile.lock DELETED
@@ -1,29 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- hash_builder (1.0.0)
5
- exec_env
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- diff-lcs (1.2.5)
11
- exec_env (0.1.0)
12
- rake (10.1.1)
13
- rspec (2.14.1)
14
- rspec-core (~> 2.14.0)
15
- rspec-expectations (~> 2.14.0)
16
- rspec-mocks (~> 2.14.0)
17
- rspec-core (2.14.7)
18
- rspec-expectations (2.14.4)
19
- diff-lcs (>= 1.1.3, < 2.0)
20
- rspec-mocks (2.14.4)
21
-
22
- PLATFORMS
23
- ruby
24
-
25
- DEPENDENCIES
26
- bundler (~> 1.4)
27
- hash_builder!
28
- rake (~> 10.1)
29
- rspec