nifty-variants 1.0.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: 5574aca0890dc28da6736189ba3a749b06fd1590
4
+ data.tar.gz: e47d15b310ff5461e757957c82484e16b6db4db9
5
+ SHA512:
6
+ metadata.gz: 10677127ef34f17ed0c9131bc2bb0cb95ae3bce6289b300792571d7250b509d3330c050d16e0c3b9ccc5fa8cd858a2e2084934efa033221945aecd54486cfa57
7
+ data.tar.gz: ebd51fefa61f36f74d462cedbd25ce39a2f2f933852f7940ffdb0946eef7d45eb905b65e83a4414c5837dbdb861be293c4b6473eb13e564bd5611772036cb6d1
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'generative', '~> 0.2.x', git: 'git@github.com:justincampbell/generative.git'
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Nate West
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # Nifty::Variants
2
+
3
+ If you are familiar with variants from a nice language like Haskell try this.
4
+
5
+ If you don't know what variants are [start here](http://youtu.be/ZQkIWWTygio)
6
+
7
+ **NOTE** Nifty::Variants makes use of Refinements which weren't introduced until
8
+ Ruby 2.1 and as a result no Ruby below 2.1 is supported. You're welcome to write
9
+ a fancy shim, but I don't recommend it.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'nifty-variants'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install -j2
22
+
23
+ ## Usage
24
+
25
+ `nifty-variants` makes use of Refinements which were introduced in Ruby 2.1.
26
+ In order to use it in your class or module simply use it.
27
+
28
+ ```ruby
29
+ class OrderDispatch
30
+ using Nifty::Variants
31
+
32
+ def shipit!(order)
33
+ cases order,
34
+ digital: ->(o) { o.email! },
35
+ delivery: ->(o) { o.deliver! }
36
+ else: ->(o) { move_to_error_queue(o) }
37
+ end
38
+ end
39
+ ```
40
+
41
+ ## Contributing
42
+
43
+ 1. Fork it ( https://github.com/[my-github-username]/nifty-variants/fork )
44
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
45
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
46
+ 4. Push to the branch (`git push origin my-new-feature`)
47
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+ require 'generative/rake_task'
4
+
5
+ task(:default).enhance [:spec, :generative]
6
+
7
+ RSpec::Core::RakeTask.new
8
+ Generative::RakeTask.new
9
+
10
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ module Nifty
2
+ module Variants
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ require "nifty/variants/version"
2
+
3
+ module Nifty
4
+ module Variants
5
+ class NonExhaustiveMatches < StandardError; end
6
+ class NonVariant < StandardError
7
+ def initialize(msg="variants looks like [:type, some_obj]")
8
+ super(msg)
9
+ end
10
+ end
11
+
12
+ refine Object do
13
+ def cases(variant, matches)
14
+ raise NonVariant unless is_variant?(variant)
15
+ type, *obj = variant
16
+ m = matches[type] ||
17
+ matches[:else] ||
18
+ (raise NonExhaustiveMatches, ": #{matches.keys}")
19
+ m.call(*obj)
20
+ end
21
+
22
+ def is_variant?(variant)
23
+ return false unless variant.is_a?(Array)
24
+ type, *rest = variant
25
+ !rest.empty? && type.is_a?(Symbol)
26
+ end
27
+ end
28
+ end
29
+ 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 'nifty/variants/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "nifty-variants"
8
+ spec.version = Nifty::Variants::VERSION
9
+ spec.authors = ["Nate West"]
10
+ spec.email = ["natescott.west@gmail.com"]
11
+ spec.summary = %q{Variants in Ruby}
12
+ spec.description = %q{Variants in Ruby}
13
+ spec.homepage = "http://github.com/niftyn8/nifty-variants"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency 'rspec-core', '~> 3.1.3'
24
+ spec.add_development_dependency 'rspec', '~> 3.1.0'
25
+ spec.add_development_dependency 'pry'
26
+ spec.add_development_dependency 'degenerate'
27
+ end
@@ -0,0 +1,39 @@
1
+ class OrderDispatch
2
+ using Nifty::Variants
3
+
4
+ def self.send_to(place)
5
+ place
6
+ end
7
+
8
+ def self.dispatch(order)
9
+ cases order,
10
+ pickup: ->(o) { send_to(o.store_id) },
11
+ digital: ->(o) { send_to(o.email) },
12
+ delivery: ->(o) { send_to(o.address) },
13
+ else: ->(o) { raise "Don't know how to handle #{o.type}" }
14
+ end
15
+ end
16
+
17
+ class Order
18
+ attr_reader :type, :address, :email, :store_id
19
+
20
+ def initialize(type: :pickup, address: "", email: "", store_id: "")
21
+ @type = type
22
+ @address = address
23
+ @email = email
24
+ @store_id = store_id
25
+ end
26
+
27
+ def to_variant
28
+ [type, self]
29
+ end
30
+ end
31
+
32
+ Generative.register_generator :order do |_|
33
+ Order.new(
34
+ type: [:pickup, :digital, :delivery].sample,
35
+ address: Generative.generate(:string, limit: 100),
36
+ email: Generative.generate(:string, limit: 100),
37
+ store_id: Generative.generate(:integer)
38
+ )
39
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Order do
4
+ describe "#to_variant" do
5
+ generative do
6
+ for_every(:order) { generate(:order) }
7
+
8
+ it "returns a tuple-like variant" do
9
+ o = order
10
+ variant, *obj = o.to_variant
11
+ expect(variant).to eq o.type
12
+ expect(*obj).to eq o
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ RSpec.describe OrderDispatch do
19
+ describe ".dispatch" do
20
+ generative do
21
+ for_every(:order) { generate(:order) }
22
+
23
+ let(:method_for) {
24
+ {
25
+ pickup: :store_id,
26
+ digital: :email,
27
+ delivery: :address
28
+ }
29
+ }
30
+
31
+ it "returns the proper place" do
32
+ o = order
33
+ expected_place = o.send(method_for[o.type])
34
+
35
+ expect(OrderDispatch.dispatch(o.to_variant)).to eq expected_place
36
+ end
37
+
38
+ it "raises an exception for unknown order types" do
39
+ type = generate(:string, limit: 10)
40
+ expect { OrderDispatch.dispatch([type, order]) }.to raise_error
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe Nifty::Variants do
4
+ using Nifty::Variants
5
+
6
+ describe "cases" do
7
+ generative do
8
+ for_every(:variant) { generate(:variant) }
9
+ for_every(:nonvariant) { generate(:nonvariant) }
10
+
11
+ it "lets you know if your matches aren't exhaustive" do
12
+ expect {
13
+ cases variant, {}
14
+ }.to raise_error(Nifty::Variants::NonExhaustiveMatches)
15
+ end
16
+
17
+ it "can take advantage of an else" do
18
+ v = variant
19
+ expect(
20
+ cases v,
21
+ else: ->(_) { v.first }
22
+ ).to eq v.first
23
+ end
24
+
25
+ it "selects the correct variant match" do
26
+ v = variant
27
+ expect(
28
+ cases v,
29
+ generate(:key) => ->(*_) { false },
30
+ generate(:key) => ->(*_) { false },
31
+ v.first => ->(*_) { true }
32
+ ).to be true
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "is_variant?" do
38
+ generative do
39
+ for_every(:variant) { generate(:variant) }
40
+ for_every(:nonvariant) { generate(:nonvariant) }
41
+
42
+ it "returns true for valid variants" do
43
+ expect(is_variant?(variant)).to be true
44
+ end
45
+
46
+ it "returns false of invalid variants" do
47
+ expect(is_variant?(nonvariant)).to be false
48
+ end
49
+ end
50
+ end
51
+
52
+ Generative.register_generator(:key) do |_|
53
+ Generative.generate(:string, limit: 10).to_sym
54
+ end
55
+
56
+ Generative.register_generator(:variant) do |_|
57
+ [Generative.generate(:key), Generative.generate(:order)]
58
+ end
59
+
60
+ Generative.register_generator(:nonvariant) do |_|
61
+ generator = Generative.manager.generators.keys.select { |g|
62
+ !g.to_s.match(/variant/)
63
+ }.sample
64
+
65
+ Generative.find_and_call(generator, limit: 100)
66
+ end
67
+ end
@@ -0,0 +1,8 @@
1
+ require_relative '../lib/nifty/variants'
2
+ require 'generative'
3
+ require 'degenerate'
4
+ require 'generators/order'
5
+ require 'pry'
6
+
7
+ RSpec.configure do |config|
8
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nifty-variants
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nate West
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-01 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.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
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-core
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.1.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.1.3
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
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: degenerate
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: Variants in Ruby
98
+ email:
99
+ - natescott.west@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/nifty/variants.rb
110
+ - lib/nifty/variants/version.rb
111
+ - nifty-variants.gemspec
112
+ - spec/generators/order.rb
113
+ - spec/lib/nifty/order_spec.rb
114
+ - spec/lib/nifty/variants_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: http://github.com/niftyn8/nifty-variants
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Variants in Ruby
140
+ test_files:
141
+ - spec/generators/order.rb
142
+ - spec/lib/nifty/order_spec.rb
143
+ - spec/lib/nifty/variants_spec.rb
144
+ - spec/spec_helper.rb