ecraft-extensions 0.2.3 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +2 -0
- data/.rubocop_todo.yml +29 -0
- data/README.md +2 -0
- data/ecraft-extensions.gemspec +1 -0
- data/lib/ecraft/extensions/mixins/hashable.rb +113 -0
- data/lib/ecraft/extensions/version.rb +1 -1
- metadata +19 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c25113dc68cc699ceb7211a9f6350287931416a8
|
4
|
+
data.tar.gz: fcf1be83809dc87238fd1731f79602bb84b8b7e5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8238dc69b792b8c9221f22b139e1ffa797d21d48c44b0a1003473d410d3d064c0f1d50550246ed11e2a04290e460fed56f51bfbb60d1239d3d28c47a315f8b7a
|
7
|
+
data.tar.gz: a94300f74e6c00a4bf5f4d295afc1bc2c845dce93473387900777c21ed9660a2e67091441fe6ef60b567ee770d2543b141515950549a01ba90f531a77de4a1d0
|
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2017-10-10 12:17:14 +0300 using RuboCop version 0.50.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 2
|
10
|
+
Metrics/AbcSize:
|
11
|
+
Max: 20
|
12
|
+
|
13
|
+
# Offense count: 2
|
14
|
+
Metrics/CyclomaticComplexity:
|
15
|
+
Max: 8
|
16
|
+
|
17
|
+
# Offense count: 3
|
18
|
+
# Configuration parameters: CountComments.
|
19
|
+
Metrics/MethodLength:
|
20
|
+
Max: 14
|
21
|
+
|
22
|
+
# Offense count: 1
|
23
|
+
# Configuration parameters: CountComments.
|
24
|
+
Metrics/ModuleLength:
|
25
|
+
Max: 135
|
26
|
+
|
27
|
+
# Offense count: 2
|
28
|
+
Metrics/PerceivedComplexity:
|
29
|
+
Max: 9
|
data/README.md
CHANGED
data/ecraft-extensions.gemspec
CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
+
spec.add_runtime_dependency 'activesupport', '>= 4.0'
|
23
24
|
spec.add_runtime_dependency 'json', '>= 1.8'
|
24
25
|
|
25
26
|
spec.add_development_dependency 'bundler'
|
@@ -0,0 +1,113 @@
|
|
1
|
+
#
|
2
|
+
# Utility mixin for converting arbitrary objects to Hashes.
|
3
|
+
#
|
4
|
+
# Similar to https://github.com/mustafaturan/hashable, but different in implementation.
|
5
|
+
#
|
6
|
+
# This mixin is deprecated, since it changes the behavior of to_h in a potentially unpleasant and surprising way for developers.
|
7
|
+
# It should only be used to make legacy code run and work; it should preferably not be used for new code being written.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'active_support'
|
11
|
+
require 'active_support/core_ext/hash/keys'
|
12
|
+
|
13
|
+
module Ecraft
|
14
|
+
module Extensions
|
15
|
+
module Mixins
|
16
|
+
module Hashable
|
17
|
+
def to_h(*targets)
|
18
|
+
target = get_target(targets)
|
19
|
+
|
20
|
+
if target.is_a?(Hash)
|
21
|
+
strip_string_recurse_enumerable_values(target)
|
22
|
+
elsif target.is_a?(Array)
|
23
|
+
strip_array(target)
|
24
|
+
elsif target.is_a?(OpenStruct)
|
25
|
+
target.marshal_dump
|
26
|
+
elsif value_type?(target)
|
27
|
+
target
|
28
|
+
# We need to check to ensure we don't call ourselves recursively.
|
29
|
+
elsif !target.class.included_modules.include?(Hashable) && target.respond_to?(:to_h)
|
30
|
+
target.to_h
|
31
|
+
else
|
32
|
+
turn_object_into_hash(target)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_hash(target = nil)
|
37
|
+
to_h(target)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def get_target(targets)
|
43
|
+
if targets.empty?
|
44
|
+
self
|
45
|
+
else
|
46
|
+
targets.first
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def value_type?(v)
|
51
|
+
v.is_a?(BigDecimal) ||
|
52
|
+
v.is_a?(Integer) ||
|
53
|
+
v.is_a?(Float) ||
|
54
|
+
v.is_a?(String) ||
|
55
|
+
v.is_a?(TrueClass) ||
|
56
|
+
v.is_a?(FalseClass)
|
57
|
+
end
|
58
|
+
|
59
|
+
def strip_string_recurse_enumerable_values(hash)
|
60
|
+
hash.each_value do |v|
|
61
|
+
v.rstrip! if v.respond_to?(:rstrip!) && !v.frozen?
|
62
|
+
to_h(v) if v.is_a?(Enumerable)
|
63
|
+
end
|
64
|
+
hash.symbolize_keys
|
65
|
+
end
|
66
|
+
|
67
|
+
def strip_array(array)
|
68
|
+
array.map do |v|
|
69
|
+
if v.nil?
|
70
|
+
nil
|
71
|
+
elsif v.is_a?(String)
|
72
|
+
v.rstrip
|
73
|
+
elsif v.respond_to?(:to_h) && !v.is_a?(Hash)
|
74
|
+
v.to_h
|
75
|
+
else
|
76
|
+
to_h(v)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Returns a Symbol-keyed Hash based on target's instance variables.
|
82
|
+
#
|
83
|
+
# @param target [Object]
|
84
|
+
#
|
85
|
+
# @return [Hash] with Symbol keys
|
86
|
+
def turn_object_into_hash(target)
|
87
|
+
target.instance_variables.each_with_object({}) do |var, hash|
|
88
|
+
original = target.instance_variable_get(var)
|
89
|
+
value = original
|
90
|
+
|
91
|
+
if value.is_a?(Hash)
|
92
|
+
# Nothing to do - it's already a hash, so we presume we don't have to do do any more conversions of it.
|
93
|
+
elsif value.is_a?(Array) && value.first.is_a?(Hash)
|
94
|
+
# Nothing to do - an Array of Hashes already have a sane enough format.
|
95
|
+
else
|
96
|
+
map_enumerable(original) if original.respond_to?(:each)
|
97
|
+
value = original.to_h if original.respond_to?(:to_h)
|
98
|
+
value.rstrip! if value.respond_to?(:rstrip!) && !value.frozen?
|
99
|
+
end
|
100
|
+
|
101
|
+
hash[var.to_s.delete('@').to_sym] = value
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def map_enumerable(enumerable)
|
106
|
+
enumerable.map do |v|
|
107
|
+
v.respond_to?(:to_h) ? v.to_h : v
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ecraft-extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tre Kronor
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '4.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '4.0'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: json
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -146,6 +160,7 @@ files:
|
|
146
160
|
- ".gitignore"
|
147
161
|
- ".rspec"
|
148
162
|
- ".rubocop.yml"
|
163
|
+
- ".rubocop_todo.yml"
|
149
164
|
- ".ruby-version"
|
150
165
|
- ".travis.yml"
|
151
166
|
- ".yardopts"
|
@@ -159,6 +174,7 @@ files:
|
|
159
174
|
- lib/ecraft/extensions/bigdecimal.rb
|
160
175
|
- lib/ecraft/extensions/date.rb
|
161
176
|
- lib/ecraft/extensions/datetime.rb
|
177
|
+
- lib/ecraft/extensions/mixins/hashable.rb
|
162
178
|
- lib/ecraft/extensions/ostruct.rb
|
163
179
|
- lib/ecraft/extensions/string.rb
|
164
180
|
- lib/ecraft/extensions/symbol.rb
|
@@ -184,7 +200,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
184
200
|
version: '0'
|
185
201
|
requirements: []
|
186
202
|
rubyforge_project:
|
187
|
-
rubygems_version: 2.6.
|
203
|
+
rubygems_version: 2.6.14
|
188
204
|
signing_key:
|
189
205
|
specification_version: 4
|
190
206
|
summary: eCraft Extensions
|