ishin 0.2.1 → 0.3.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 +4 -4
- data/README.md +19 -0
- data/ishin.gemspec +10 -10
- data/lib/ishin/version.rb +1 -1
- data/lib/ishin.rb +26 -12
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 916893740198856f3e8461ad709f9764bf1d286e
|
4
|
+
data.tar.gz: 6fdf9f93f7300115d3f6423e5784a48a39e3a366
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bad9f801b6189842d1a1bc57627de382d3fe22ed5847230e082d4a12ff40a289b72198ac0f8fdb59b079fbe9caef3cf5aa6b89819b0695149a08adc247d43184
|
7
|
+
data.tar.gz: a4412fccee7db64485611fca3ef02182985101ca635ec935bab2af393972e78e0ac8624bf0732184d05948280380e3d3b55f96d866dac7c7f510f1de0ede2fcf
|
data/README.md
CHANGED
@@ -129,6 +129,25 @@ Ishin.to_hash(lassie, symbolize: false)
|
|
129
129
|
```
|
130
130
|
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.
|
131
131
|
|
132
|
+
### Evaluating Methods
|
133
|
+
|
134
|
+
Normally, Ishin does not evaluate methods, but is possible to do so through the optional `evaluate` option by passing it an array of method names to evaluate.
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
class Speaker
|
138
|
+
def say
|
139
|
+
"Say what?"
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
speaker = Speaker.new
|
144
|
+
# => #<Speaker:0x007fb2bb1812d8>
|
145
|
+
Ishin.to_hash(speaker, evaluate: [ :say ])
|
146
|
+
# => {:say=>"Say what?"}
|
147
|
+
```
|
148
|
+
|
149
|
+
It is currently only possible to evaluate methods that do not require arguments.
|
150
|
+
|
132
151
|
## As a Mixin
|
133
152
|
|
134
153
|
To use Ishin as a mixin in your own objects, simply include `Ishin::Mixin`:
|
data/ishin.gemspec
CHANGED
@@ -4,23 +4,23 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
require 'ishin/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
7
|
+
spec.name = 'ishin'
|
8
8
|
spec.version = Ishin::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
9
|
+
spec.authors = ['Eddy Luten']
|
10
|
+
spec.email = ['eddyluten@gmail.com']
|
11
11
|
|
12
12
|
spec.summary = %q{Ishin is an object to hash converter.}
|
13
13
|
spec.description = %q{Ishin converts objects into their Hash representations.}
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
14
|
+
spec.homepage = 'https://github.com/EddyLuten/ishin'
|
15
|
+
spec.license = 'MIT'
|
16
16
|
spec.platform = Gem::Platform::RUBY
|
17
17
|
|
18
18
|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
-
spec.bindir =
|
19
|
+
spec.bindir = 'exe'
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
-
spec.require_paths = [
|
21
|
+
spec.require_paths = ['lib']
|
22
22
|
|
23
|
-
spec.add_development_dependency
|
24
|
-
spec.add_development_dependency
|
25
|
-
spec.add_development_dependency
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
spec.add_development_dependency 'rspec'
|
25
|
+
spec.add_development_dependency 'codeclimate-test-reporter'
|
26
26
|
end
|
data/lib/ishin/version.rb
CHANGED
data/lib/ishin.rb
CHANGED
@@ -1,10 +1,9 @@
|
|
1
1
|
require 'ishin/version'
|
2
2
|
|
3
3
|
module Ishin
|
4
|
-
|
5
4
|
module Mixin
|
6
5
|
def to_hash(options = {})
|
7
|
-
Ishin
|
6
|
+
Ishin.to_hash(self, options)
|
8
7
|
end
|
9
8
|
end
|
10
9
|
|
@@ -13,11 +12,12 @@ module Ishin
|
|
13
12
|
result = {}
|
14
13
|
|
15
14
|
case object
|
16
|
-
|
17
|
-
|
18
|
-
|
15
|
+
when Struct then struct_to_hash(result, object, options)
|
16
|
+
when Hash then hash_to_hash(result, object, options)
|
17
|
+
else object_to_hash(result, object, options)
|
19
18
|
end
|
20
19
|
|
20
|
+
evaluate_methods(result, object, options)
|
21
21
|
result
|
22
22
|
end
|
23
23
|
|
@@ -29,14 +29,16 @@ module Ishin
|
|
29
29
|
end
|
30
30
|
|
31
31
|
def self.hash_to_hash(result, object, options)
|
32
|
-
|
32
|
+
if !should_recurse?(options) && options[:symbolize]
|
33
|
+
return result.replace(object)
|
34
|
+
end
|
33
35
|
|
34
36
|
new_options = decrement_recursion_depth(options.clone)
|
35
37
|
|
36
38
|
object.each do |key, value|
|
37
39
|
key = key.to_sym if options[:symbolize] && key.is_a?(String)
|
38
40
|
|
39
|
-
result[key] =
|
41
|
+
result[key] = native_type?(value) ? value : to_hash(value, new_options)
|
40
42
|
end
|
41
43
|
end
|
42
44
|
|
@@ -47,7 +49,7 @@ module Ishin
|
|
47
49
|
new_options = decrement_recursion_depth(options.clone)
|
48
50
|
|
49
51
|
result.each do |key, value|
|
50
|
-
result[key] = to_hash(value, new_options) unless
|
52
|
+
result[key] = to_hash(value, new_options) unless native_type?(value)
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
@@ -59,7 +61,7 @@ module Ishin
|
|
59
61
|
key = var.to_s.delete('@')
|
60
62
|
key = key.to_sym if options[:symbolize]
|
61
63
|
|
62
|
-
if should_recurse?(options) && !
|
64
|
+
if should_recurse?(options) && !native_type?(value)
|
63
65
|
result[key] = to_hash(value, new_options)
|
64
66
|
else
|
65
67
|
result[key] = value
|
@@ -67,19 +69,31 @@ module Ishin
|
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
72
|
+
def self.evaluate_methods(result, object, options)
|
73
|
+
return if options[:evaluate].nil? || options[:evaluate].empty?
|
74
|
+
|
75
|
+
options[:evaluate].each do |method|
|
76
|
+
next unless object.methods.include?(method)
|
77
|
+
key_name = options[:symbolize] ? method : method.to_s
|
78
|
+
result[key_name] = object.send(method)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
70
82
|
def self.should_recurse?(options)
|
71
83
|
options[:recursive] && options[:recursion_depth] > 0
|
72
84
|
end
|
73
85
|
|
74
|
-
def self.
|
75
|
-
[
|
86
|
+
def self.native_type?(value)
|
87
|
+
[String, Numeric, TrueClass, FalseClass].any? { |i| value.is_a?(i) }
|
76
88
|
end
|
77
89
|
|
78
90
|
def self.defaults
|
79
91
|
{
|
80
92
|
recursive: false,
|
81
93
|
recursion_depth: 1,
|
82
|
-
symbolize: true
|
94
|
+
symbolize: true,
|
95
|
+
evaluate: [],
|
96
|
+
exclude: []
|
83
97
|
}
|
84
98
|
end
|
85
99
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ishin
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eddy Luten
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|