flatten 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 +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/flatten.gemspec +33 -0
- data/lib/flatten.rb +98 -0
- data/lib/flatten/core_ext/kernel.rb +18 -0
- data/lib/flatten/deprecations.rb +28 -0
- data/lib/flatten/guard_methods.rb +24 -0
- data/lib/flatten/separator.rb +75 -0
- data/lib/flatten/string.rb +5 -0
- data/lib/flatten/utility_methods.rb +201 -0
- data/lib/flatten/version.rb +3 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 842d1d572be35589eaa48929578f8d2cc9559165
|
4
|
+
data.tar.gz: 83787fafa3226e13e57ea7672370810e693dc8cf
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e2217507bd0bf23e94c259ee315b67eca3c0c1b955ab4c840f8552c83067352af16e162b09f7b51d89320ae07f125ed54f48a60a872ea68084ebd91de15328aa
|
7
|
+
data.tar.gz: 7af4798c8aeab5d36827846c03c39ca05ef9c8cce17ac513ab4c2d17fa04d893f71b5e720ee1d6530abfdbf07f9705842b78d79bbac94abe338d1293a323ce22
|
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) 2016 l3x
|
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,39 @@
|
|
1
|
+
# Flatten
|
2
|
+
|
3
|
+
A Ruby Gem to Flatten Nested JSON
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'flatten'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install flatten
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/flatten.
|
34
|
+
|
35
|
+
|
36
|
+
## License
|
37
|
+
|
38
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
39
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "flatten"
|
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/flatten.gemspec
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'flatten/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "flatten"
|
8
|
+
spec.version = Flatten::VERSION
|
9
|
+
spec.authors = ["l3x"]
|
10
|
+
spec.email = ["lex.sheehan@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A Ruby Gem to Flatten Nested JSON }
|
13
|
+
spec.description = %q{A Ruby Gem to Flatten Nested JSON }
|
14
|
+
spec.homepage = "https://github.com/l3x/flatten"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency "bundler", "~> 1.12"
|
31
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
32
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
33
|
+
end
|
data/lib/flatten.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'flatten/version'
|
4
|
+
require "flatten/string"
|
5
|
+
require 'flatten/utility_methods'
|
6
|
+
require 'flatten/guard_methods'
|
7
|
+
require 'flatten/separator'
|
8
|
+
require 'flatten/core_ext/kernel'
|
9
|
+
|
10
|
+
# Provides smash-key access to a Hash.
|
11
|
+
#
|
12
|
+
# {'foo'=>{'bar'=>'bingo'}}.smash #=> {'foo.bar'=>'bingo'}
|
13
|
+
# {'foo.bar'=>'bingo'}.unsmash => {'foo'=>{'bar'=>'bingo'}}
|
14
|
+
#
|
15
|
+
module Flatten
|
16
|
+
# The default separator, used if not specified in command's
|
17
|
+
# options hash.
|
18
|
+
DEFAULT_SEPARATOR = '.'.freeze
|
19
|
+
|
20
|
+
# Returns a smash version of self using the options provided.
|
21
|
+
#
|
22
|
+
# @param options [Hash<Symbol,Object>]
|
23
|
+
# @option options [String] :separator
|
24
|
+
# @option options [String] :prefix
|
25
|
+
# @option options [Boolean,:zero_pad] :smash_array (false)
|
26
|
+
# truthy values will cause arrays to be smashd by index and decended into.
|
27
|
+
# :zero_pad causes indexes to be zero-padded to make them sort lexically.
|
28
|
+
# @return [Hash<String,Object>]
|
29
|
+
def smash(options = {})
|
30
|
+
Flatten.smash(self, options)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Replaces self with smash version of itself.
|
34
|
+
#
|
35
|
+
# @param options (see #smash)
|
36
|
+
# @return [Hash<String,Object>]
|
37
|
+
def smash!(options = {})
|
38
|
+
self.replace(smash, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
# Used internally by both Flatten::Utility#smash and
|
42
|
+
# Flatten::Utility#unsmash
|
43
|
+
#
|
44
|
+
# @overload smash_each(options = {}, &block)
|
45
|
+
# Yields once per key in smash version of itself.
|
46
|
+
# @param options (see #smash)
|
47
|
+
# @yieldparam [(smash_key,value)]
|
48
|
+
# @return [void]
|
49
|
+
# @overload smash_each(options = {})
|
50
|
+
# @param options (see #smash)
|
51
|
+
# @return [Enumerator<(smash_key,value)>]
|
52
|
+
def smash_each(options = {}, &block)
|
53
|
+
Flatten.smash_each(self, options, &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
# Follows semantics of Hash#fetch
|
57
|
+
#
|
58
|
+
# @overload smash_fetch(smash_key, options = {})
|
59
|
+
# @param options (see #smash)
|
60
|
+
# @raise [KeyError] if smash_key not found
|
61
|
+
# @return [Object]
|
62
|
+
# @overload smash_fetch(smash_key, default, options = {})
|
63
|
+
# @param options (see #smash)
|
64
|
+
# @param default [Object] the default object
|
65
|
+
# @return [default]
|
66
|
+
# @overload smash_fetch(smash_key, options = {}, &block)
|
67
|
+
# @param options (see #smash)
|
68
|
+
# @yield if smash_key not founs
|
69
|
+
# @return [Object] that which was returned by the given block.
|
70
|
+
def smash_fetch(*args, &block)
|
71
|
+
Flatten.smash_fetch(self, *args, &block)
|
72
|
+
end
|
73
|
+
|
74
|
+
# Follows semantics of Hash#[] without support for Hash#default_proc
|
75
|
+
#
|
76
|
+
# @overload smash_get(smash_key, options = {})
|
77
|
+
# @param options (see #smash)
|
78
|
+
# @return [Object] at that address or nil if none found
|
79
|
+
def smash_get(*args)
|
80
|
+
Flatten.smash_get(self, *args)
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns a deeply-nested hash version of self.
|
84
|
+
#
|
85
|
+
# @param options (see #smash)
|
86
|
+
# @return [Hash<String,Object>]
|
87
|
+
def unsmash(options = {})
|
88
|
+
Flatten.unsmash(self, options)
|
89
|
+
end
|
90
|
+
|
91
|
+
# Replaces self with deeply-nested version of self.
|
92
|
+
#
|
93
|
+
# @param options (see #smash)
|
94
|
+
# @return [Hash<String,Object>]
|
95
|
+
def unsmash!(options = {})
|
96
|
+
self.replace(unsmash, options)
|
97
|
+
end
|
98
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
|
5
|
+
# @see Flatten#smash
|
6
|
+
# @api public
|
7
|
+
def Flatten(hsh, options = {})
|
8
|
+
Flatten.smash(hsh, options)
|
9
|
+
end
|
10
|
+
private :Flatten
|
11
|
+
|
12
|
+
# @see Flatten#unsmash
|
13
|
+
# @api public
|
14
|
+
def Unflatten(hsh, options = {})
|
15
|
+
Flatten.unsmash(hsh, options)
|
16
|
+
end
|
17
|
+
private :Unflatten
|
18
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module Flatten
|
6
|
+
module Deprecations
|
7
|
+
class << self
|
8
|
+
def deprecate(message, target)
|
9
|
+
@deprecations ||= Set.new
|
10
|
+
msg = "Flatten: #{message} is deprecated " +
|
11
|
+
"and will be removed in #{target} (at #{external_callpoint})"
|
12
|
+
warn(msg) if @deprecations.add?(msg)
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def external_callpoint
|
18
|
+
caller.drop_while { |loc| loc['lib/flatten/'] }.first
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def deprecate(message, target)
|
25
|
+
Deprecations.deprecate(message, target)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Flatten
|
4
|
+
# Methods to ensure Flatten isn't mixed into nonsensical things
|
5
|
+
module GuardMethods
|
6
|
+
# Flatten can be *extended* into instances of Hash
|
7
|
+
# @param base [Hash]
|
8
|
+
def extended(base)
|
9
|
+
unless base.is_a? Hash
|
10
|
+
raise ArgumentError, "<#{base.inspect}> is not a Hash!"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# Sparsigy can be *included* into implementations of Hash
|
15
|
+
# @param base [Hash.class]
|
16
|
+
def included(base)
|
17
|
+
unless base <= Hash
|
18
|
+
raise ArgumentError, "<#{base.inspect} does not inherit Hash"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
extend GuardMethods
|
24
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'flatten/deprecations'
|
4
|
+
|
5
|
+
module Flatten
|
6
|
+
# Container for a separator and functionality for splitting &
|
7
|
+
# joining with proper escaping.
|
8
|
+
# @api private
|
9
|
+
class Separator
|
10
|
+
include Deprecations
|
11
|
+
|
12
|
+
@separators = {}
|
13
|
+
|
14
|
+
# Returns a memoized Separator object for the given separator_character
|
15
|
+
# Evicts a member at random if memoized pool grows beyond 100.
|
16
|
+
#
|
17
|
+
# @param separator_character [String] (see #initialize)
|
18
|
+
def self.[](separator_character)
|
19
|
+
@separators[separator_character] ||= begin
|
20
|
+
@separators.delete(@separators.keys.sample) if @separators.size > 100
|
21
|
+
new(separator_character)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# @param separator [String] single-character string
|
26
|
+
def initialize(separator)
|
27
|
+
unless separator.kind_of?(String) && separator.size > 0
|
28
|
+
fail ArgumentError, "separator must be a non-empty String " +
|
29
|
+
"got #{separator.inspect}"
|
30
|
+
end
|
31
|
+
deprecate('multi-character separator', '2.0') unless separator.size == 1
|
32
|
+
@separator = separator
|
33
|
+
end
|
34
|
+
|
35
|
+
# Joins a pre-escaped string with a not-yet escaped string on our separator,
|
36
|
+
# escaping the new part before joining.
|
37
|
+
# @param pre_escaped_prefix [String]
|
38
|
+
# @param new_part [String] - will be escaped before joining
|
39
|
+
# @return [String]
|
40
|
+
def join(pre_escaped_prefix, new_part)
|
41
|
+
[pre_escaped_prefix, escape(new_part)].compact.join(@separator)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Splits a string by our separator into non-escaped parts
|
45
|
+
# @param str [String]
|
46
|
+
# @return [Array<String>]
|
47
|
+
def split(str)
|
48
|
+
@unescaped_separator ||= /(?<!\\)(#{Regexp.escape(@separator)})/
|
49
|
+
# String#split(<Regexp>) on non zero-width matches yields the match
|
50
|
+
# as the even entries in the array.
|
51
|
+
parts = str.split(@unescaped_separator).each_slice(2).map(&:first)
|
52
|
+
parts.map do |part|
|
53
|
+
unescape(part)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
# backslash-escapes our separator and backlashes in a string
|
60
|
+
# @param str [String]
|
61
|
+
# @return [String]
|
62
|
+
def escape(str)
|
63
|
+
@escape_pattern ||= /(\\|#{Regexp.escape(@separator)})/
|
64
|
+
str.gsub(@escape_pattern, '\\\\\1')
|
65
|
+
end
|
66
|
+
|
67
|
+
# removes backslash-escaping of our separator and backslashes from a string
|
68
|
+
# @param str [String]
|
69
|
+
# @return [String]
|
70
|
+
def unescape(str)
|
71
|
+
@unescape_pattern ||= /\\(\\|#{Regexp.escape(@separator)})/
|
72
|
+
str.gsub(@unescape_pattern, '\1')
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module Flatten
|
4
|
+
# The Utility Methods provide a significant (~4x) performance increase
|
5
|
+
# over extend-ing instance methods everywhere we need them.
|
6
|
+
module UtilityMethods
|
7
|
+
|
8
|
+
# Provides a way to iterate through a deeply-nested hash as if it were
|
9
|
+
# a smash-hash. Used internally for generating and deconstructing smash
|
10
|
+
# hashes.
|
11
|
+
#
|
12
|
+
# @overload smash_each(hsh, options = {}, &block)
|
13
|
+
# Yields once per key in smash version of itself.
|
14
|
+
# @param hsh [Hash<#to_s,Object>]
|
15
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
16
|
+
# @yieldparam [(smash_key,value)]
|
17
|
+
# @return [void]
|
18
|
+
# @overload smash_each(hsh, options = {})
|
19
|
+
# @param hsh [Hash<#to_s,Object>]
|
20
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
21
|
+
# @return [Enumerator<(smash_key,value)>]
|
22
|
+
def smash_each(hsh, options = {}, &block)
|
23
|
+
return enum_for(:smash_each, hsh, options) unless block_given?
|
24
|
+
|
25
|
+
inherited_prefix = options.fetch(:prefix, nil)
|
26
|
+
separator = options.fetch(:separator, DEFAULT_SEPARATOR)
|
27
|
+
smash_array = options.fetch(:smash_array, false)
|
28
|
+
|
29
|
+
hsh.each do |partial_key, value|
|
30
|
+
key = escaped_join(inherited_prefix, partial_key.to_s, separator)
|
31
|
+
if value.kind_of?(Hash) && !value.empty?
|
32
|
+
smash_each(value, options.merge(prefix: key), &block)
|
33
|
+
elsif smash_array && value.kind_of?(Array) && !value.empty?
|
34
|
+
zps = (smash_array == :zero_pad ? "%0#{value.count.to_s.size}d" : '%d')# zero-pad string
|
35
|
+
smash_each(value.count.times.map(&zps.method(:%)).zip(value), options.merge(prefix: key), &block)
|
36
|
+
else
|
37
|
+
yield key, value
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Returns a smash version of the given hash
|
43
|
+
#
|
44
|
+
# @param hsh [Hash<#to_s,Object>]
|
45
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
46
|
+
# @return [Hash<String,Object>]
|
47
|
+
def smash(hsh, options = {})
|
48
|
+
enum = smash_each(hsh, options)
|
49
|
+
enum.each_with_object(Hash.new) do |(key, value), memo|
|
50
|
+
memo[key] = value
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns a deeply-nested version of the given smash hash
|
55
|
+
# @param hsh [Hash<#to_s,Object>]
|
56
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
57
|
+
# @return [Hash<String,Object>]
|
58
|
+
def unsmash(hsh, options = {})
|
59
|
+
separator = options.fetch(:separator, DEFAULT_SEPARATOR)
|
60
|
+
hsh.each_with_object({}) do |(k, v), memo|
|
61
|
+
current = memo
|
62
|
+
key = escaped_split(k, separator)
|
63
|
+
puts "key: #{key}"
|
64
|
+
up_next = partial = key.shift
|
65
|
+
until key.size.zero?
|
66
|
+
up_next = key.shift
|
67
|
+
up_next = up_next.to_i if (up_next =~ /\A[0-9]+\Z/)
|
68
|
+
current = (current[partial] ||= (up_next.kind_of?(Integer) ? [] : {}))
|
69
|
+
case up_next
|
70
|
+
when Integer then raise KeyError unless current.kind_of?(Array)
|
71
|
+
else raise KeyError unless current.kind_of?(Hash)
|
72
|
+
end
|
73
|
+
partial = up_next
|
74
|
+
end
|
75
|
+
current[up_next] = v
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
# Fetch a smash key from the given deeply-nested hash.
|
80
|
+
#
|
81
|
+
# @overload smash_fetch(hsh, smash_key, default, options = {})
|
82
|
+
# @param hsh [Hash<#to_s,Object>]
|
83
|
+
# @param smash_key [#to_s]
|
84
|
+
# @param default [Object] returned if smash key not found
|
85
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
86
|
+
# @return [Object]
|
87
|
+
# @overload smash_fetch(hsh, smash_key, options = {}, &block)
|
88
|
+
# @param hsh [Hash<#to_s,Object>]
|
89
|
+
# @param smash_key [#to_s]
|
90
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
91
|
+
# @yieldreturn is returned if key not found
|
92
|
+
# @return [Object]
|
93
|
+
# @overload smash_fetch(hsh, smash_key, options = {})
|
94
|
+
# @param hsh [Hash<#to_s,Object>]
|
95
|
+
# @param smash_key [#to_s]
|
96
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
97
|
+
# @raise KeyError if key not found
|
98
|
+
# @return [Object]
|
99
|
+
def smash_fetch(hsh, smash_key, *args, &block)
|
100
|
+
options = ( args.last.kind_of?(Hash) ? args.pop : {})
|
101
|
+
default = args.pop
|
102
|
+
|
103
|
+
separator = options.fetch(:separator, DEFAULT_SEPARATOR)
|
104
|
+
|
105
|
+
escaped_split(smash_key, separator).reduce(hsh) do |memo, kp|
|
106
|
+
if memo.kind_of?(Hash) and memo.has_key?(kp)
|
107
|
+
memo.fetch(kp)
|
108
|
+
elsif default
|
109
|
+
return default
|
110
|
+
elsif block_given?
|
111
|
+
return yield
|
112
|
+
else
|
113
|
+
raise KeyError, smash_key
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# Get a smash key from the given deeply-nested hash, or return nil
|
119
|
+
# if key not found.
|
120
|
+
#
|
121
|
+
# Worth noting is that Hash#default_proc is *not* used, as the intricacies
|
122
|
+
# of implementation would lead to all sorts of terrible surprises.
|
123
|
+
#
|
124
|
+
# @param hsh [Hash<#to_s,Object>]
|
125
|
+
# @param smash_key [#to_s]
|
126
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
127
|
+
# @return [Object]
|
128
|
+
def smash_get(hsh, smash_key, options = {})
|
129
|
+
smash_fetch(hsh, smash_key, options) { nil }
|
130
|
+
end
|
131
|
+
|
132
|
+
# Given a smash hash, unflatten a subset by address, returning
|
133
|
+
# a *modified copy* of the original smash hash.
|
134
|
+
#
|
135
|
+
# @overload expand(smash_hsh, smash_key, options = {}, &block)
|
136
|
+
# @param smash_hsh [Hash{String=>Object}]
|
137
|
+
# @param smash_key [String]
|
138
|
+
# @param options (see Flatten::UtilityMethods#smash)
|
139
|
+
# @return [Object]
|
140
|
+
#
|
141
|
+
# @example
|
142
|
+
# ~~~ ruby
|
143
|
+
# smash = {'a.b' => 2, 'a.c.d' => 4, 'a.c.e' => 3, 'b.f' => 4}
|
144
|
+
# Flatten::expand(smash, 'a.c')
|
145
|
+
# # => {'a.b' => 2, 'a.c' => {'d' => 4, 'e' => 3}, 'b.f' => 4}
|
146
|
+
# ~~~
|
147
|
+
def expand(smash_hsh, smash_key, *args)
|
148
|
+
# if smash_hsh includes our key, its value is already expanded.
|
149
|
+
return smash_hsh if smash_hsh.include?(smash_key)
|
150
|
+
|
151
|
+
options = (args.last.kind_of?(Hash) ? args.pop : {})
|
152
|
+
separator = options.fetch(:separator, DEFAULT_SEPARATOR)
|
153
|
+
pattern = /\A#{Regexp.escape(smash_key)}#{Regexp.escape(separator)}/i
|
154
|
+
|
155
|
+
match = {}
|
156
|
+
unmatch = {}
|
157
|
+
smash_hsh.each do |k, v|
|
158
|
+
if pattern =~ k
|
159
|
+
sk = k.gsub(pattern, '')
|
160
|
+
match[sk] = v
|
161
|
+
else
|
162
|
+
unmatch[k] = v
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
unmatch.update(smash_key => unsmash(match, options)) unless match.empty?
|
167
|
+
unmatch
|
168
|
+
end
|
169
|
+
|
170
|
+
# Given a smash hash, unflatten a subset by address *in place*
|
171
|
+
# (@see Flatten::UtilityMethods#expand)
|
172
|
+
def expand!(smash_hsh, *args)
|
173
|
+
smash_hsh.replace expand(smash_hsh, *args)
|
174
|
+
end
|
175
|
+
|
176
|
+
private
|
177
|
+
|
178
|
+
# Utility method for splitting a string by a separator into
|
179
|
+
# non-escaped parts
|
180
|
+
# @api private
|
181
|
+
# @param str [String]
|
182
|
+
# @param separator [String] single-character string
|
183
|
+
# @return [Array<String>]
|
184
|
+
def escaped_split(str, separator)
|
185
|
+
Separator[separator].split(str)
|
186
|
+
end
|
187
|
+
|
188
|
+
# Utility method for joining a pre-escaped string with a not-yet escaped
|
189
|
+
# string on a given separator, escaping the new part before joining.
|
190
|
+
# @api private
|
191
|
+
# @param pre_escaped_prefix [String]
|
192
|
+
# @param new_part [String] - will be escaped before joining
|
193
|
+
# @param separator [String] single-character string
|
194
|
+
# @return [String]
|
195
|
+
def escaped_join(pre_escaped_prefix, new_part, separator)
|
196
|
+
Separator[separator].join(pre_escaped_prefix, new_part)
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
extend UtilityMethods
|
201
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: flatten
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- l3x
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-08-16 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
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: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: 'A Ruby Gem to Flatten Nested JSON '
|
56
|
+
email:
|
57
|
+
- lex.sheehan@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- flatten.gemspec
|
72
|
+
- lib/flatten.rb
|
73
|
+
- lib/flatten/core_ext/kernel.rb
|
74
|
+
- lib/flatten/deprecations.rb
|
75
|
+
- lib/flatten/guard_methods.rb
|
76
|
+
- lib/flatten/separator.rb
|
77
|
+
- lib/flatten/string.rb
|
78
|
+
- lib/flatten/utility_methods.rb
|
79
|
+
- lib/flatten/version.rb
|
80
|
+
homepage: https://github.com/l3x/flatten
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.5.1
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: A Ruby Gem to Flatten Nested JSON
|
104
|
+
test_files: []
|