ioughta 0.1.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: 9bd946f7ba2d07976031712406bf90ea7c845b6d
4
+ data.tar.gz: 323d3409b17d8db6c1e64a7c9bbd664f3d524036
5
+ SHA512:
6
+ metadata.gz: 5efc1fd72d5e450ed3e6ca13022b6d69ded722c9fde75e294bcf5cafc588c2802152b46fef58e651b90f20cb487208692e344f33078072289e9181c78d8069b3
7
+ data.tar.gz: 162ddfcede4fe8d9b0182f9a020e6ade3aa126fd030978786fa1a9546a6390db689a7e51df4ccff017e8437b2b0bd69d60c6bb3f962578501e918c42d21ddf2e
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/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.2.5
5
+ - 2.3.1
6
+ - ruby-head
7
+ - jruby-9.1.5.0
8
+ - jruby-head
9
+ before_install: gem install bundler -v 1.13.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Mike Pastore
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,179 @@
1
+ # Io(ugh)ta
2
+
3
+ [![Build Status](https://travis-ci.org/mwpastore/ioughta.svg?branch=master)](https://travis-ci.org/mwpastore/ioughta)
4
+ [![Gem Version](https://badge.fury.io/rb/ioughta.svg)](https://badge.fury.io/rb/ioughta)
5
+
6
+ Helpers for defining Go-like constants and hashes in Ruby using iota.
7
+
8
+ Go has quite a nice facility for defining constants derived from a sequential
9
+ value using a [simple and elegant syntax][1], so I thought I'd steal it for
10
+ Ruby. Rubyists tend to group constants together in hashes rather than littering
11
+ their programs with countless constants, so there's a mechanism for that, too.
12
+
13
+ Here's an example, written in Go:
14
+
15
+ ```go
16
+ type Allergen int
17
+
18
+ const (
19
+ IgEggs Allergen = 1 << iota // 1 << 0 which is 00000001
20
+ IgChocolate // 1 << 1 which is 00000010
21
+ IgNuts // 1 << 2 which is 00000100
22
+ IgStrawberries // 1 << 3 which is 00001000
23
+ IgShellfish // 1 << 4 which is 00010000
24
+ )
25
+ ```
26
+
27
+ Here it is in Ruby, using ioughta:
28
+
29
+ ```ruby
30
+ Object.ioughta_const(
31
+ :IG_EGGS, ->(ioughta) { 1 << ioughta },
32
+ :IG_CHOCOLATE,
33
+ :IG_NUTS,
34
+ :IG_STRAWBERRIES,
35
+ :IG_SHELLFISH
36
+ )
37
+
38
+ IG_STRAWBERRIES # => 8
39
+ ```
40
+
41
+ Or, perhaps a little more Rubyishly:
42
+
43
+ ```ruby
44
+ IG = Object.ioughta_hash(
45
+ :eggs, ->(i) { 1 << i },
46
+ :chocolate,
47
+ :nuts,
48
+ :strawberries,
49
+ :shellfish
50
+ ).freeze
51
+
52
+ IG[:strawberries] # => 8
53
+ ```
54
+
55
+ ## Installation
56
+
57
+ Add this line to your application's Gemfile:
58
+
59
+ ```ruby
60
+ gem 'ioughta'
61
+ ```
62
+
63
+ And then execute:
64
+
65
+ ```sh
66
+ $ bundle
67
+ ```
68
+
69
+ Or install it yourself as:
70
+
71
+ ```sh
72
+ $ gem install ioughta
73
+ ```
74
+
75
+ ## Usage
76
+
77
+ Ioughta works just like `const` and `iota` do in Go, with only a few minor
78
+ differences. You must `include` the module in your program, class, or module in
79
+ order to start using it. The iterator starts at zero (`0`) and increments for
80
+ each constant. The default lambda is simply `:itself`, so you can very easily
81
+ create a sequence of constants with consecutive integer values:
82
+
83
+ ```ruby
84
+ require 'ioughta'
85
+ include Ioughta
86
+
87
+ Object.ioughta_const(:FOO, :BAR, :QUX)
88
+
89
+ QUX # => 2
90
+ ```
91
+
92
+ To skip value(s) in the sequence, use the `:_` symbol:
93
+
94
+ ```ruby
95
+ Object.ioughta_const(:_, :FOO, :BAR, :_, :QUX)
96
+
97
+ QUX # => 4
98
+ ```
99
+
100
+ As soon as Ioughta sees a lambda, it will start using it to generate future
101
+ values from the iterator. In Go parlance, this is (apparently) known as
102
+ *implicit repetition of the last non-empty expression list*. You can redefine
103
+ the lambda as many times as you like:
104
+
105
+ ```ruby
106
+ Object.ioughta_const(
107
+ :A, # will use the default lambda (0 => 0)
108
+ :B, ->(i) { i * 2 }, # will multiply by two (1 => 2)
109
+ :C, # will also multiply by two (2 => 4)
110
+ :D, ->(j) { j ** 3 }, # will cube (3 => 27)
111
+ :E, # will also cube (4 => 64)
112
+ :F, # cube all the things (5 => 125)
113
+ :G, proc(&:itself) # restore the default behavior (6 => 6)
114
+ )
115
+ ```
116
+
117
+ The only major feature missing from the Go implementation is the ability to
118
+ perform parallel assignment in the constant list. We're defining a list of
119
+ terms, not a list of expressions, so it's not possible to do in Ruby without
120
+ resourcing to nasty `eval` tricks. Don't forget to separate your terms with
121
+ commas!
122
+
123
+ You've probably noticed that in order to use Ioughta in the top-level
124
+ namespace, we need to explicitly specify the `Object` receiver (just like we
125
+ need to do for `#const_set`). I didn't want to get too crazy with the
126
+ monkeypatching and/or dynamic dispatch. No such limitation exists when
127
+ including Ioughta in a module or class, thanks to the available context. Also,
128
+ if the `ioughta_const` and `ioughta_hash` methods are too ugly for you (I don't
129
+ blame you), they're aliased as `iota_const` and `iota_hash`, respectively.
130
+
131
+ Here is a very contrived and arbitrary example:
132
+
133
+ ```ruby
134
+ require 'ioughta'
135
+
136
+ module MyFileUtils
137
+ include Ioughta
138
+
139
+ iota_const :EXECUTE, ->(b) { 0b1 << b }, :WRITE, :READ
140
+ iota_const :TACKY, ->(b) { 0b1 << b }, :SETGID, :SETUID
141
+
142
+ SHIFT = iota_hash(:other, ->(d) { d * 3 }, :group, :user, :special).freeze
143
+ MASK = iota_hash(:other, ->(_o, key) { 07 << SHIFT[key] }, :group, :user, :special).freeze
144
+
145
+ def self.mask_and_shift(mode, field)
146
+ (mode & MASK[field]) >> SHIFT[field]
147
+ end
148
+ end
149
+
150
+ MyFileUtils.mask_and_shift(0644, :user) & MyFileUtils::EXECUTE # => 0
151
+ MyFileUtils.mask_and_shift(01777, :special) & MyFileUtils::TACKY # => 1
152
+ ```
153
+
154
+ One note on the above: the lambda can take the key at the current iteration as
155
+ an optional second argument.
156
+
157
+ ## Development
158
+
159
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
160
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
161
+ prompt that will allow you to experiment.
162
+
163
+ To install this gem onto your local machine, run `bundle exec rake install`. To
164
+ release a new version, update the version number in `version.rb`, and then run
165
+ `bundle exec rake release`, which will create a git tag for the version, push
166
+ git commits and tags, and push the `.gem` file to
167
+ [rubygems.org](https://rubygems.org).
168
+
169
+ ## Contributing
170
+
171
+ Bug reports and pull requests are welcome on GitHub at
172
+ https://github.com/mwpastore/ioughta.
173
+
174
+ ## License
175
+
176
+ The gem is available as open source under the terms of the [MIT
177
+ License](http://opensource.org/licenses/MIT).
178
+
179
+ [1]: https://splice.com/blog/iota-elegant-constants-golang/
data/Rakefile ADDED
@@ -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
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/ioughta.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+ # coding: utf-8
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'ioughta/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'ioughta'
9
+ spec.version = Ioughta::VERSION
10
+ spec.authors = ['Mike Pastore']
11
+ spec.email = ['mike@oobak.org']
12
+
13
+ spec.summary = 'Helpers for defining Go-like constants and hashes using iota'
14
+ spec.homepage = 'http://github.com/mwpastore/ioughta'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.required_ruby_version = '>= 2.2.0'
25
+
26
+ spec.add_development_dependency 'bundler', '~> 1.13'
27
+ spec.add_development_dependency 'rake', '~> 10.0'
28
+ spec.add_development_dependency 'rspec', '~> 3.0'
29
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: false
2
+ module Ioughta
3
+ VERSION = '0.1.0'.freeze
4
+ end
data/lib/ioughta.rb ADDED
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+ require 'ioughta/version'
3
+
4
+ module Ioughta
5
+ def self.included(base)
6
+ class << base
7
+ def ioughta_const(*data)
8
+ each_resolved_pair(pair(data)) { |nom, val| const_set(nom, val) }
9
+ end
10
+
11
+ alias_method :iota_const, :ioughta_const
12
+
13
+ def ioughta_hash(*data)
14
+ each_resolved_pair(pair(data)).to_h
15
+ end
16
+
17
+ alias_method :iota_hash, :ioughta_hash
18
+
19
+ private
20
+
21
+ DEFAULT_LAMBDA = proc(&:itself)
22
+ SKIP_SYMBOL = :_
23
+
24
+ def lazy_iota
25
+ (0..Float::INFINITY).lazy
26
+ end
27
+
28
+ def pair(data)
29
+ data, lam = data.dup, DEFAULT_LAMBDA
30
+ lazy_iota.each do |i|
31
+ if i % 2 != 0
32
+ if data[i].respond_to?(:call)
33
+ lam = data[i]
34
+ else
35
+ data.insert(i, lam)
36
+ end
37
+ elsif data[i].nil?
38
+ break
39
+ end
40
+ end
41
+ data
42
+ end
43
+
44
+ def each_resolved_pair(data)
45
+ return enum_for(:each_resolved_pair, data) unless block_given?
46
+
47
+ data.each_slice(2).with_object(lazy_iota) do |(nom, lam), iota|
48
+ val = lam.arity == 2 ? lam.call(iota.next, nom) : lam.call(iota.next)
49
+ next if nom == SKIP_SYMBOL
50
+ yield nom, val
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ioughta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Pastore
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-13 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.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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:
56
+ email:
57
+ - mike@oobak.org
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/setup
70
+ - ioughta.gemspec
71
+ - lib/ioughta.rb
72
+ - lib/ioughta/version.rb
73
+ homepage: http://github.com/mwpastore/ioughta
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.2.0
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.5.1
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Helpers for defining Go-like constants and hashes using iota
97
+ test_files: []