keyword_builder 1.0.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 +7 -0
- data/.circleci/config.yml +95 -0
- data/.gitignore +9 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +24 -0
- data/Rakefile +2 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/keyword_builder.gemspec +31 -0
- data/lib/keyword_builder.rb +98 -0
- data/lib/keyword_builder/version.rb +5 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 743dfc25e553bb58429f988d10aa0e1b4b78b5c6d0cf285b88215485047257ea
|
4
|
+
data.tar.gz: 8153a618882d9a8aece384223901b7ac4aa073faa8ecee9e65fe91016ecc0bf4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4e2df82927a3f3f1c3249af0d266b7b7e8da191415de8798d48d80992b159479e751a66a410af99b098f4ec78cb16ace3966d9c5c28a2ab06a9f12cfe58fb1a
|
7
|
+
data.tar.gz: ec0a214b4ebb2ad2f8c5a627891dbb969e718c0dc4ce9d75c1e1f9a95f8ce5b97dc7de6cddb9eae242a0be2cd18e914b0bb6651762a8b00404765470d2546c92
|
@@ -0,0 +1,95 @@
|
|
1
|
+
version: 2.1
|
2
|
+
|
3
|
+
executors:
|
4
|
+
ruby:
|
5
|
+
parameters:
|
6
|
+
ruby-version:
|
7
|
+
type: string
|
8
|
+
default: "2.6"
|
9
|
+
gemfile:
|
10
|
+
type: string
|
11
|
+
default: "Gemfile"
|
12
|
+
docker:
|
13
|
+
- image: circleci/ruby:<< parameters.ruby-version >>
|
14
|
+
environment:
|
15
|
+
BUNDLE_JOBS: 3
|
16
|
+
BUNDLE_RETRY: 3
|
17
|
+
BUNDLE_PATH: vendor/bundle
|
18
|
+
RAILS_ENV: test
|
19
|
+
BUNDLE_GEMFILE: << parameters.gemfile >>
|
20
|
+
|
21
|
+
jobs:
|
22
|
+
test:
|
23
|
+
parameters:
|
24
|
+
ruby-version:
|
25
|
+
type: string
|
26
|
+
executor:
|
27
|
+
name: ruby
|
28
|
+
ruby-version: << parameters.ruby-version >>
|
29
|
+
parallelism: 1
|
30
|
+
steps:
|
31
|
+
- checkout
|
32
|
+
|
33
|
+
- run:
|
34
|
+
# Remove the non-appraisal gemfile for safety: we never want to use it.
|
35
|
+
name: Prepare bundler
|
36
|
+
command: bundle -v
|
37
|
+
|
38
|
+
- run:
|
39
|
+
name: Compute a gemfile lock
|
40
|
+
command: bundle lock && cp "${BUNDLE_GEMFILE}.lock" /tmp/gem-lock
|
41
|
+
|
42
|
+
- restore_cache:
|
43
|
+
keys:
|
44
|
+
- keyword_builder-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
|
45
|
+
- keyword_builder-
|
46
|
+
|
47
|
+
- run:
|
48
|
+
name: Bundle Install
|
49
|
+
command: bundle check || bundle install
|
50
|
+
|
51
|
+
- save_cache:
|
52
|
+
key: keyword_builder-<< parameters.ruby-version >>-{{ checksum "/tmp/gem-lock" }}
|
53
|
+
paths:
|
54
|
+
- vendor/bundle
|
55
|
+
|
56
|
+
- run:
|
57
|
+
name: Run rspec
|
58
|
+
command: bundle exec rspec --profile 10 --format RspecJunitFormatter --out test_results/rspec.xml --format progress
|
59
|
+
|
60
|
+
- store_test_results:
|
61
|
+
path: test_results
|
62
|
+
|
63
|
+
publish:
|
64
|
+
executor: ruby
|
65
|
+
steps:
|
66
|
+
- checkout
|
67
|
+
- run:
|
68
|
+
name: Setup Rubygems
|
69
|
+
command: |
|
70
|
+
mkdir ~/.gem &&
|
71
|
+
echo -e "---\r\n:rubygems_api_key: $RUBYGEMS_API_KEY" > ~/.gem/credentials &&
|
72
|
+
chmod 0600 ~/.gem/credentials
|
73
|
+
- run:
|
74
|
+
name: Publish to Rubygems
|
75
|
+
command: |
|
76
|
+
gem build keyword_builder.gemspec
|
77
|
+
gem push keyword_builder-*.gem
|
78
|
+
|
79
|
+
|
80
|
+
workflows:
|
81
|
+
version: 2.1
|
82
|
+
build:
|
83
|
+
jobs:
|
84
|
+
- test:
|
85
|
+
name: "ruby 2.5"
|
86
|
+
ruby-version: "2.5"
|
87
|
+
- test:
|
88
|
+
name: "ruby 2.6"
|
89
|
+
ruby-version: "2.6"
|
90
|
+
- publish:
|
91
|
+
filters:
|
92
|
+
branches:
|
93
|
+
only: master
|
94
|
+
tags:
|
95
|
+
ignore: /.*/
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2018 DMM.com
|
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,24 @@
|
|
1
|
+
# KeywordBuilder
|
2
|
+
|
3
|
+
KeywordBuilder is a simple wrapper to create a builder-style DSL for any method
|
4
|
+
taking keyword arguments.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'keyword_builder'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install keyword_builder
|
21
|
+
|
22
|
+
## Contributing
|
23
|
+
|
24
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/iknow/keyword_builder.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "keyword_builder"
|
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,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'keyword_builder/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'keyword_builder'
|
9
|
+
spec.version = KeywordBuilder::VERSION
|
10
|
+
spec.authors = ['DMM Eikaiwa']
|
11
|
+
spec.email = ['dev@iknow.jp']
|
12
|
+
|
13
|
+
spec.summary = 'DSL builder interface to wrap keyword constructors.'
|
14
|
+
spec.homepage = 'https://github.com/iknow/keyword_builder'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'byebug'
|
29
|
+
spec.add_development_dependency 'rspec'
|
30
|
+
spec.add_development_dependency 'rspec_junit_formatter'
|
31
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Abstract builder interface for types with keyword argument constructors.
|
4
|
+
class KeywordBuilder
|
5
|
+
class BuilderError < ArgumentError; end
|
6
|
+
|
7
|
+
class << self
|
8
|
+
def create(clazz, constructor: :new)
|
9
|
+
keywords, wildcard = parse_constructor_parameters(clazz, constructor)
|
10
|
+
|
11
|
+
Class.new(self) do
|
12
|
+
define_singleton_method(:clazz) { clazz }
|
13
|
+
define_singleton_method(:constructor) { constructor }
|
14
|
+
define_singleton_method(:keywords) { keywords }
|
15
|
+
define_singleton_method(:wildcard?) { wildcard }
|
16
|
+
keywords.each do |keyword|
|
17
|
+
define_method(keyword) { |*args, &block| _set_attribute(keyword, *args, &block) }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def valid_keyword?(param)
|
23
|
+
wildcard? || keywords.include?(param)
|
24
|
+
end
|
25
|
+
|
26
|
+
def build!(**initial_attrs, &block)
|
27
|
+
builder = self.new(initial_attrs)
|
28
|
+
builder.instance_eval(&block) if block_given?
|
29
|
+
clazz.public_send(constructor, **builder.attrs)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def parse_constructor_parameters(clazz, constructor)
|
35
|
+
parameters =
|
36
|
+
if constructor == :new
|
37
|
+
clazz.instance_method(:initialize).parameters
|
38
|
+
else
|
39
|
+
clazz.method(constructor).parameters
|
40
|
+
end
|
41
|
+
|
42
|
+
keywords = Set.new
|
43
|
+
wildcard = false
|
44
|
+
|
45
|
+
parameters.each do |type, name|
|
46
|
+
case type
|
47
|
+
when :opt, :rest
|
48
|
+
next
|
49
|
+
when :key, :keyreq
|
50
|
+
keywords << name
|
51
|
+
when :keyrest
|
52
|
+
wildcard = true
|
53
|
+
else
|
54
|
+
raise ArgumentError.new("Invalid builder method, contains required non-keyword parameter #{name}")
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
[keywords.freeze, wildcard]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_reader :attrs
|
63
|
+
|
64
|
+
def initialize(initial_attrs)
|
65
|
+
@attrs = initial_attrs.dup
|
66
|
+
end
|
67
|
+
|
68
|
+
def _set_attribute(attr, *args, &block)
|
69
|
+
if attrs.has_key?(attr)
|
70
|
+
raise BuilderError.new("Invalid builder state: #{attr} already provided")
|
71
|
+
end
|
72
|
+
|
73
|
+
value =
|
74
|
+
if block_given?
|
75
|
+
raise ArgumentError.new('Cannot provide both immediate and block value') unless args.blank?
|
76
|
+
|
77
|
+
block
|
78
|
+
elsif args.size == 1
|
79
|
+
args[0]
|
80
|
+
else
|
81
|
+
raise ArgumentError.new('Wrong number of arguments: expected 1 or block')
|
82
|
+
end
|
83
|
+
|
84
|
+
attrs[attr] = value
|
85
|
+
end
|
86
|
+
|
87
|
+
def method_missing(attr, *args, &block)
|
88
|
+
if self.class.wildcard?
|
89
|
+
_set_attribute(attr, *args, &block)
|
90
|
+
else
|
91
|
+
super
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def respond_to_missing?(attr, _include_all = false)
|
96
|
+
self.class.wildcard? || super
|
97
|
+
end
|
98
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: keyword_builder
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- DMM Eikaiwa
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-04-08 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: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
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: byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
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: rspec_junit_formatter
|
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
|
+
description:
|
84
|
+
email:
|
85
|
+
- dev@iknow.jp
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".circleci/config.yml"
|
91
|
+
- ".gitignore"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- keyword_builder.gemspec
|
99
|
+
- lib/keyword_builder.rb
|
100
|
+
- lib/keyword_builder/version.rb
|
101
|
+
homepage: https://github.com/iknow/keyword_builder
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata: {}
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
require_paths:
|
108
|
+
- lib
|
109
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
+
requirements:
|
111
|
+
- - ">="
|
112
|
+
- !ruby/object:Gem::Version
|
113
|
+
version: '0'
|
114
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
requirements: []
|
120
|
+
rubygems_version: 3.0.3
|
121
|
+
signing_key:
|
122
|
+
specification_version: 4
|
123
|
+
summary: DSL builder interface to wrap keyword constructors.
|
124
|
+
test_files: []
|