ishin 0.1.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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +4 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +117 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/ishin.gemspec +27 -0
- data/lib/ishin.rb +79 -0
- data/lib/ishin/version.rb +3 -0
- metadata +112 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9fd416241ae632c771e85f2cbb4a8ecb151b6226
|
4
|
+
data.tar.gz: a2c3b7dccd9496fee3e790e58684bd782644e75f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a06fd4e613aab614d59d635369ab2b4b4be16ed6aadbe9dcae441a9515ec962d93a55a16a29ce5e36c4afb52f19cae90a1da3dbce58cb1af566bbb6035fe0f54
|
7
|
+
data.tar.gz: 5cff38f8588ed32d1b51eccafad2a97d7196983c406a365f8d2fff5ec1995bf44959020f54512fac1473aae05e9b735c47d0ff4aedb5cedf3110062fc1448850
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Eddy Luten
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
# Ishin
|
2
|
+
|
3
|
+
Ishin converts Ruby objects into their Hash representations. It works with plain old classes, extended classes, classes with mixins, and hashes (see Usage for more on that).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'ishin'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
bundle
|
16
|
+
|
17
|
+
Or install it manually by:
|
18
|
+
|
19
|
+
gem install ishin
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
require 'ishin'
|
25
|
+
|
26
|
+
hash = Ishin.to_hash(my_object)
|
27
|
+
```
|
28
|
+
|
29
|
+
### Introduction
|
30
|
+
|
31
|
+
A simple example is worth a thousand words:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
class Animal
|
35
|
+
attr_reader :leg_count
|
36
|
+
|
37
|
+
def initialize leg_count
|
38
|
+
@leg_count = leg_count
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
dog = Animal.new(4)
|
43
|
+
|
44
|
+
dog_hash = Ishin.to_hash(dog)
|
45
|
+
# => {:leg_count=>4}
|
46
|
+
```
|
47
|
+
|
48
|
+
### Recursion
|
49
|
+
|
50
|
+
Ishin also handles object instances nested within other object instances. By default, recursive hash conversion is turned off. To enable recursion, set the `recursive` option to `true`:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
test_struct = Struct.new(:value)
|
54
|
+
nested_structs = test_struct.new(test_struct.new('value'))
|
55
|
+
|
56
|
+
|
57
|
+
Ishin.to_hash(nested_structs, recursive: true)
|
58
|
+
# => {:value=>{:value=>"value"}}
|
59
|
+
```
|
60
|
+
|
61
|
+
### Recursion Depth
|
62
|
+
|
63
|
+
For deeply nested object instances, a maximum recursion depth can be provided in combination with the `recursive` option. The default recursion depth is one (initial call + 1).
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
nest_me = Struct.new(:value)
|
67
|
+
deep_nesting = nest_me.new(nest_me.new(nest_me.new(nest_me.new('such depth'))))
|
68
|
+
|
69
|
+
Ishin.to_hash(deep_nesting, recursive: true, recursion_depth: 2)
|
70
|
+
# => {:value=>{:value=>{:value=>#<struct value="such depth">}}}
|
71
|
+
```
|
72
|
+
|
73
|
+
Notice in the above example that the recursion stopped after 3 steps (initial call + 2).
|
74
|
+
|
75
|
+
### Expanding Hashes using Recursion
|
76
|
+
|
77
|
+
Using recursion, it is also possible to convert hashes containing object instances to a hash-only representation as well. Notice that this only works if the `recursive` option is provided.
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
another_struct = Struct.new(:value)
|
81
|
+
my_hash = {
|
82
|
+
my_struct: another_struct.new("yup, it's a struct")
|
83
|
+
}
|
84
|
+
|
85
|
+
Ishin.to_hash(my_hash, recursive: true)
|
86
|
+
# => {:my_struct=>{:value=>"yup, it's a struct"}}
|
87
|
+
```
|
88
|
+
|
89
|
+
Keep in mind that the `recursive` option works in conjunction with the `recursion_depth` option.
|
90
|
+
|
91
|
+
### Symbolizing Keys
|
92
|
+
|
93
|
+
By default, Ishin stores key names as symbols. This behavior can be disabled by setting the `symbolize` option to `false`.
|
94
|
+
|
95
|
+
```ruby
|
96
|
+
class Dog
|
97
|
+
attr_reader :says
|
98
|
+
|
99
|
+
def initialize(says)
|
100
|
+
@says = says
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
lassie = Dog.new('Timmy is stuck in a well!')
|
105
|
+
|
106
|
+
Ishin.to_hash(lassie)
|
107
|
+
# => {:says=>"Timmy is stuck in a well!"}
|
108
|
+
Ishin.to_hash(lassie, symbolize: false)
|
109
|
+
# => {"says"=>"Timmy is stuck in a well!"}
|
110
|
+
```
|
111
|
+
When setting the `symbolize` option to `false`, the explicit conversion of strings to symbols is prevented. This, however, does *not* mean that hashes whose keys are already symbols are converted into string-based keys.
|
112
|
+
|
113
|
+
## Running the Specs
|
114
|
+
|
115
|
+
Once `bundle` is executed, simply run:
|
116
|
+
|
117
|
+
rake spec
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ishin"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/ishin.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ishin/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ishin"
|
8
|
+
spec.version = Ishin::VERSION
|
9
|
+
spec.authors = ["Eddy Luten"]
|
10
|
+
spec.email = ["eddyluten@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ishin is an object to hash converter.}
|
13
|
+
spec.description = %q{Ishin converts objects into their Hash representations.}
|
14
|
+
spec.homepage = "https://github.com/EddyLuten/ishin"
|
15
|
+
spec.license = "MIT"
|
16
|
+
spec.platform = Gem::Platform::RUBY
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec"
|
26
|
+
spec.add_development_dependency "rspec-nc"
|
27
|
+
end
|
data/lib/ishin.rb
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'ishin/version'
|
2
|
+
|
3
|
+
module Ishin
|
4
|
+
|
5
|
+
def self.to_hash(object, options = {})
|
6
|
+
options = defaults.merge(options)
|
7
|
+
result = {}
|
8
|
+
|
9
|
+
case object
|
10
|
+
when Struct
|
11
|
+
struct_to_hash(result, object, options)
|
12
|
+
when Hash
|
13
|
+
hash_to_hash(result, object, options)
|
14
|
+
else
|
15
|
+
object_to_hash(result, object, options)
|
16
|
+
end
|
17
|
+
|
18
|
+
result
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def self.decrement_recursion_depth(options)
|
24
|
+
options[:recursion_depth] = [0, options[:recursion_depth] - 1].max
|
25
|
+
options
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.assign_value(result, key, value, options, new_options)
|
29
|
+
result[key] = should_recurse?(options, value) ? to_hash(value, new_options) : value
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.hash_to_hash(result, object, options)
|
33
|
+
return result.replace(object) unless options[:recursive]
|
34
|
+
|
35
|
+
new_options = decrement_recursion_depth(options.clone)
|
36
|
+
|
37
|
+
object.each do |key, value|
|
38
|
+
key = key.to_sym if options[:symbolize] && key.is_a?(String)
|
39
|
+
assign_value(result, key, value, options, new_options)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.struct_to_hash result, object, options
|
44
|
+
new_options = decrement_recursion_depth options.clone
|
45
|
+
|
46
|
+
object.members.each do |member|
|
47
|
+
key = options[:symbolize] ? member : member.to_s
|
48
|
+
value = object[member]
|
49
|
+
assign_value(result, key, value, options, new_options)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def self.object_to_hash(result, object, options)
|
54
|
+
new_options = decrement_recursion_depth(options.clone)
|
55
|
+
|
56
|
+
object.instance_variables.each do |var|
|
57
|
+
value = object.instance_variable_get(var)
|
58
|
+
key = var.to_s.delete('@')
|
59
|
+
key = key.to_sym if options[:symbolize]
|
60
|
+
assign_value(result, key, value, options, new_options)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.should_recurse? options, value
|
65
|
+
options[:recursive] && options[:recursion_depth] > 0 && !is_native_type?(value)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.is_native_type? value
|
69
|
+
[ String, Numeric, TrueClass, FalseClass ].any? { |i| value.is_a?(i) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.defaults
|
73
|
+
{
|
74
|
+
recursive: false,
|
75
|
+
recursion_depth: 1,
|
76
|
+
symbolize: true
|
77
|
+
}
|
78
|
+
end
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ishin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eddy Luten
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-24 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.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-nc
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Ishin converts objects into their Hash representations.
|
70
|
+
email:
|
71
|
+
- eddyluten@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- ishin.gemspec
|
86
|
+
- lib/ishin.rb
|
87
|
+
- lib/ishin/version.rb
|
88
|
+
homepage: https://github.com/EddyLuten/ishin
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - ">="
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.4.5
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Ishin is an object to hash converter.
|
112
|
+
test_files: []
|