render_hash 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: efeeb485371194984abf2c6b934132ec65724e82
4
+ data.tar.gz: 4b176fba0cf6c9211162fa58dea000cb97bc56b2
5
+ SHA512:
6
+ metadata.gz: 2edeefa5538cd309f0c7e0dc57b9c92ec6f00841e74679c7c301df95a0a83fa713bd9f1fc1046ebf452c23f5243deb169707b15c4bdf5d2b89ac6a628c7bb285
7
+ data.tar.gz: 9891db65c0a0b5f78d296f39cd59e2ff8c662fead0bb0525f00d8d776fde1b68a339c29ae58797c34fcf54cd040b35bdb6e4d6a65be7a09391b7c4b6902624e6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in render_hash.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Ducksan Cho
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,70 @@
1
+ # RenderHash
2
+
3
+ RenderHash is an alternative to .as_json in rails providing simple syntax to
4
+ generate nested hash from any ruby object
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'render_hash'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install render_hash
21
+
22
+ ## Usage
23
+ RenderHash module can be included to a class
24
+
25
+ user.render(:name, :age) #=> {name: "bob", age: 20}
26
+
27
+ or render directly
28
+
29
+ RenderHash.render(user, :name, :age) #=> {name: "bob", age: 20}
30
+
31
+ ## Syntax
32
+
33
+ render from methods
34
+
35
+ user.render(:name, :age) #=> {name: "bob", age: 20}
36
+
37
+ render from methods with a custom key
38
+
39
+ user.render({username: :name}, :age) #=> {username: "bob", age: 20}
40
+
41
+ render a custom key and value
42
+
43
+ user.render({hobby: "fishing"}) #=> {hobby: "fishing"}
44
+
45
+ hash expressions can be grouped together
46
+
47
+ user.render({username: :name}, {hobby: "fishing"}) ==
48
+ user.render({username: :name, hobby: "fishing"})
49
+
50
+ render a custom key and value with lambda
51
+
52
+ name_with_age = ->(user){"#{user.name}(#{user.age})"}
53
+ user.render({name_with_age: name_with_age}) #=> {name_with_age: "bob(20)"}
54
+
55
+ render nested hash from a method returns an array
56
+
57
+ user.render([jobs: [:title]])
58
+ #=> {jobs: [{title: "doctor"}, {title: "driver"}]}
59
+
60
+ render with an array
61
+
62
+ RenderHash.render([user1, user2], :name) #=> [{name: "bob"}, {name: "tom"}]
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/[my-github-username]/render_hash/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,3 @@
1
+ module RenderHash
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'render_hash/version'
2
+
3
+ module RenderHash
4
+
5
+ # Usage
6
+ # User.include RenderHash
7
+ # user.render(
8
+ # :name, :age,
9
+ # {username: :name, hobby: "fishing"},
10
+ # {name_with_age: -> (user) {"#{user.name}(#{user.age})"}},
11
+ # [jobs: [:title]]
12
+ # )
13
+ # => {
14
+ # name: "bob",
15
+ # age: 20,
16
+ # username: "bob",
17
+ # hobby: "finishig",
18
+ # name_with_age: "bob(20)",
19
+ # jobs: [{title: "doctor"}]
20
+ # }
21
+ def render(*args)
22
+ RenderHash.render(self, *args)
23
+ end
24
+
25
+ # Usage
26
+ # RenderHash.render(user, :name, :age)
27
+ def self.render(obj, *args)
28
+ return obj.map {|x| render(x, *args)} if obj.is_a? Array
29
+
30
+ args.inject({}) do |result, task|
31
+ result.merge(
32
+ case task
33
+ when Symbol
34
+ {task => obj.send(task)}
35
+ when Hash
36
+ task.inject({}) do |h, (k, v)|
37
+ h.merge(k =>
38
+ case v
39
+ when Symbol then obj.send(v)
40
+ when Proc then v.call(obj)
41
+ else v
42
+ end
43
+ )
44
+ end
45
+ when Array
46
+ {task[0] => obj.send(task[0]).map {|x| render(x, *task[1])}}
47
+ end
48
+ )
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'render_hash/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "render_hash"
8
+ spec.version = RenderHash::VERSION
9
+ spec.authors = ["Ducksan Cho"]
10
+ spec.email = ["ducktyper@gmail.com"]
11
+ spec.summary = %q{Build a hash from an object}
12
+ spec.description = %q{render_hash is an alternative to .as_json in rails providing simple syntax to generate nested hash from any ruby object}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,81 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'minitest/autorun'
3
+ require 'render_hash'
4
+
5
+ describe RenderHash do
6
+
7
+ class User
8
+ include RenderHash
9
+ attr_reader :name, :age, :jobs
10
+ def initialize(name, age, jobs)
11
+ @name = name
12
+ @age = age
13
+ @jobs = jobs
14
+ end
15
+ end
16
+
17
+ class Job
18
+ attr_reader :title
19
+ def initialize(title)
20
+ @title = title
21
+ end
22
+ end
23
+
24
+ let(:bob) {User.new('bob', 20, [Job.new('doctor'), Job.new('driver')])}
25
+
26
+ it "add render method on include" do
27
+ assert_equal({}, bob.render)
28
+ end
29
+
30
+ it "render method name and return value" do
31
+ assert_equal({name: 'bob', age: 20}, bob.render(:name, :age))
32
+ end
33
+
34
+ it "raise if method is not found" do
35
+ assert_raises(NoMethodError) {bob.render(:method_not_exist)}
36
+ end
37
+
38
+ it "raise if nested hash method does not return array" do
39
+ assert_raises(NoMethodError) {bob.render(:age, [:name])}
40
+ end
41
+
42
+ it "rename method" do
43
+ assert_equal({username: 'bob'}, bob.render({username: :name}))
44
+ end
45
+
46
+ it "render custom string" do
47
+ assert_equal({name: 'tom'}, bob.render({name: 'tom'}))
48
+ end
49
+
50
+ it "render custom object" do
51
+ hobby = 'fishing'
52
+ assert_equal({hobby: 'fishing'}, bob.render({hobby: hobby}))
53
+ end
54
+
55
+ it "render custom with object" do
56
+ assert_equal(
57
+ {name_with_age: 'bob(20)'},
58
+ bob.render({name_with_age: ->(user){"#{user.name}(#{user.age})"}})
59
+ )
60
+ end
61
+
62
+ it "hash can be ..." do
63
+ assert_equal({username: 'bob', hobby: 'fishing'}, bob.render({username: :name, hobby: "fishing"}))
64
+ end
65
+
66
+ it "render nested hash" do
67
+ assert_equal(
68
+ {jobs: [{title: 'doctor'}, {title: 'driver'}]},
69
+ bob.render([:jobs, [:title]])
70
+ )
71
+ end
72
+
73
+ it "render from self" do
74
+ assert_equal({name: 'bob'}, RenderHash.render(bob, :name))
75
+ end
76
+
77
+ it "render with array" do
78
+ assert_equal([{name: 'bob'}], RenderHash.render([bob], :name))
79
+ end
80
+
81
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: render_hash
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ducksan Cho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: render_hash is an alternative to .as_json in rails providing simple syntax
42
+ to generate nested hash from any ruby object
43
+ email:
44
+ - ducktyper@gmail.com
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - lib/render_hash.rb
55
+ - lib/render_hash/version.rb
56
+ - render_hash.gemspec
57
+ - test/render_hash_test.rb
58
+ homepage: ''
59
+ licenses:
60
+ - MIT
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.4.5
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: Build a hash from an object
82
+ test_files:
83
+ - test/render_hash_test.rb