jsontoerb 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,16 +1,16 @@
1
1
  = jsontoerb
2
2
 
3
- Description goes here.
3
+ JsonToErb gem allows to render erb templates with json data with console command
4
4
 
5
- == Contributing to jsontoerb
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
5
+ After gem is installed you should be able to execute following command: 'jsontoerb <path_to_erb_template> <json_data>' and rendered template will be returned as output. One thing to be aware of: json key has to be quoted with ".
6
+
7
+ Example:
8
+ template.erb
9
+ <div>parmas['content']</div>
10
+ command:
11
+ jsontoerb template.erb {\"content\": \"Sweet!\"}
12
+ output:
13
+ <div>Sweet!</div>
14
14
 
15
15
  == Copyright
16
16
 
data/jsontoerb.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "jsontoerb"
8
- s.version = "0.0.1"
8
+ s.version = "0.0.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andriy Romanov"]
data/lib/jsontoerb.rb CHANGED
@@ -3,10 +3,11 @@ require 'json'
3
3
 
4
4
  class JsonToErb
5
5
 
6
- def self.render_template(template_content, json_string)
6
+ def self.render_template(template_content, json_string, css_map = {})
7
7
  template = Erubis::Eruby.new(template_content)
8
8
  params = JSON.parse(json_string)
9
- return template.result({:params => params})
9
+ css = lambda {|css_class| css_map[css_class] || css_class }
10
+ return template.result({:params => params, :css => css})
10
11
  end
11
12
 
12
13
  end
@@ -4,4 +4,14 @@ describe "Jsontoerb" do
4
4
  it "should render template from json" do
5
5
  JsonToErb.render_template("<div><%= params['name'] %></div>", "{\"name\": \"Name\"}").should == "<div>Name</div>"
6
6
  end
7
+
8
+ it "should render template with css class from json" do
9
+ JsonToErb.render_template("<div class='<%= css['cssClass']%>'><%= params['name'] %></div>", "{\"name\": \"Name\"}").should == "<div class='cssClass'>Name</div>"
10
+ end
11
+
12
+ it "should use css map while rendering template" do
13
+ template = "<div class='<%= css['cssClass']%>'><%= params['name'] %></div>"
14
+ JsonToErb.render_template(template, "{\"name\": \"Name\"}", {"cssClass" => "a"}).should == "<div class='a'>Name</div>"
15
+ end
16
+
7
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsontoerb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -118,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
118
  version: '0'
119
119
  requirements: []
120
120
  rubyforge_project:
121
- rubygems_version: 1.8.24
121
+ rubygems_version: 1.8.23
122
122
  signing_key:
123
123
  specification_version: 3
124
124
  summary: Gem to render erb template using json data