blockify 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8af64338fc8a469e61f4286ef0703d40d3368365
4
+ data.tar.gz: 667d48278b272ed5910fb3fe8afc777d13bcc2c9
5
+ SHA512:
6
+ metadata.gz: f6b6f7afec83d58d134b771598b9cb69d9424842d9b1f5eaa7038dfd765757186127843300c287cfbb39dc6c1015337525281ae155473e40b4d0af9eac76ee13
7
+ data.tar.gz: e06d7a2a90c69ab54f032da75392a260abc49b34d61a139227a9e6a60404c2483d99f3c0506e42915d8f1bc40470f11ccc38c66b8f80112a6334bb20a2fe2514
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at bryan@bdlsys.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in blockify.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Bryan Colvin
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.
@@ -0,0 +1,142 @@
1
+ # Blockify
2
+
3
+ The `blockify` gem solves some of the problems associated with complex hierarchical nested arrays and hashes.
4
+ It is possible to represent an HTML file as a series of Arrays and Hashes with very deep levels of Hashes within Arrays within Hashes.
5
+ Traversing such a tree is tricky at best. The methods of the `blockify` gem are included in Array, and Hash using a naming convention
6
+ that should not compete with anything. We don't need to concern ourselves with the complexity of the structure and can instead
7
+ focus on the base elements wherever they may be.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'blockify'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install blockify
24
+
25
+ ## Usage
26
+
27
+ This first release includes a module that inserts itself with the Array and Hash classes.
28
+ This same module could be used to service custom container classes as well so long as they look Array-like or Hash-like.
29
+ As a practical point of view, examine the following code:
30
+
31
+ ```ruby
32
+ require 'blockify'
33
+
34
+ nested_thingee = [0,1,2,[3,4,5,[6,7,8,[9,10,11,12,[13,{:a=>[16,17],:b=>[18,19,20]}]]]],14,15]
35
+ ary = nested_thingee.stringify_elements
36
+ # ary == ["0","1","2",["3","4","5",["6","7","8",["9","10","11","12",["13",{:a=>["16","17"], :b=>["18","19","20"]}]]]],"14","15"]
37
+ ```
38
+ We don't need to worry about what the structure is, we can traverse the entire system with a single call.
39
+ Note that the returned structure is identical to the original.
40
+ The `blockify` gem uses recursion to step through the entire structure. If we peek inside the `#stringify_elements` method we see this:
41
+
42
+ ```ruby
43
+ def stringify_elements
44
+ blockify_elements {|elm| elm.to_s}
45
+ end
46
+ ```
47
+ The `#blockify_elements` method travels through the structure and calls our block whenever it encounters an element that is not an Array or a Hash.
48
+ Whatever the block returns, the element in question will be modified with that result. In this case, each element is converted to a string.
49
+ All the recursion magic is performed by the gem.
50
+
51
+ Another tool is an element locator. This is best shown by example as follows:
52
+
53
+ ```ruby
54
+ require 'blockify'
55
+
56
+ nested_thingee = [0,1,2,[3,4,5,[6,7,8,[9,10,11,12,[13,{:a=>[16,17],:b=>[18,19,20]}]]]],14,15]
57
+ path = nested_thingee.find_element_path { |t| t == 17 } # path == [3, 3, 3, 4, 1, :a, 1]
58
+ elm = nested_thingee[3][3][3][4][1][:a][1] # elm == 17
59
+
60
+ # or easier ...
61
+ elm = nested_thingee.path_get path # elm == 17
62
+
63
+ # and we can change it too!
64
+ old = nested_thingee.path_put "Fred" path # old == 17
65
+ elm = nested_thingee.path_get path # elm =="Fred"
66
+ # nested_thingee == [0,1,2,[3,4,5,[6,7,8,[9,10,11,12,[13,{:a=>[16,"Fred"],:b=>[18,19,20]}]]]],14,15]
67
+ ```
68
+ It is up to your imagination what you put in the block. When the block returns true, the recursion is done, and you get the path.
69
+ Each method in the `blockify` gem is imported into both `Hash` and `Array`.
70
+ The gem also includes three access methods that utilize the returned path. The above example shows two of them called `#path_put` and `#path_get`.
71
+ Instead of searching for the first find, we have a means of finding every match as well; this is called: `#find_element_paths`.
72
+ This methods returns an array of paths where each path is an array of indicies.
73
+ We can access everything with `#paths_get` which will return an array of every found element.
74
+ See the example below:
75
+
76
+ ```ruby
77
+ require 'blockify'
78
+
79
+ nested_thingee = [0,1,2,[13,4,15,[6,7,8,[9,10,11,12,[3,{:a=>[16,17],:b=>[18,19,20]}]]]],14,5]
80
+ paths = nested_thingee.find_element_paths { |t| (5..15).include? t }
81
+ paths.first # [3,0]
82
+ paths.last # [5]
83
+ paths[5] # [3, 3, 3, 0]
84
+ nested_thingee.path_get paths[5] # 9
85
+ nested_thingee.paths_get paths # [13, 15, 6, 7, 8, 9, 10, 11, 12, 14, 5]
86
+ ```
87
+
88
+ One of the methods of of the `blockify` gem utilizes the `#find_element_path` is called `#includify?`.
89
+ This is defined as follows:
90
+
91
+ ```ruby
92
+ def includify?(search_string)
93
+ path = find_element_path { |elm| elm.to_s.include? search_string }
94
+ !path.empty?
95
+ end
96
+ ```
97
+
98
+ When you have a deeply nested array_hash system, the standard `#include?` method is no help.
99
+ Instead of looking one-level deep for an exact match, `#includify?` examines each element
100
+ until it finds what it is looking for. Internally, we are looking for a substring on one of the elements.
101
+ See the example below:
102
+
103
+ ```ruby
104
+ require 'blockify'
105
+
106
+ nasty_array_hash = [{:quick=>{:a=>"tool", :b=>["xray","tent"]}, :quicker=>{:a=>:mess}, "green"=>"fox in socks", :empty=>[{}]},"fred"]
107
+
108
+ nasty_array_hash.includify? "tool" # true
109
+ nasty_array_hash.includify? "mess" # true
110
+ nasty_array_hash.includify? "quick" # false, :quick is a key, not an element
111
+ nasty_array_hash.includify? "green" # false, "green" is a key, not an element
112
+ nasty_array_hash.includify? "fox" # true, "fox" is a substring of "fox in socks"
113
+ ep = nasty_array_hash.find_element_path { |elm| elm.to_s.include? "fox"} # [0, "green"]
114
+ str = nasty_array_hash.path_get ep # str == "fox in socks"
115
+
116
+ ```
117
+
118
+ The current methods for this first release are as follows:
119
+
120
+ ```ruby
121
+ # new_array_hash = ary_hash.blockify_elements { |elm| some_expression_using_elm_that_gets_saved_to_elm }
122
+ # ary_hash.blockify_elements! { your_block_here } # ... self-modified ary_hash
123
+ # path_array = ary_hash.find_element_path { |elm| some_true_false_method_here_operating_on_elm }
124
+ # result = ary_hash.stringify_elements # creates same structure with elements converted to strings
125
+ # ary_hash.stringify_elements! # self-modified version of the above
126
+ # result = ary_hash.inspectify_elements # all elements replaced with a call to #inspect
127
+ # ary_hash.inspectify_elements! # self-modified version of the above
128
+ # ary_hash.includify? substring # true if substring is found in one of the elements
129
+ # ary_hash.find_element_paths { ... } # returns array of found paths
130
+ # ary_hash.path_get path_array # returns the element or structure found by the path array ... path_get([]) returns the entire structure
131
+ # ary_hash.paths_get array_paths # paths looks like this: [[],[],[],[],...] returns an array of each path_get
132
+ # ary_hash.path_put(data, path) # replaces current path element with data, where path is an ordered list of keys
133
+ ```
134
+
135
+ ## Development
136
+
137
+ I need to control this for the time being, so stay tuned! I will add more goodies in later releases.
138
+
139
+ ## License
140
+
141
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
142
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "blockify"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'blockify/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "blockify"
8
+ spec.version = Blockify::VERSION
9
+ spec.authors = ["Bryan Colvin"]
10
+ spec.email = ["bryan@bdlsys.com"]
11
+
12
+ spec.summary = %q{Hierarchical nested Hash and Array access tool}
13
+ spec.description = %q{The `blockify` gem solves some of the problems associated with complex hierarchical nested arrays and hashes.
14
+ It is possible to represent an HTML file as a series of Arrays and Hashes with very deep levels of Hashes within Arrays within Hashes.
15
+ Traversing such a tree is tricky at best. The methods of the `blockify` gem are included in Array, and Hash using a naming convention
16
+ that should not compete with anything. We don't need to concern ourselves with the complexity of the structure and can instead
17
+ focus on the base elements wherever they may be.}
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency 'byebug', '~> 5.0', '>= 5.0.0'
29
+ end
@@ -0,0 +1,150 @@
1
+ require "blockify/version"
2
+
3
+ module Blockify
4
+ def blockify_elements(&block) # blockify
5
+ if self.respond_to? :each_pair
6
+ hash = {}
7
+ self.each_pair do |key,val|
8
+ if val.respond_to? :each
9
+ hash[key] = val.blockify_elements &block
10
+ else
11
+ hash[key] = block.call(val)
12
+ end
13
+ end
14
+ return hash
15
+ elsif self.respond_to? :each
16
+ ary = []
17
+ self.each do |val|
18
+ if val.respond_to? :each
19
+ ary.push val.blockify_elements &block
20
+ else
21
+ ary.push block.call(val)
22
+ end
23
+ end
24
+ return ary
25
+ end
26
+ return self # should never get here
27
+ end
28
+
29
+ def find_element_path(path=[],done=[false], &block)
30
+ if self.respond_to? :each_pair
31
+ self.each_pair do |key,val|
32
+ if val.respond_to? :each
33
+ path.push key
34
+ val.find_element_path(path, done, &block)
35
+ return path if done.first
36
+ path.pop
37
+ else
38
+ if block.call(val)
39
+ path.push key
40
+ done[0]=true
41
+ return path
42
+ end
43
+ end
44
+ end
45
+ elsif self.respond_to? :each
46
+ idx = 0
47
+ self.each do |val|
48
+ if val.respond_to? :each
49
+ path.push idx
50
+ val.find_element_path(path, done, &block)
51
+ return path if done.first
52
+ path.pop
53
+ else
54
+ if block.call(val)
55
+ path.push idx
56
+ done[0]=true
57
+ return path
58
+ end
59
+ end
60
+ idx += 1
61
+ end
62
+ end # if-else
63
+ return path
64
+ end
65
+
66
+ def find_element_paths(path=[],paths=[], &block)
67
+ if self.respond_to? :each_pair
68
+ self.each_pair do |key,val|
69
+ if val.respond_to? :each
70
+ path.push key
71
+ val.find_element_paths(path, paths, &block)
72
+ path.pop
73
+ else
74
+ if block.call(val)
75
+ path.push key
76
+ paths.push path.dup
77
+ path.pop
78
+ end
79
+ end
80
+ end
81
+ elsif self.respond_to? :each
82
+ idx = 0
83
+ self.each do |val|
84
+ if val.respond_to? :each
85
+ path.push idx
86
+ val.find_element_paths(path, paths, &block)
87
+ path.pop
88
+ else
89
+ if block.call(val)
90
+ path.push idx
91
+ paths.push path.dup
92
+ path.pop
93
+ end
94
+ end
95
+ idx += 1
96
+ end
97
+ end # if-else
98
+ return paths
99
+ end
100
+
101
+ def path_get(path)
102
+ item = self
103
+ path.each do |idx|
104
+ item = item[idx]
105
+ end
106
+ return item
107
+ end
108
+
109
+ def paths_get(paths)
110
+ rtn = []
111
+ paths.each do |path|
112
+ rtn.push path_get(path)
113
+ end
114
+ return rtn
115
+ end
116
+
117
+ def path_put(val,path)
118
+ idx = path.pop
119
+ con = path_get(path)
120
+ rtn = con[idx]
121
+ con[idx]=val
122
+ path.push idx # put back
123
+ return rtn
124
+ end
125
+
126
+ def includify?(search_string)
127
+ path = find_element_path { |elm| elm.to_s.include? search_string }
128
+ !path.empty?
129
+ end
130
+
131
+ def blockify_elements!(&block)
132
+ replace blockify_elements &block
133
+ end
134
+ def stringify_elements
135
+ blockify_elements {|t| t.to_s}
136
+ end
137
+ def stringify_elements!
138
+ replace stringify_elements
139
+ end
140
+ def inspectify_elements
141
+ blockify_elements {|t| t.inspect}
142
+ end
143
+ def inspectify_elements!
144
+ replace inspectify_elements
145
+ end
146
+
147
+ end # module
148
+
149
+ Array.include Blockify
150
+ Hash.include Blockify
@@ -0,0 +1,3 @@
1
+ module Blockify
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: blockify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bryan Colvin
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-27 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.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
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
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: 5.0.0
65
+ type: :development
66
+ prerelease: false
67
+ version_requirements: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - "~>"
70
+ - !ruby/object:Gem::Version
71
+ version: '5.0'
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 5.0.0
75
+ description: |-
76
+ The `blockify` gem solves some of the problems associated with complex hierarchical nested arrays and hashes.
77
+ It is possible to represent an HTML file as a series of Arrays and Hashes with very deep levels of Hashes within Arrays within Hashes.
78
+ Traversing such a tree is tricky at best. The methods of the `blockify` gem are included in Array, and Hash using a naming convention
79
+ that should not compete with anything. We don't need to concern ourselves with the complexity of the structure and can instead
80
+ focus on the base elements wherever they may be.
81
+ email:
82
+ - bryan@bdlsys.com
83
+ executables: []
84
+ extensions: []
85
+ extra_rdoc_files: []
86
+ files:
87
+ - ".gitignore"
88
+ - ".rspec"
89
+ - ".travis.yml"
90
+ - CODE_OF_CONDUCT.md
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - bin/console
96
+ - bin/setup
97
+ - blockify.gemspec
98
+ - lib/blockify.rb
99
+ - lib/blockify/version.rb
100
+ homepage:
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.5.1
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: Hierarchical nested Hash and Array access tool
124
+ test_files: []