pred 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: 5402c239ba1ad513309622fe4c8e607d303f69b6
4
+ data.tar.gz: b54b8168ed5d061d8b8c9dfdf6d94cb75a48d184
5
+ SHA512:
6
+ metadata.gz: d4d92f7f794c12d5199e9feeee6acab2a6d92f6de598b217ea0805dfc11e5fa889c4298618324dcfa8c98330a1c2a7ca64c52aa7945621166291ba4bb8594671
7
+ data.tar.gz: 911faa9655cd7cdc56819512b4f730c70188097e7fe4d76c28a6bc47dc9ae5319c918ed99d95da870620feef7d074584cde7ca896a36e92cb22cded082d11d65
@@ -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 pred.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 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,64 @@
1
+ # Pred
2
+
3
+ This creates methods `#pred` and `#pred!` to target multiple base Objects.
4
+ The plan is to include this gem in future upgrades on several gems I am working on.
5
+ This basically implements the inverse of `#succ` and `#succ!`, which will be imported
6
+ on the appropriate basic ruby types. The goal is to create a `Range` super class that
7
+ works forwards as well as backwards. This extended `Range` is needed on an `Array` super class
8
+ that uses lists of integers, ranges, and backward ranges to work within the context of the access operators `:[]` and `:[]=`.
9
+ I expect this to snowball on other projects as well.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'pred'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install pred
26
+
27
+ ## Usage
28
+
29
+ This method updates String, Integer, and Symbol to include `#pred` as one of its instance methods.
30
+ The `#pred` method is simply the inverse of `#succ`. The method `#pred!` is implemented only on `String` due to its mutating abilities.
31
+ Integer is trivial, but String is a mother bear. There are many tricky edge cases on how `#succ`
32
+ works, and finding an inverse operation proved harder than first glance. See the code below to see why:
33
+
34
+ ```ruby
35
+ "az".succ # "ba"
36
+ "a__z".succ # "b__a" ... "b__a".pred should give me "a__z"
37
+ "z_z".succ # "aa_a" ... inserts carry-over at front of string
38
+ "0_z".succ # "0_aa" ... ignores digits if separated by underscore
39
+ "a9z".succ # "b0a" ... but if contiguous, rolls over as expected
40
+ "a99z".succ # "b00a" ... things look non-numeric here
41
+ "9z".succ # "10a" ... looks more numeric here as 9+1==10
42
+ "az_Z9Z_z".succ # "ba_A0A_a" ... numbers touching letters work as expected
43
+ "az9_zZ9z".succ # "az9_aaA0a" ... but not if they touch something else
44
+ "a_9z".succ # "a_10a" ... either side breaks the connection
45
+ "`".succ # "a" ... edge case "a".pred should equal "`"
46
+ ```
47
+
48
+ Now we have a balance of force in the ruby universe as `#pred` is the ying of the yang `#succ`.
49
+ The name `pred` is shorthand for predecessor as `succ` is shorthand for successor.
50
+ If you create the method `#succ` on one of your classes, you should also create its counterpart `#pred`.
51
+
52
+ ## Development
53
+
54
+ 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.
55
+ To install this gem onto your local machine, run `bundle exec rake install`.
56
+
57
+ ## Contributing
58
+
59
+ I need to control this for the time being
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
@@ -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 "pred"
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,210 @@
1
+ require "pred/version"
2
+
3
+ module Pred
4
+ def self.letter_digit?(ch)
5
+ return true if ('a'..'z').cover? ch
6
+ return true if ('A'..'Z').cover? ch
7
+ return true if ('0'..'9').cover? ch
8
+ false
9
+ end
10
+
11
+ def self.letter?(ch)
12
+ return true if ('a'..'z').cover? ch
13
+ return true if ('A'..'Z').cover? ch
14
+ false
15
+ end
16
+
17
+ def self.upper?(ch)
18
+ ('A'..'Z').cover? ch
19
+ end
20
+
21
+ def self.lower?(ch)
22
+ ('a'..'z').cover? ch
23
+ end
24
+
25
+ def self.digit?(ch)
26
+ ('0'..'9').cover? ch
27
+ end
28
+
29
+ def self.count_digit?(ch)
30
+ ('0'..'9').cover? ch
31
+ end
32
+
33
+ def self.decrements?(str)
34
+ str.each_char do |ch|
35
+ return true if letter_digit? ch
36
+ end
37
+ return false
38
+ end
39
+
40
+ def self.decrementals?(str)
41
+ last = str[-1]
42
+ str = str[0..-2]
43
+ mode = true
44
+ if digit? last
45
+ while !str.empty? do
46
+ if mode
47
+ # if last=='0'
48
+ # if '1'==str[-1] # we have the weird edge case
49
+ # byebug
50
+ # y = :not # can't fix it here ... but stop and look around for fix
51
+ # end
52
+ # end
53
+ return true if letter_digit?(str[-1])
54
+ mode = false
55
+ else
56
+ if letter_digit?(str[-1])
57
+ return digit? str[-1]
58
+ end
59
+ end
60
+ str = str[0..-2]
61
+ end
62
+ elsif letter? last
63
+ while !str.empty? do
64
+ if mode
65
+ return true if letter_digit?(str[-1])
66
+ mode = false
67
+ else
68
+ if letter_digit?(str[-1])
69
+ return letter? str[-1]
70
+ end
71
+ end
72
+ str = str[0..-2]
73
+ end
74
+ else
75
+ return decrementals?(str) unless str.empty?
76
+ end
77
+ return false
78
+ end
79
+
80
+
81
+ # def self.inc(ch)
82
+ # (ch.ord+1).chr(Encoding::UTF_8)
83
+ # end
84
+
85
+ def self.dec(ch)
86
+ ii = (ch.ord-1)
87
+ ii = 0 if ii < 0
88
+ (ii).chr(Encoding::UTF_8)
89
+ end
90
+
91
+ def self.decrement_char(ch)
92
+ if upper?(ch)
93
+ t = dec(ch)
94
+ return [:done, t] if upper?(t)
95
+ return [:borrow, 'Z', t, :upper? ]
96
+ elsif lower?(ch)
97
+ t = dec(ch)
98
+ return [:done, t] if lower?(t)
99
+ return [:borrow, 'z', t, :lower? ]
100
+ elsif digit?(ch)
101
+ t = dec(ch)
102
+ return [:done, t] if digit?(t)
103
+ return [:borrow, '9', t, :digit? ]
104
+ else
105
+ t = dec(ch)
106
+ return [:ignore, t]
107
+ end
108
+ end
109
+
110
+ def self.rcur_decrement_string(str, ref, pos)
111
+ if pos < 0 # drop any pending stuff now even if borrow mode active
112
+ return ref[:rtn].reverse
113
+ end
114
+ if ref[:mode] == :start
115
+ rslt = decrement_char(str[pos])
116
+ if rslt.first == :done
117
+ ref[:rtn] += rslt[1]
118
+ ref[:mode] = :done
119
+ return rcur_decrement_string(str, ref, pos-1)
120
+ elsif rslt.first == :borrow
121
+ if decrementals? str[0..pos]
122
+ ref[:mode] = :borrow
123
+ ref[:rtn] += rslt[1]
124
+ else # drop one of the chars
125
+ ref[:mode] = :done
126
+ end
127
+ return rcur_decrement_string(str, ref, pos-1)
128
+ else
129
+ if decrements? str[0..pos]
130
+ ref[:rtn] += str[pos]
131
+ ref[:mode] = :start
132
+ else
133
+ ref[:rtn] += rslt[1]
134
+ ref[:mode] = :done
135
+ end
136
+ return rcur_decrement_string(str, ref, pos-1)
137
+ end
138
+ elsif ref[:mode] == :done
139
+ ref[:rtn] += str[pos]
140
+ return rcur_decrement_string(str, ref, pos-1)
141
+ elsif ref[:mode] == :borrow
142
+ if letter_digit? str[pos]
143
+ rslt = decrement_char(str[pos])
144
+ if rslt.first == :done
145
+ # if (rslt[1]=='0')
146
+ # byebug # special edge case here ... nothing left to borrow if digit
147
+ # y = :not
148
+ # end
149
+ ref[:rtn] += rslt[1] unless (rslt[1]=='0') # added this qualifier ...
150
+ ref[:mode] = :done
151
+ return rcur_decrement_string(str, ref, pos-1)
152
+ elsif rslt.first == :borrow
153
+ if decrementals? str[0..pos]
154
+ ref[:mode] = :borrow
155
+ ref[:rtn] += rslt[1]
156
+ else # drop one of the chars
157
+ ref[:mode] = :done
158
+ end
159
+ return rcur_decrement_string(str, ref, pos-1)
160
+ end
161
+ else
162
+ ref[:rtn] += str[pos]
163
+ end
164
+ return rcur_decrement_string(str, ref, pos-1)
165
+ else
166
+ ref[:rtn] += str[pos]
167
+ return rcur_decrement_string(str, ref, pos-1)
168
+ end
169
+ end
170
+
171
+ def self.decrement_string(str, ref={:mode=> :start, :rtn=> "" })
172
+ return "" if str.empty?
173
+ if str.length==1
174
+ return "" if str.ord==0
175
+ end
176
+ return str[0..-2] + dec(str[-1]) unless decrements?(str)
177
+ return rcur_decrement_string(str, ref, str.length-1)
178
+ end
179
+ end
180
+
181
+ class String
182
+ def pred
183
+ Pred::decrement_string(self)
184
+ end
185
+ def pred!
186
+ replace pred
187
+ end
188
+ end
189
+
190
+ class Symbol
191
+ def pred
192
+ Pred::decrement_string(self.to_s).to_sym
193
+ end
194
+ end
195
+
196
+ class Integer # move this to top
197
+ def pred
198
+ self - 1
199
+ end
200
+ end
201
+
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+
210
+
@@ -0,0 +1,3 @@
1
+ module Pred
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pred/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pred"
8
+ spec.version = Pred::VERSION
9
+ spec.authors = ["Bryan Colvin"]
10
+ spec.email = ["bryan@bdlsys.com"]
11
+
12
+ spec.summary = %q{implements #pred, the inverse of #succ}
13
+ spec.description = %q{Implements #pred, the inverse of #succ.
14
+ The goal is to include this gem on multiple projects and to gift extended Range objects with proper reverse functionality.}
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency 'byebug', '~> 5.0', '>= 5.0.0'
26
+
27
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pred
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: 2018-01-10 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
+ Implements #pred, the inverse of #succ.
77
+ The goal is to include this gem on multiple projects and to gift extended Range objects with proper reverse functionality.
78
+ email:
79
+ - bryan@bdlsys.com
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - ".gitignore"
85
+ - ".rspec"
86
+ - ".travis.yml"
87
+ - CODE_OF_CONDUCT.md
88
+ - Gemfile
89
+ - LICENSE.txt
90
+ - README.md
91
+ - Rakefile
92
+ - bin/console
93
+ - bin/setup
94
+ - lib/pred.rb
95
+ - lib/pred/version.rb
96
+ - pred.gemspec
97
+ homepage:
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.5.1
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: 'implements #pred, the inverse of #succ'
121
+ test_files: []