active_pattern 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
+ SHA256:
3
+ metadata.gz: ba9d3de7a7e6106b1f2a2c2bb1954acbc1546cc14597450f6b729cfada2e8c9d
4
+ data.tar.gz: b332430bd467a2e0856e5cd0cc8796c91dc30eae9878a58e479eec30af32d800
5
+ SHA512:
6
+ metadata.gz: '0376129c411d88dbaac3f1cd624940ddef01b8288999d304392c346e59192d08b2520261b9996c4b8d19a5ca093eabdc9b60bccf2844f7fc46ba7cd12731e1c4'
7
+ data.tar.gz: 4556656222a4cd7140f7e08937a6f274a705faf54e25d24099041acf7ac70aa4497c082fe63d4769f2dfc175b450656c66b9ccfbe76c58466d4da9f8f4dbbf24
@@ -0,0 +1,17 @@
1
+ version: 2
2
+ jobs:
3
+ build:
4
+ docker:
5
+ - image: ruby:2.7.0-preview2
6
+ steps:
7
+ - checkout
8
+
9
+ - run:
10
+ name: install dependencies
11
+ command: |
12
+ bundle install --jobs=4 --retry=3
13
+
14
+ - run:
15
+ name: run tests
16
+ command: |
17
+ bundle exec rake
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ 2.7.0-preview2
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.0.pre.3
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_pattern.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 kokuyouwind
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,62 @@
1
+ # ActivePattern
2
+
3
+ [![CircleCI](https://circleci.com/gh/kokuyouwind/active_pattern.svg?style=svg)](https://circleci.com/gh/kokuyouwind/active_pattern)
4
+
5
+ F# like ActivePattern in ruby pattern matching
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'active_pattern'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install active_pattern
22
+
23
+ ## Usage
24
+
25
+ ```ruby
26
+ module FZPattern
27
+ extend ActivePattern::Context[Integer]
28
+ FizzBuzz = pattern { self % 15 == 0 }
29
+ Fizz = pattern { self % 3 == 0 }
30
+ Buzz = pattern { self % 5 == 0 }
31
+ Number = pattern { [self] }
32
+ end
33
+
34
+ def fizzbuzz(n)
35
+ case n
36
+ in FZPattern::FizzBuzz; :FizzBuzz
37
+ in FZPattern::Fizz; :Fizz
38
+ in FZPattern::Buzz; :Buzz
39
+ in FZPattern::Number[n]; n
40
+ end
41
+ end
42
+
43
+ fizzbuzz(1) # => 1
44
+ fizzbuzz(3) # => :Fizz
45
+ fizzbuzz(5) # => :Buzz
46
+ fizzbuzz(15) # => :FizzBuzz
47
+ ```
48
+
49
+ ## Development
50
+
51
+ 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.
52
+
53
+ 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).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kokuyouwind/active_pattern.
58
+
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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,27 @@
1
+ require_relative 'lib/active_pattern/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "active_pattern"
5
+ spec.version = ActivePattern::VERSION
6
+ spec.authors = ["kokuyouwind"]
7
+ spec.email = ["kokuyouwind@gmail.com"]
8
+
9
+ spec.summary = %q{F# like ActivePattern in ruby pattern matching}
10
+ spec.description = %q{F# like ActivePattern in ruby pattern matching}
11
+ spec.homepage = "https://github.com/kokuyouwind/active_pattern"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = spec.homepage
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_pattern"
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
+ module FZPattern
14
+ extend ActivePattern::Context[Integer]
15
+ FizzBuzz = pattern { self % 15 == 0 }
16
+ Fizz = pattern { self % 3 == 0 }
17
+ Buzz = pattern { self % 5 == 0 }
18
+ Number = pattern { [self] }
19
+ end
20
+
21
+ def fizzbuzz(n)
22
+ case n
23
+ in FZPattern::FizzBuzz; :FizzBuzz
24
+ in FZPattern::Fizz; :Fizz
25
+ in FZPattern::Buzz; :Buzz
26
+ in FZPattern::Number[n]; n
27
+ end
28
+ end
29
+
30
+ require "irb"
31
+ IRB.start(__FILE__)
@@ -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,10 @@
1
+ require 'active_pattern/context'
2
+ require 'active_pattern/comparator'
3
+ require 'active_pattern/pattern_object'
4
+ require 'active_pattern/version'
5
+
6
+ module ActivePattern
7
+ def self.[](context_class)
8
+ Module.new.extend(ActivePattern::Context[context_class])
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module ActivePattern
2
+ module Comparator
3
+ def deconstruct
4
+ matches = $_ACTIVE_PATTERN_MATCHES
5
+ $_ACTIVE_PATTERN_MATCHES = nil
6
+ return matches if matches.is_a?(Array)
7
+
8
+ defined?(super) ? super : nil
9
+ end
10
+
11
+ def deconstruct_keys(keys)
12
+ matches = $_ACTIVE_PATTERN_MATCHES
13
+ $_ACTIVE_PATTERN_MATCHES = nil
14
+ return matches if matches.is_a?(Hash)
15
+
16
+ defined?(super) ? super : nil
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,14 @@
1
+ module ActivePattern
2
+ module Context
3
+ def self.[](context_class)
4
+ Module.new do
5
+ @@context_class = context_class
6
+ context_class.prepend(ActivePattern::Comparator)
7
+
8
+ def pattern(&pattern_proc)
9
+ PatternObject.new(@@context_class, pattern_proc)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ module ActivePattern
2
+ class PatternObject
3
+ def initialize(context_class, pattern)
4
+ @context_class = context_class
5
+ @pattern = pattern
6
+ end
7
+
8
+ def ===(other)
9
+ return false unless @context_class === other
10
+
11
+ match_result = other.instance_eval(&@pattern)
12
+ case match_result
13
+ in Array | Hash
14
+ $_ACTIVE_PATTERN_MATCHES = match_result
15
+ true
16
+ else
17
+ $_ACTIVE_PATTERN_MATCHES = nil
18
+ match_result
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module ActivePattern
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_pattern
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - kokuyouwind
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-11-29 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: F# like ActivePattern in ruby pattern matching
14
+ email:
15
+ - kokuyouwind@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".circleci/config.yml"
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".ruby-version"
24
+ - ".travis.yml"
25
+ - Gemfile
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - active_pattern.gemspec
30
+ - bin/console
31
+ - bin/setup
32
+ - lib/active_pattern.rb
33
+ - lib/active_pattern/comparator.rb
34
+ - lib/active_pattern/context.rb
35
+ - lib/active_pattern/pattern_object.rb
36
+ - lib/active_pattern/version.rb
37
+ homepage: https://github.com/kokuyouwind/active_pattern
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/kokuyouwind/active_pattern
42
+ source_code_uri: https://github.com/kokuyouwind/active_pattern
43
+ changelog_uri: https://github.com/kokuyouwind/active_pattern
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 2.3.0
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubygems_version: 3.0.6
60
+ signing_key:
61
+ specification_version: 4
62
+ summary: F# like ActivePattern in ruby pattern matching
63
+ test_files: []