iteraptor 0.3.0

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: e7e10b8c75dbdfdf080bd009d3302062054f6cb4
4
+ data.tar.gz: 70b94b42f847c4d9479ba70bcd76281cbc070790
5
+ SHA512:
6
+ metadata.gz: 8f1620fbb853986345d3ff7fa5cba3d5e05c1eba74a884e757f4bfef7b50078be6987564f134335b217ef884b9b5a5f31472833b2de52b3d9a71967a4f13e9e1
7
+ data.tar.gz: b56d38d92a782908f55fdd5349218d1c03459a0f4dd17b87a7a6673a327e4a9da4dc44612c819e88a599fadd8f92d7e80ede3fdb8f06444ef9662ac40a42748f
data/.gitignore ADDED
@@ -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
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ Metrics/LineLength:
4
+ Max: 180
5
+
6
+ Style/HashSyntax:
7
+ EnforcedStyle: ruby19
8
+
9
+ Style/MethodDefParentheses:
10
+ Exclude:
11
+ - 'spec/**/*'
12
+ - 'test/**/*'
13
+ - 'lib/**/*'
14
+
15
+ Style/AsciiIdentifiers:
16
+ Exclude:
17
+ - 'spec/**/*'
18
+ - 'test/**/*'
19
+ - 'lib/**/*'
20
+
21
+ Style/VariableName:
22
+ Exclude:
23
+ - 'spec/**/*'
24
+ - 'test/**/*'
25
+ - 'lib/**/*'
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,40 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config`
3
+ # on 2016-04-15 10:45:36 +0200 using RuboCop version 0.39.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: 1
10
+ Style/Documentation:
11
+ Exclude:
12
+ - 'spec/**/*'
13
+ - 'test/**/*'
14
+ - 'lib/**/*'
15
+
16
+ # Offense count: 1
17
+ # Cop supports --auto-correct.
18
+ Style/MutableConstant:
19
+ Exclude:
20
+ - 'lib/iteraptor/version.rb'
21
+
22
+ # Offense count: 2
23
+ # Cop supports --auto-correct.
24
+ # Configuration parameters: PreferredDelimiters.
25
+ Style/PercentLiteralDelimiters:
26
+ Exclude:
27
+ - 'iteraptor.gemspec'
28
+
29
+ # Offense count: 20
30
+ # Cop supports --auto-correct.
31
+ # Configuration parameters: EnforcedStyle, SupportedStyles, ConsistentQuotesInMultiline.
32
+ # SupportedStyles: single_quotes, double_quotes
33
+ Style/StringLiterals:
34
+ Enabled: false
35
+
36
+ # Offense count: 2
37
+ # Cop supports --auto-correct.
38
+ Style/UnneededPercentQ:
39
+ Exclude:
40
+ - 'iteraptor.gemspec'
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.8
4
+ - 2.2.0
5
+ 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 aleksei.matiushkin@kantox.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 iteraptor.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Aleksei Matiushkin
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,60 @@
1
+ # Iteraptor
2
+
3
+ [![Build Status](https://travis-ci.org/am-kantox/iteraptor.svg?branch=master)](https://travis-ci.org/am-kantox/iteraptor)
4
+ This small mixin allows the deep iteration / mapping of `Enumerable`s instances.
5
+
6
+ Adopted to be used with hashes/arrays. It **is not** intended to be used with
7
+ large objects.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'iteraptor'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install iteraptor
24
+
25
+ ## Usage
26
+
27
+
28
+
29
+ ### Iteration
30
+
31
+ ```ruby
32
+ λ = ->(parent, element) { puts ... }
33
+
34
+ [:a, b: {c: 42}].cada &λ
35
+ #⇒ 0 » :a
36
+ #⇒ 1 » {:b=>{:c=>42}}
37
+ #⇒ 1.b » {:c=>42}
38
+ #⇒ 1.b.c » 42
39
+
40
+ {a: 42, b: [:c, :d]}.cada &λ
41
+ #⇒ a » 42
42
+ #⇒ b » [:c, :d]
43
+ #⇒ b.0 » :c
44
+ #⇒ b.1 » :d
45
+ ```
46
+
47
+ ## Development
48
+
49
+ 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.
50
+
51
+ 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).
52
+
53
+ ## Contributing
54
+
55
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/iteraptor. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
56
+
57
+
58
+ ## License
59
+
60
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,24 @@
1
+ # encoding: UTF-8
2
+ require 'rubygems'
3
+
4
+ require 'bundler'
5
+ require 'bundler/setup'
6
+ require 'bundler/gem_tasks'
7
+
8
+ require 'rspec/core/rake_task'
9
+
10
+ begin
11
+ Bundler.setup(:default, :development, :test)
12
+ rescue Bundler::BundlerError => e
13
+ $stderr.puts e.message
14
+ $stderr.puts 'Run `bundle install` to install missing gems'
15
+ exit e.status_code
16
+ end
17
+
18
+ desc 'Tests'
19
+ RSpec::Core::RakeTask.new(:spec) do |spec|
20
+ spec.rspec_opts = '-Ispec'
21
+ # spec.rcov = true
22
+ end
23
+
24
+ task default: [:spec]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "iteraptor"
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
@@ -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
data/iteraptor.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'iteraptor/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'iteraptor'
8
+ spec.version = Iteraptor::VERSION
9
+ spec.authors = ['Aleksei Matiushkin']
10
+ spec.email = ['aleksei.matiushkin@kantox.com']
11
+
12
+ spec.summary = 'Iterator on steroids: allows thorough iteration through nested Enumerables'
13
+ spec.description = 'Adds several methods similar to Enumerable’s iterators to where it is included.'
14
+ spec.homepage = 'https://kantox.com'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes." unless spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+
22
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ spec.bindir = 'bin'
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.11'
28
+ spec.add_development_dependency 'rake', '~> 10.0'
29
+ spec.add_development_dependency 'rspec', '~> 3.0'
30
+ spec.add_development_dependency 'pry', '~> 0.10'
31
+ end
@@ -0,0 +1,8 @@
1
+ module Iteraptor
2
+ def attach_to klazz
3
+ klazz.send :include, Iteraptor
4
+ end
5
+ module_function :attach_to
6
+
7
+ [Array, Hash].each { |c| Iteraptor.attach_to c }
8
+ end
@@ -0,0 +1,3 @@
1
+ module Iteraptor
2
+ VERSION = "0.3.0"
3
+ end
data/lib/iteraptor.rb ADDED
@@ -0,0 +1,112 @@
1
+ require "iteraptor/version"
2
+
3
+ module Iteraptor
4
+ DELIMITER = '.'.freeze
5
+
6
+ def self.included base
7
+ raise "This module might be included into Enumerables only" unless base.ancestors.include? Enumerable
8
+ end
9
+
10
+ %i(cada mapa).each do |m|
11
+ define_method m do |root = nil, parent = nil, &λ|
12
+ return enum_for(m) unless λ
13
+ send_to = [Hash, Array, Enumerable].detect { |c| is_a? c }
14
+ send_to && send("#{m}_in_#{send_to.name.downcase}", root || self, parent, &λ)
15
+ end
16
+ end
17
+
18
+ # rubocop:disable Metrics/CyclomaticComplexity
19
+ # rubocop:disable Metrics/MethodLength
20
+ def segar filter
21
+ return enum_for(:segar) unless block_given?
22
+
23
+ cada.with_object({}) do |(parent, element), memo|
24
+ p = parent.split(DELIMITER)
25
+ if case filter
26
+ when String then p.include?(filter)
27
+ when Symbol then p.include?(filter.to_s)
28
+ when Regexp then p.any? { |key| key =~ filter }
29
+ when Array then parent.include?(filter.join(DELIMITER))
30
+ end
31
+ yield parent, element
32
+ memo[parent] = element
33
+ end
34
+ end
35
+ end
36
+ # rubocop:enable Metrics/MethodLength
37
+ # rubocop:enable Metrics/CyclomaticComplexity
38
+
39
+ private
40
+
41
+ ##############################################################################
42
+ ### cada
43
+ def cada_in_array root = nil, parent = nil
44
+ λ = Proc.new
45
+
46
+ each.with_index do |e, idx|
47
+ p = [parent, idx].compact.join(DELIMITER)
48
+
49
+ yield p, e
50
+
51
+ case e
52
+ when Iteraptor then e.cada(root, p, &λ)
53
+ when Enumerable then e.each(&λ.curry[p])
54
+ end
55
+ end
56
+ end
57
+ alias cada_in_enumerable cada_in_array
58
+
59
+ def cada_in_hash root = nil, parent = nil
60
+ λ = Proc.new
61
+
62
+ each do |k, v|
63
+ p = [parent, k].compact.join(DELIMITER)
64
+
65
+ yield p, v
66
+
67
+ case v
68
+ when Iteraptor then v.cada(root, p, &λ)
69
+ when Enumerable then v.each(&λ.curry[p])
70
+ end
71
+ end
72
+ end
73
+
74
+ ##############################################################################
75
+ ### mapa
76
+ def mapa_in_array root = nil, parent = nil
77
+ λ = Proc.new
78
+
79
+ map.with_index do |e, idx|
80
+ p = [parent, idx].compact.join(DELIMITER)
81
+
82
+ case e
83
+ when Iteraptor then e.mapa(root, p, &λ)
84
+ when Enumerable then e.map(&λ.curry[p])
85
+ else yield p, e
86
+ end
87
+ end
88
+ end
89
+ alias mapa_in_enumerable mapa_in_array
90
+
91
+ def mapa_in_hash root = nil, parent = nil
92
+ λ = Proc.new
93
+
94
+ map do |k, v|
95
+ p = [parent, k].compact.join(DELIMITER)
96
+
97
+ case v
98
+ when Iteraptor then [k, v.mapa(root, p, &λ)]
99
+ when Enumerable then [k, v.map(&λ.curry[p])]
100
+ else yield p, [k, v]
101
+ end
102
+ end.to_h
103
+ end
104
+
105
+ ##############################################################################
106
+ ### helpers
107
+ def leaf? e
108
+ [Iteraptor, Enumerable].none? { |c| e.is_a? c }
109
+ end
110
+ end
111
+
112
+ require "iteraptor/greedy"
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: iteraptor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Aleksei Matiushkin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-04-19 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: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.10'
69
+ description: Adds several methods similar to Enumerable’s iterators to where it is
70
+ included.
71
+ email:
72
+ - aleksei.matiushkin@kantox.com
73
+ executables:
74
+ - console
75
+ - setup
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".rubocop.yml"
82
+ - ".rubocop_todo.yml"
83
+ - ".travis.yml"
84
+ - CODE_OF_CONDUCT.md
85
+ - Gemfile
86
+ - LICENSE.txt
87
+ - README.md
88
+ - Rakefile
89
+ - bin/console
90
+ - bin/setup
91
+ - iteraptor.gemspec
92
+ - lib/iteraptor.rb
93
+ - lib/iteraptor/greedy.rb
94
+ - lib/iteraptor/version.rb
95
+ homepage: https://kantox.com
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.4.8
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: 'Iterator on steroids: allows thorough iteration through nested Enumerables'
119
+ test_files: []