depth 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4571967f84027109674890d8d99993df6515740d
4
+ data.tar.gz: 031c3763b9bf00568b76d70b6ef59f5bfcccfe4d
5
+ SHA512:
6
+ metadata.gz: 18b2fc8ca9c8b8145366ad48e1e7df6c9c36696c7ab2de5eb3728641d5f778eb5eb70f8dbeccf79003e8fd166e964c983449c24e1a0193930d6f8cb53c3af535
7
+ data.tar.gz: 2c9e9dd29645a4a35bcc4531bf46392318586a47502d631e01a849f0c598ad32612e939bf41255a65aac545eaa69e76d9b868b86ee352e7972cb242739217f6d
data/.gitignore ADDED
@@ -0,0 +1,16 @@
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
15
+ *.swo
16
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ ruby '2.1.3'
4
+
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Max Dupenois
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,3 @@
1
+ # Depth
2
+
3
+
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ require 'rspec/core/rake_task'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'lib'
8
+ t.libs << 'spec'
9
+ t.pattern = 'spec/**/*_spec.rb'
10
+ t.verbose = false
11
+ end
12
+
13
+
14
+ task default: :spec
data/depth.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'depth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "depth"
8
+ spec.version = Depth::VERSION
9
+ spec.authors = ["Max"]
10
+ spec.email = ["max.dupenois@gmail.com"]
11
+ spec.summary = %q{Depth is a utility gem for dealing with nested hashes and arrays}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/maxdupenois/depth"
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
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pry-byebug"
25
+ spec.add_development_dependency "simplecov"
26
+ end
@@ -0,0 +1,41 @@
1
+ module Depth
2
+ module Actions
3
+ def set(route, value)
4
+ route = RouteElement.convert_route(route)
5
+ object = route[0 ... -1].reduce(Traverser.new(hsh)) { |t, route_el|
6
+ t.next_or_create(route_el.key) { route_el.create }
7
+ }.object
8
+ object[route.last.key] = value
9
+ end
10
+
11
+ def find(route)
12
+ route = RouteElement.convert_route(route)
13
+ route.reduce(Traverser.new(hsh)) { |t, route_el|
14
+ t.next(route_el.key)
15
+ }.object
16
+ end
17
+
18
+ def alter(route, key: nil, value: nil)
19
+ return set(route, value) if key == nil
20
+ route = RouteElement.convert_route(route)
21
+ value = find(route) unless value
22
+ new_route = (route[0 ... -1] << RouteElement.convert(new_key))
23
+ set(new_route, value) # ensure it exists
24
+ old_key = route.last.key
25
+ return unless old_key != new_key
26
+ delete(route)
27
+ end
28
+
29
+ def delete(route)
30
+ route = RouteElement.convert_route(route)
31
+ traverser = route[0...-1].reduce(Traverser.new(hsh)) do |t, route_el|
32
+ t.next(route_el.key)
33
+ end
34
+ if traverser.array?
35
+ traverser.object.delete_at(route.last.key)
36
+ elsif traverser.hash?
37
+ traverser.object.delete(route.last.key)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,10 @@
1
+ module Depth
2
+ class ComplexHash
3
+ include Depth::Actions
4
+ include Depth::Enumerable
5
+ attr_reader :base
6
+ def initialize(base = {})
7
+ @base = base
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,82 @@
1
+ module Depth
2
+ module Enumerable
3
+
4
+ def each_with_object(object, &block)
5
+ each do |key, fragment|
6
+ block.call(key, fragment, object)
7
+ end
8
+ object
9
+ end
10
+
11
+ def reduce(memo, &block)
12
+ each do |key, fragment|
13
+ memo = block.call(memo, key, fragment)
14
+ end
15
+ memo
16
+ end
17
+
18
+ def map_keys!(&block)
19
+ @base = map_keys(&block).base
20
+ end
21
+
22
+ def map!(&block)
23
+ @base = map(&block).base
24
+ end
25
+
26
+ def map_keys_and_values!(&block)
27
+ @base = map_keys_and_values(&block).base
28
+ end
29
+
30
+ def map_keys(&block)
31
+ map_keys_and_values do |key, fragment|
32
+ [block.call(key), fragment]
33
+ end
34
+ end
35
+
36
+ # Convention is that only values are mapped
37
+ def map(&block)
38
+ map_keys_and_values do |key, fragment|
39
+ [key, block.call(fragment)]
40
+ end
41
+ end
42
+
43
+ def map_keys_and_values(&block)
44
+ node_map do |node, new_q|
45
+ orig_key = node.parent_key
46
+ existing = new_q.find(node.route)
47
+ orig_fragment = existing.nil? ? node.fragment : existing
48
+ next [orig_key, orig_fragment] unless node.parent.hash?
49
+ block.call(orig_key, orig_fragment)
50
+ end
51
+ end
52
+
53
+ def each(&block)
54
+ enumerate { |node| block.call(node.parent_key, node.fragment) }
55
+ end
56
+
57
+ private
58
+
59
+ def node_map(&block)
60
+ new_q = ComplexHash.new(base.class.new)
61
+ enumerate do |node|
62
+ key, val = block.call(node, new_q)
63
+ new_q.alter(node.route, key: key, value: val)
64
+ end
65
+ new_q
66
+ end
67
+
68
+ def enumerate
69
+ root = Node.new(nil, nil, query)
70
+ current = root
71
+ begin
72
+ if current.next?
73
+ current = current.next
74
+ elsif !current.root?
75
+ yield(current)
76
+ current = current.parent
77
+ end
78
+ end while !current.root? || current.next?
79
+ root.fragment
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,67 @@
1
+ module Depth
2
+ module Enumeration
3
+ class Node < Struct.new(:parent, :parent_index, :fragment)
4
+ def current_index
5
+ @current_index ||= 0
6
+ end
7
+
8
+ def route
9
+ route = []
10
+ current = self
11
+ while(!current.root?)
12
+ route << RouteElement.new(current.parent_key, fragment_type)
13
+ current = current.parent
14
+ end
15
+ route.reverse
16
+ end
17
+
18
+ def next
19
+ if array?
20
+ val = fragment[current_index]
21
+ else
22
+ val = fragment[fragment.keys[current_index]]
23
+ end
24
+ Node.new(self, current_index, val).tap { @current_index += 1 }
25
+ end
26
+
27
+ def parent_key
28
+ return nil unless parent.enumerable? # root
29
+ return parent_index if parent.array?
30
+ parent.fragment.keys[parent_index]
31
+ end
32
+
33
+ def next?
34
+ return false if leaf?
35
+ current_index < fragment.count
36
+ end
37
+
38
+ def array?
39
+ fragment.is_a?(Array)
40
+ end
41
+
42
+ def hash?
43
+ fragment.is_a?(Hash)
44
+ end
45
+
46
+ def fragment_type
47
+ {
48
+ Array => :array,
49
+ Hash => :hash
50
+ }.fetch(fragment.class, :leaf)
51
+ end
52
+
53
+ def enumerable?
54
+ # ignore other types for the moment
55
+ array? || hash?
56
+ end
57
+
58
+ def leaf?
59
+ !enumerable?
60
+ end
61
+
62
+ def root?
63
+ parent.nil?
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,35 @@
1
+ module Depth
2
+ class RouteElement
3
+ attr_reader :key_or_index, :type
4
+ alias_method :key, :key_or_index
5
+ alias_method :index, :key_or_index
6
+ def initialize(key_or_index, type: :hash)
7
+ @key_or_index = key_or_index
8
+ @type = type.to_sym
9
+ end
10
+
11
+ def create
12
+ { hash: {}, array: [], leaf: nil }.fetch(type, nil)
13
+ end
14
+
15
+ class << self
16
+ def convert_route(route_array)
17
+ Array(route_array).map { |el| convert(el) }
18
+ end
19
+
20
+ def convert(el)
21
+ return el if el.is_a?(RouteElement)
22
+ case el
23
+ when Array
24
+ type = el.count > 1 ? el[1] : :hash
25
+ RouteElement.new(route_el[0], type: type)
26
+ when Hash
27
+ key_or_index = el.fetch(:key, el.fetch(:index))
28
+ RouteElement.new(key_or_index, type: el.fetch(:type, :hash))
29
+ else
30
+ RouteElement.new(el)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,22 @@
1
+ module Depth
2
+ class Traverser < Struct.new(:object)
3
+ def array?
4
+ object.is_a?(Array)
5
+ end
6
+
7
+ def hash?
8
+ object.is_a?(Hash)
9
+ end
10
+
11
+ def next(key_or_index)
12
+ return Traverser.new(nil) if object.nil?
13
+ Traverser.new(object[key_or_index])
14
+ end
15
+
16
+ def next_or_create(key_or_index, &block)
17
+ return Traverser.new(nil) if object.nil?
18
+ object[key_or_index] = block.call if object[key_or_index].nil?
19
+ Traverser.new(object[key_or_index])
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module Depth
2
+ VERSION = '0.0.1'
3
+ end
data/lib/depth.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'depth/version'
2
+
3
+ module Depth
4
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: depth
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Max
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-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
+ - !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: pry-byebug
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
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: ''
84
+ email:
85
+ - max.dupenois@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - depth.gemspec
96
+ - lib/depth.rb
97
+ - lib/depth/actions.rb
98
+ - lib/depth/complex_hash.rb
99
+ - lib/depth/enumeration/enumerable.rb
100
+ - lib/depth/enumeration/node.rb
101
+ - lib/depth/route_element.rb
102
+ - lib/depth/traverser.rb
103
+ - lib/depth/version.rb
104
+ homepage: https://github.com/maxdupenois/depth
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.2.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Depth is a utility gem for dealing with nested hashes and arrays
128
+ test_files: []