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 +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +24 -0
- data/lib/hash_builder/template.rb +13 -4
- data/lib/hash_builder/version.rb +1 -1
- data/spec/hash_builder/template_spec.rb +9 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: faad6edf321e3f342176b49d7609e0571d97bb97
|
4
|
+
data.tar.gz: 6760b476808d2998613b9f19f79a0a95c0ce473f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 70413930a07dce15f1a1ec8c3cfe7acc6b92176a23395bf3c65053c169e0cfee3ba94e760d84d7fc3fb091fae3928e5409e72a132b1895aefe87db4d2da5abcb
|
7
|
+
data.tar.gz: f18883d701e0b11d710a81aadc1567895fca6bec5c513b1ae2cd7658ca499eec7a0d091206220fae07ffd7070de9ef21e1466ad02d8504dfcf2ed3dae0207836
|
data/Gemfile.lock
CHANGED
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
|
-
|
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
|
29
|
+
render_code += "ActiveSupport::JSON.encode(hash)"
|
23
30
|
else
|
24
|
-
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
|
data/lib/hash_builder/version.rb
CHANGED
@@ -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.
|
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-
|
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.
|
125
|
+
rubygems_version: 2.1.4
|
126
126
|
signing_key:
|
127
127
|
specification_version: 4
|
128
128
|
summary: The readme has examples
|