hash_to_html 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hash_to_html.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Josh Crews
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # HashToHtml
2
+
3
+ Let's you call @my_hash.to_html in your views and prints out the contents to html with ul and li tags
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'hash_to_html'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install hash_to_html
18
+
19
+ ## Usage
20
+
21
+ In your Rails views
22
+
23
+ <%= @my_hash.to_html %>
24
+
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/hash_to_html/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Josh Crews"]
6
+ gem.email = ["crews.josh@gmail.com"]
7
+ gem.description = %q{For Rails, Html formats Hash into ul and li html tags}
8
+ gem.summary = %q{For Rails projects, this will take a Hash and make into html including nested hashes}
9
+ gem.homepage = "http://github.com/joshcrews/hash_to_html"
10
+
11
+ gem.add_development_dependency "rspec"
12
+
13
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
14
+ gem.files = `git ls-files`.split("\n")
15
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ gem.name = "hash_to_html"
17
+ gem.require_paths = ["lib"]
18
+ gem.add_dependency('activesupport')
19
+ gem.add_dependency('actionpack')
20
+ gem.version = HashToHtml::VERSION
21
+ end
data/lib/hash.rb ADDED
@@ -0,0 +1,30 @@
1
+ unless defined?(Rails)
2
+ require 'active_support/concern'
3
+ require 'action_view/helpers/capture_helper'
4
+ require 'action_view/helpers/tag_helper'
5
+ require 'active_support/core_ext/string/inflections'
6
+ end
7
+
8
+ class Hash
9
+
10
+ include ActionView::Helpers::TagHelper
11
+
12
+ def to_html
13
+ hash_to_list_items(self)
14
+ end
15
+
16
+ private
17
+
18
+ def hash_to_list_items(hash)
19
+ html = hash.map do |key, value|
20
+ if value.is_a?(Hash)
21
+ inner_html = hash_to_list_items(value)
22
+ content_tag(:li, "#{key.to_s.humanize}: #{inner_html}".html_safe)
23
+ else
24
+ content_tag(:li, "#{key.to_s.humanize}: #{value}".html_safe)
25
+ end
26
+ end.join("\n")
27
+ content_tag(:ul, "\n#{html}\n".html_safe) + "\n"
28
+ end
29
+
30
+ end
@@ -0,0 +1,3 @@
1
+ module HashToHtml
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "hash_to_html/version"
2
+ require "hash"
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Hash do
4
+
5
+ describe "to_html" do
6
+
7
+ context "non-nested hash" do
8
+
9
+ it "should create a single ul" do
10
+ sample_hash = {:name => 'Josh', :book => 'Bible'}
11
+ result = "<ul>\n<li>Name: Josh</li>\n<li>Book: Bible</li>\n</ul>\n"
12
+ sample_hash.to_html.should == result
13
+ end
14
+
15
+ end
16
+
17
+ context "single nesting hash" do
18
+
19
+ it "should create a couple nested ul" do
20
+ sample_hash = {
21
+ :name => 'Biology',
22
+ :teachers => {:primary => 'Mr. Henderson', :assistant => 'Mr. Davis'},
23
+ :schedule => {:monday => "11am", :wednesday => "1pm"}
24
+ }
25
+ result = "<ul>\n<li>Name: Biology</li>\n<li>Teachers: <ul>\n<li>Primary: Mr. Henderson</li>\n<li>Assistant: Mr. Davis</li>\n</ul>\n</li>\n<li>Schedule: <ul>\n<li>Monday: 11am</li>\n<li>Wednesday: 1pm</li>\n</ul>\n</li>\n</ul>\n"
26
+ sample_hash.to_html.should == result
27
+ end
28
+
29
+ end
30
+
31
+ context "double nesting hash" do
32
+
33
+ it "should create some double nested ul's" do
34
+ sample_hash = {
35
+ :name => 'Biology',
36
+ :teachers => {
37
+ :primary => {
38
+ :name => 'Mr. Henderson',
39
+ :title => 'Senior Professor'
40
+ },
41
+ :assistant => {
42
+ :name => 'Mr. Davis',
43
+ :title => 'Graduate Assistant'
44
+ },
45
+ },
46
+ :schedule => {:monday => "11am", :wednesday => "1pm"}
47
+ }
48
+ result = "<ul>\n<li>Name: Biology</li>\n<li>Teachers: <ul>\n<li>Primary: <ul>\n<li>Name: Mr. Henderson</li>\n<li>Title: Senior Professor</li>\n</ul>\n</li>\n<li>Assistant: <ul>\n<li>Name: Mr. Davis</li>\n<li>Title: Graduate Assistant</li>\n</ul>\n</li>\n</ul>\n</li>\n<li>Schedule: <ul>\n<li>Monday: 11am</li>\n<li>Wednesday: 1pm</li>\n</ul>\n</li>\n</ul>\n"
49
+ sample_hash.to_html.should == result
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ end
@@ -0,0 +1,14 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'hash_to_html'
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hash_to_html
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Crews
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &70292872399040 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70292872399040
25
+ - !ruby/object:Gem::Dependency
26
+ name: activesupport
27
+ requirement: &70292872395720 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70292872395720
36
+ - !ruby/object:Gem::Dependency
37
+ name: actionpack
38
+ requirement: &70292872395080 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70292872395080
47
+ description: For Rails, Html formats Hash into ul and li html tags
48
+ email:
49
+ - crews.josh@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - .rspec
56
+ - Gemfile
57
+ - LICENSE
58
+ - README.md
59
+ - Rakefile
60
+ - hash_to_html.gemspec
61
+ - lib/hash.rb
62
+ - lib/hash_to_html.rb
63
+ - lib/hash_to_html/version.rb
64
+ - spec/hash_to_html_spec.rb
65
+ - spec/spec_helper.rb
66
+ homepage: http://github.com/joshcrews/hash_to_html
67
+ licenses: []
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.11
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: For Rails projects, this will take a Hash and make into html including nested
90
+ hashes
91
+ test_files:
92
+ - spec/hash_to_html_spec.rb
93
+ - spec/spec_helper.rb