dux 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d20cd7046baebf04427f80b205d460b4931e6284
4
+ data.tar.gz: 97250074fa582b00cafce8cb3b3d38fb98be0b50
5
+ SHA512:
6
+ metadata.gz: 1fc05fe33a193d4a3298aba28c9fa7b7f7a3f63c66d3a02f95acde4545a7fa01ebea4eba930ab419ffbdf6f17e5c7f81e3e9bbd98212387db40a613fbdad4ff0
7
+ data.tar.gz: f5aab9d54d3ca4c1ce2a2d2f0bd173c5f03a52a29a7dfc52237a820db20b37ce4d6f32f3975288ceb7e02304d1c4e252de62fad3458dc8cbf127e529d5e7f2ad
@@ -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
+ --color
2
+ --require spec_helper
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ - 2.1.0
5
+ - 2.2.1
6
+ - ruby-head
7
+ - rbx-2
8
+ - jruby-head
@@ -0,0 +1,8 @@
1
+ --private
2
+ --protected
3
+ -m markdown
4
+ lib/**/*.rb
5
+ -
6
+ README.md
7
+ CODE_OF_CONDUCT.md
8
+ LICENSE.txt
@@ -0,0 +1,15 @@
1
+ # @markup markdown
2
+ # @title Contributor Code of Conduct
3
+ # Contributor Code of Conduct
4
+
5
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
6
+
7
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
8
+
9
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
10
+
11
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
12
+
13
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
14
+
15
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dux.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Alexa Grey
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,123 @@
1
+ # Dux
2
+
3
+ A lazy duck-type matching gem that is particularly designed for use in case statements.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dux'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dux
20
+
21
+ ## Usage
22
+
23
+ Usage is straightforward out of the box:
24
+
25
+ ```ruby
26
+ case foo
27
+ when Dux[:bar] then foo.bar
28
+ when Dux[:baz] then foo.baz
29
+ when Dux[:quux] then foo.quux
30
+ end
31
+ ```
32
+
33
+ It also has some methods for matching against a strict interface:
34
+
35
+ ```ruby
36
+ case foo
37
+ when Dux.all(:bar, :baz) then foo.bar && foo.baz
38
+ when Dux.any(:quux, :bloop) then foo.try(:quux) || foo.try(:bloop)
39
+ end
40
+ ```
41
+
42
+ ### Core Extensions
43
+
44
+ There are a few core extensions available that can be manually enabled.
45
+
46
+ #### String / Symbol
47
+
48
+ Strings and symbols can have a `duckify` method added that operates the same
49
+ way as `Dux.[]`:
50
+
51
+ ```ruby
52
+ Dux.extend_strings_and_symbols!
53
+
54
+ # Or, if you only want to touch one class:
55
+ # Dux.extend_strings!
56
+ # Dux.extend_symbols!
57
+
58
+ case foo
59
+ when "bar".duckify then foo.bar
60
+ when :baz.duckify then foo.baz
61
+ end
62
+ ```
63
+
64
+ #### Array
65
+
66
+ There is also the ability to _duckify_ an array of strings or symbols for
67
+ interface matching:
68
+
69
+ ```ruby
70
+ Dux.add_flock_methods!
71
+
72
+ case foo
73
+ when %i[bar baz].duckify then foo.bar && foo.baz
74
+ when %i[quux bloop].duckify(type: :any) then foo.try(:quux) || foo.try(:bloop)
75
+ end
76
+ ```
77
+
78
+ #### Shorthand
79
+
80
+ There is an experimental option that uses unary `~` as an alias for `#duckify`.
81
+
82
+ `Array#~`, `String#~`, and `Symbol#~` are presently not used by Ruby core, but
83
+ other gems might define them.
84
+
85
+ ```ruby
86
+ # Enable per class:
87
+ Dux.array_shorthand!
88
+ Dux.string_shorthand!
89
+ Dux.symbol_shorthand!
90
+
91
+ case foo
92
+ when ~%i[bar baz] then foo.bar && foo.baz
93
+ when ~:quux then foo.quux
94
+ when ~"bloop" then foo.bloop
95
+ end
96
+ ```
97
+
98
+ #### All core extensions
99
+ To enable all core extensions:
100
+
101
+ ```ruby
102
+ # To enable flock methods and String / Symbol #duckify
103
+
104
+ Dux.extend_all!
105
+
106
+ # To enable the above as well as the unary ~ shorthand:
107
+
108
+ Dux.extend_all! experimental: true
109
+ ```
110
+
111
+ ## Development
112
+
113
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
114
+
115
+ 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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
116
+
117
+ ## Contributing
118
+
119
+ 1. Fork it ( https://github.com/scryptmouse/dux/fork )
120
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
121
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
122
+ 4. Push to the branch (`git push origin my-new-feature`)
123
+ 5. Create a new Pull Request
@@ -0,0 +1,17 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+ require 'yard'
4
+ require 'yard/rake/yardoc_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec') do |t|
7
+ t.verbose = false
8
+ end
9
+
10
+ YARD::Rake::YardocTask.new do |t|
11
+ t.files = ['lib/**/*.rb', '-', 'README.md', 'LICENSE.txt', 'CODE_OF_CONDUCT.md']
12
+ t.options = ['--private', '--protected']
13
+ t.stats_options = ['--list-undoc']
14
+ end
15
+
16
+ # If you want to make this the default task
17
+ task :default => :spec
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dux"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -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 'dux/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dux"
8
+ spec.version = Dux::VERSION
9
+ spec.authors = ["Alexa Grey"]
10
+ spec.email = ["devel@mouse.vc"]
11
+
12
+ spec.summary = %q{Lazy duck-type matching}
13
+ spec.homepage = "https://github.com/scryptmouse/dux"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.9"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "pry"
25
+ spec.add_development_dependency "simplecov"
26
+ spec.add_development_dependency "yard"
27
+ end
@@ -0,0 +1,175 @@
1
+ require 'dux/version'
2
+ require 'dux/duckify'
3
+ require 'dux/hacks_like_a_duck'
4
+ require 'dux/flock_methods'
5
+
6
+ # Super simple duck-type testing.
7
+ module Dux
8
+ # Methods for generating "interface" checks against an array of symbols
9
+ FLOCK_TYPES = %i[all any none]
10
+
11
+ module_function
12
+
13
+ # @param [Symbol] symbol
14
+ # @param [Boolean] include_all
15
+ # @return [Proc]
16
+ def dux(symbol, include_all: false)
17
+ ->(obj) { obj.respond_to? symbol, include_all }
18
+ end
19
+
20
+ class << self
21
+ alias_method :[], :dux
22
+
23
+ # @param [:all, :any, :none] type
24
+ # @param [<Symbol, String>] methods
25
+ # @param [Boolean] include_all
26
+ # @raise [ArgumentError] on invalid type
27
+ # @return [<Proc>]
28
+ def flock(type, *methods, include_all: false)
29
+ raise ArgumentError, "Invalid flock type: #{type}" unless FLOCK_TYPES.include? type
30
+
31
+ __send__ type, methods, include_all: include_all
32
+ end
33
+
34
+ # Creates a lambda that describes a restrictive interface,
35
+ # wherein the provided object must `respond_to?` *all* of
36
+ # the methods.
37
+ #
38
+ # @param [<Symbol, String>] methods
39
+ # @param [Boolean] include_all
40
+ # @return [Proc]
41
+ def all(*methods, include_all: false)
42
+ ducks = flockify methods, include_all: include_all
43
+
44
+ ->(obj) { ducks.all? { |duck| duck.call obj } }
45
+ end
46
+
47
+ # Creates a lambda that describes a permissive interface,
48
+ # wherein the provided object must `respond_to?` at least
49
+ # one of the methods.
50
+ #
51
+ # @param [<Symbol>] methods
52
+ # @param [Boolean] include_all
53
+ # @return [Proc]
54
+ def any(*methods, include_all: false)
55
+ ducks = flockify methods, include_all: include_all
56
+
57
+ ->(obj) { ducks.any? { |duck| duck.call obj } }
58
+ end
59
+
60
+ # Creates a lambda that describes a restrictive interface,
61
+ # wherein the provided object must `respond_to?` *none* of
62
+ # the methods.
63
+ #
64
+ # @param [<Symbol>] methods
65
+ # @param [Boolean] include_all
66
+ # @return [Proc]
67
+ def none(*methods, include_all: false)
68
+ ducks = flockify methods, include_all: include_all
69
+
70
+ ->(obj) { ducks.none? { |duck| duck.call obj } }
71
+ end
72
+
73
+ # @!group Core extensions
74
+
75
+ # Enhance `Array` with {Dux::FlockMethods#duckify}
76
+ #
77
+ # @return [void]
78
+ def add_flock_methods!
79
+
80
+ Array.prepend Dux::FlockMethods
81
+
82
+ return nil
83
+ end
84
+
85
+ # Load all `Dux` core extensions.
86
+ #
87
+ # @param [Boolean] experimental whether to load experimental shorthand methods
88
+ # @return [void]
89
+ def extend_all!(experimental: false)
90
+ add_flock_methods!
91
+ extend_strings_and_symbols!
92
+
93
+ return unless experimental
94
+
95
+ array_shorthand!
96
+ string_shorthand!
97
+ symbol_shorthand!
98
+ end
99
+
100
+ # Enhance `String` with {Dux::Duckify}
101
+ #
102
+ # @return [void]
103
+ def extend_strings!
104
+ String.prepend Dux::Duckify
105
+
106
+ return nil
107
+ end
108
+
109
+ # Enhance `Symbol` with {Dux::Duckify}
110
+ #
111
+ # @return [void]
112
+ def extend_symbols!
113
+ Symbol.prepend Dux::Duckify
114
+
115
+ return nil
116
+ end
117
+
118
+ # Enhance `String` and `Symbol` classes with {Dux::Duckify#duckify}
119
+ #
120
+ # @return [void]
121
+ def extend_strings_and_symbols!
122
+ extend_strings!
123
+ extend_symbols!
124
+
125
+ return nil
126
+ end
127
+
128
+ # Experimental feature to add unary `~` to `Array`s
129
+ # for quick usage in case statements.
130
+ #
131
+ # @return [void]
132
+ def array_shorthand!
133
+ add_flock_methods!
134
+
135
+ Array.prepend Dux::HacksLikeADuck
136
+
137
+ nil
138
+ end
139
+
140
+ # Experimental feature to add unary `~` to `String`s
141
+ # for quick usage in case statements.
142
+ #
143
+ # @return [void]
144
+ def string_shorthand!
145
+ extend_strings!
146
+ String.prepend Dux::HacksLikeADuck
147
+
148
+ nil
149
+ end
150
+
151
+ # Experimental feature to add unary `~` to `Symbol`s
152
+ # for quick usage in case statements.
153
+ #
154
+ # @return [void]
155
+ def symbol_shorthand!
156
+ extend_symbols!
157
+
158
+ Symbol.prepend Dux::HacksLikeADuck
159
+
160
+ return nil
161
+ end
162
+
163
+ # @!endgroup
164
+
165
+ protected
166
+ # @param [<Symbol>] methods
167
+ # @param [Boolean] include_all
168
+ # @return [<Proc>]
169
+ def flockify(methods, include_all: false)
170
+ methods.flatten.map do |sym|
171
+ dux sym, include_all: include_all
172
+ end
173
+ end
174
+ end
175
+ end
@@ -0,0 +1,30 @@
1
+ module Dux
2
+ # Adds a fluent {#duckify} method to classes that include / prepend.
3
+ #
4
+ # Non-stringish / symbol classes should override {#for_duckify} to
5
+ # return a string or symbol.
6
+ module Duckify
7
+ # @return [self, String, Symbol]
8
+ def for_duckify
9
+ self
10
+ end
11
+
12
+ # @param [Boolean] include_all
13
+ # @return [Proc]
14
+ def duckify(include_all: false)
15
+ dux for_duckify, include_all: include_all
16
+ end
17
+
18
+ class << self
19
+ # @api private
20
+ # @param [Class] base
21
+ # @return [void]
22
+ def included(base)
23
+ base.__send__ :include, Dux
24
+ end
25
+
26
+ alias_method :prepended, :included
27
+ alias_method :extended, :included
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ module Dux
2
+ # Can be included on any Array or Array-like (implements `#to_a`)
3
+ # to grant it a fluent method for generating duck checks.
4
+ module FlockMethods
5
+ # @param [:all, :any, :none] type
6
+ # @param [Boolean] include_all
7
+ # @return [<Proc>]
8
+ def duckify(type: :all, include_all: false)
9
+ Dux.flock type, to_a, include_all: include_all
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ module Dux
2
+ # Experimental module to grant unary `~` to symbols or strings for duck-checking
3
+ module HacksLikeADuck
4
+
5
+ # Transform into a proc that asks its argument if it responds to `self`
6
+ #
7
+ # @return [Proc]
8
+ def ~
9
+ duckify
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ module Dux
2
+ # Gem version.
3
+ VERSION = "0.1.0"
4
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dux
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexa Grey
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-04-05 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.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'
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
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - devel@mouse.vc
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - ".yardopts"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - dux.gemspec
116
+ - lib/dux.rb
117
+ - lib/dux/duckify.rb
118
+ - lib/dux/flock_methods.rb
119
+ - lib/dux/hacks_like_a_duck.rb
120
+ - lib/dux/version.rb
121
+ homepage: https://github.com/scryptmouse/dux
122
+ licenses:
123
+ - MIT
124
+ metadata: {}
125
+ post_install_message:
126
+ rdoc_options: []
127
+ require_paths:
128
+ - lib
129
+ required_ruby_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ required_rubygems_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ requirements: []
140
+ rubyforge_project:
141
+ rubygems_version: 2.4.5
142
+ signing_key:
143
+ specification_version: 4
144
+ summary: Lazy duck-type matching
145
+ test_files: []
146
+ has_rdoc: