hash_builder 0.1.0 → 0.2.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: 9d558232e333db81f54b2908babff87135a04524
4
- data.tar.gz: 9a7ab805dfed411cd2692581801583b55e2c4589
3
+ metadata.gz: faad6edf321e3f342176b49d7609e0571d97bb97
4
+ data.tar.gz: 6760b476808d2998613b9f19f79a0a95c0ce473f
5
5
  SHA512:
6
- metadata.gz: 855bc8140cfd594a54e0f9ac6bfcf601c5fc108e0821f2f0cb8d55285d078718e1de2235996037b8c2e04a66a156021edd7106ea93e6355020362929516a8791
7
- data.tar.gz: 261c27848fa2040eddde40c2beec7346c56ab35aee6d2bb0a4645f31c13f2c62d43817bcd6e0a78efe78db832f54b92f6fb49072f367b67ddd5d6db9a4b19267
6
+ metadata.gz: 70413930a07dce15f1a1ec8c3cfe7acc6b92176a23395bf3c65053c169e0cfee3ba94e760d84d7fc3fb091fae3928e5409e72a132b1895aefe87db4d2da5abcb
7
+ data.tar.gz: f18883d701e0b11d710a81aadc1567895fca6bec5c513b1ae2cd7658ca499eec7a0d091206220fae07ffd7070de9ef21e1466ad02d8504dfcf2ed3dae0207836
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- hash_builder (0.1.0)
4
+ hash_builder (0.2.0)
5
5
  exec_env
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -113,6 +113,30 @@ A request to `/hash/` returns the following JSON response
113
113
  }
114
114
  ```
115
115
 
116
+ In a rails view there is also a special syntax to create a top level array
117
+
118
+ ```ruby
119
+ array @users do |user|
120
+ name user[:name]
121
+ quote user[:quote]
122
+ end
123
+ ```
124
+
125
+ Would result in
126
+
127
+ ```json
128
+ [
129
+ {
130
+ "name": "CQQL",
131
+ "quote": "Emacs > Vim"
132
+ },
133
+ {
134
+ "name": "Joshua Bloch",
135
+ "quote": "The cleaner and nicer the program, the faster it's going to run. And if it doesn't, it'll be easy to make it fast."
136
+ }
137
+ ]
138
+ ```
139
+
116
140
  ## Performance
117
141
 
118
142
  There is a [benchmark script](./benchmark.rb), that returns the
@@ -10,19 +10,28 @@ module HashBuilder
10
10
  end
11
11
 
12
12
  def self.call (template)
13
+ # The template is on the first line, so that line numbers in the
14
+ # error stacks are correct.
13
15
  render_code = <<-RUBY
14
- HashBuilder.build(scope: self, locals: local_assigns) do
15
- #{template.source}
16
+ hash = HashBuilder.build(scope: self, locals: local_assigns) do #{template.source} end
17
+
18
+ if hash.size == 1 && hash.keys == [:array]
19
+ hash = hash[:array]
16
20
  end
21
+
22
+
17
23
  RUBY
24
+
18
25
  if !is_partial?(template)
19
26
  # ActiveModel defines #as_json in a way, that is not compatible
20
27
  # with JSON.
21
28
  if defined?(ActiveModel)
22
- render_code = "ActiveSupport::JSON.encode(#{render_code})"
29
+ render_code += "ActiveSupport::JSON.encode(hash)"
23
30
  else
24
- render_code = "JSON.generate(#{render_code})"
31
+ render_code += "JSON.generate(hash)"
25
32
  end
33
+ else
34
+ render_code += "hash"
26
35
  end
27
36
 
28
37
  render_code
@@ -1,3 +1,3 @@
1
1
  module HashBuilder
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -26,4 +26,13 @@ describe HashBuilder::Template do
26
26
 
27
27
  expect(hash).to eq({ foo: 1, bar: "test" })
28
28
  end
29
+
30
+ it "should render an array if the has only one top level key and it is :array" do
31
+ template = double("template", source: "array (0..3).to_a do |i|\n i * 2\nend", virtual_path: "test/_index")
32
+
33
+ code = HashBuilder::Template.call(template)
34
+ hash = view.instance_eval(code)
35
+
36
+ expect(hash).to eq([0, 2, 4, 6])
37
+ end
29
38
  end
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: 0.1.0
4
+ version: 0.2.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: 2013-11-27 00:00:00.000000000 Z
11
+ date: 2013-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
122
  version: '0'
123
123
  requirements: []
124
124
  rubyforge_project:
125
- rubygems_version: 2.1.5
125
+ rubygems_version: 2.1.4
126
126
  signing_key:
127
127
  specification_version: 4
128
128
  summary: The readme has examples