pattern_matchable 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8232bcf22e17e7d55079ab42f11af82f9f612a55f8e9c2506a796bcd67da1116
4
+ data.tar.gz: f45d869e1679fe340a5f8e73139ed3de6f00038c9813fae20bb5e0d3d7b510d5
5
+ SHA512:
6
+ metadata.gz: 01c18ccf82903a74f7346f6858668ffb4d5a33d23b829cba43ade37dbce0be91c7c85515c1e7115b8af2346c5bd6064d02d6bdd72a48a424537966b7e5f00713
7
+ data.tar.gz: f21aa128c4329d7ec59978cba5ce931f47cbac4a29e6927e11351e3f5f1bd02ab1dacbebc363db9f2eac995fa9aef088cffdc75ff69a50a4993a679b9558c258
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -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.2
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in pattern_matchable.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
data/README.md ADDED
@@ -0,0 +1,157 @@
1
+ # PatternMatchable
2
+
3
+ call method with pattern match.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pattern_matchable'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pattern_matchable
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require "pattern_matchable"
25
+
26
+ class Array
27
+ # defined #deconstruct_keys
28
+ include PatternMatchable
29
+ end
30
+
31
+ case [1, 2, 3]
32
+ in first:, last:
33
+ pp "OK : #{first}, #{last}"
34
+ # => "OK : 1, 3"
35
+ end
36
+
37
+
38
+ class Time
39
+ include PatternMatchable
40
+
41
+ def to_four_seasons
42
+ case self
43
+ in month: (3..5)
44
+ "spring"
45
+ in month: (6..8)
46
+ "summer"
47
+ in month: (9..11)
48
+ "autumn"
49
+ in { month: (1..2) } | { month: 12 }
50
+ "winter"
51
+ end
52
+ end
53
+ end
54
+
55
+ p Time.local(2019, 1, 1).to_four_seasons # => "winter"
56
+ p Time.local(2019, 3, 1).to_four_seasons # => "spring"
57
+ p Time.local(2019, 5, 1).to_four_seasons # => "spring"
58
+ p Time.local(2019, 8, 1).to_four_seasons # => "summer"
59
+ p Time.local(2019, 10, 1).to_four_seasons # => "autumn"
60
+ p Time.local(2019, 12, 1).to_four_seasons # => "winter"
61
+ ```
62
+
63
+ ### 1. `include PatternMatchable`
64
+
65
+ `include` and add `#deconstruct_keys` to any class / module.
66
+
67
+ ```ruby
68
+ require "pattern_matchable"
69
+
70
+ # mixin PatternMatchable to Array
71
+ # defined Array#deconstruct_keys
72
+ class Array
73
+ include PatternMatchable
74
+ end
75
+
76
+ [1, 2, 3, 4, 5] in { first:, last: }
77
+ p first # => 1
78
+ p last # => 5
79
+
80
+ # error: NoMatchingPatternError
81
+ "Homu" in { downcase:, upcase: }
82
+ ```
83
+
84
+ ### 2. `using PatternMatchable`
85
+
86
+ Use Refinements to add `Object#deconstruct_keys`.
87
+
88
+ ```ruby
89
+ require "pattern_matchable"
90
+
91
+ # defined Object#deconstruct_keys
92
+ using PatternMatchable
93
+
94
+ [1, 2, 3, 4, 5] in { first:, last: }
95
+ p first # => 1
96
+ p last # => 5
97
+
98
+ "Homu" in { downcase:, upcase: }
99
+ p downcase # => "homu"
100
+ p upcase # => "HOMU"
101
+ ```
102
+
103
+ ### 3. `using PatternMatchable.refining klass`
104
+
105
+ Add `#deconstruct_keys` to any class / module using Refinements.
106
+
107
+ ```ruby
108
+ require "pattern_matchable"
109
+
110
+ # define Array#deconstruct_keys
111
+ using PatternMatchable.refining Array
112
+
113
+ [1, 2, 3, 4, 5] in { first:, last: }
114
+ p first # => 1
115
+ p last # => 5
116
+
117
+ # error: NoMatchingPatternError
118
+ "Homu" in { downcase:, upcase: }
119
+ ```
120
+
121
+ ### 4. `using PatternMatchable::#{class name}`
122
+
123
+ Use `Refinements` using `const_missing`.
124
+
125
+ ```ruby
126
+ require "pattern_matchable"
127
+
128
+ # define Array#deconstruct_keys
129
+ using PatternMatchable::Array
130
+
131
+ [1, 2, 3, 4, 5] in { first:, last: }
132
+ p first # => 1
133
+ p last # => 5
134
+
135
+ # nested class name
136
+ # defined Enumerator::Lazy
137
+ using PatternMatchable::Enumerator::Lazy
138
+
139
+ (1..10).lazy.map { _1 * 2 } in { first:, count: }
140
+ p first # => 2
141
+ p count # => 10
142
+
143
+ # error: NoMatchingPatternError
144
+ "Homu" in { downcase:, upcase: }
145
+ ```
146
+
147
+
148
+ ## Development
149
+
150
+ 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.
151
+
152
+ 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).
153
+
154
+ ## Contributing
155
+
156
+ Bug reports and pull requests are welcome on GitHub at https://github.com/osyo-manga/pattern_matchable.
157
+
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/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pattern_matchable"
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(__FILE__)
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
@@ -0,0 +1,27 @@
1
+ require "pattern_matchable/version"
2
+
3
+ module PatternMatchable
4
+ def deconstruct_keys(keys)
5
+ keys.to_h { [_1, public_send(_1)] }
6
+ end
7
+
8
+ refine Object do
9
+ include PatternMatchable
10
+ end
11
+
12
+ def self.refining(klass)
13
+ Module.new {
14
+ refine klass do
15
+ include PatternMatchable
16
+ end
17
+
18
+ define_singleton_method(:const_missing) { |nested_name|
19
+ PatternMatchable.refining Object.const_get("#{klass.name}::#{nested_name}")
20
+ }
21
+ }
22
+ end
23
+
24
+ def self.const_missing(klass_name)
25
+ self.refining(Object.const_get(klass_name))
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module PatternMatchable
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,22 @@
1
+ require_relative 'lib/pattern_matchable/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "pattern_matchable"
5
+ spec.version = PatternMatchable::VERSION
6
+ spec.authors = ["manga_osyo"]
7
+ spec.email = ["manga.osyo@gmail.com"]
8
+
9
+ spec.summary = %q{call method with pattern match.}
10
+ spec.description = %q{call method with pattern match.}
11
+ spec.homepage = ""
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ # Specify which files should be added to the gem when it is released.
15
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+ end
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pattern_matchable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - manga_osyo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-04-11 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: call method with pattern match.
14
+ email:
15
+ - manga.osyo@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - README.md
25
+ - Rakefile
26
+ - bin/console
27
+ - bin/setup
28
+ - lib/pattern_matchable.rb
29
+ - lib/pattern_matchable/version.rb
30
+ - pattern_matchable.gemspec
31
+ homepage: ''
32
+ licenses: []
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 2.3.0
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.1.2
50
+ signing_key:
51
+ specification_version: 4
52
+ summary: call method with pattern match.
53
+ test_files: []