option_initializer 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +7 -0
- data/lib/option_initializer.rb +69 -0
- data/lib/option_initializer/version.rb +3 -0
- data/option_initializer.gemspec +19 -0
- data/test/test_option_initializer.rb +62 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Junegunn Choi
|
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,38 @@
|
|
1
|
+
# option_initializer
|
2
|
+
|
3
|
+
Provides syntactic sugar for constructing objects with method chaining.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```
|
8
|
+
gem install option_initializer
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'option_initializer'
|
15
|
+
|
16
|
+
class Person
|
17
|
+
include OptionInitializer
|
18
|
+
option_initializer :id, :name, :age
|
19
|
+
|
20
|
+
def initialize opts
|
21
|
+
@options = opts
|
22
|
+
end
|
23
|
+
|
24
|
+
def say_hello
|
25
|
+
puts "Hi, I'm #{@options[:name]}!"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Then
|
30
|
+
john = Person.name('John Doe').age(19).id(1000).new
|
31
|
+
|
32
|
+
# becomes equivalent to
|
33
|
+
john = Person.new :id => 1000, :name => 'John Doe', :age => 19
|
34
|
+
|
35
|
+
# Method call shortcut
|
36
|
+
Person.name('John Doe').age(19).id(1000).say_hello
|
37
|
+
```
|
38
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
require "option_initializer/version"
|
2
|
+
|
3
|
+
module OptionInitializer
|
4
|
+
def self.included base
|
5
|
+
base.const_set :OptionInitializing, Class.new {
|
6
|
+
attr_reader :options
|
7
|
+
alias to_h options
|
8
|
+
|
9
|
+
def initialize base, options
|
10
|
+
@base = base
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def new *args
|
15
|
+
args = args.dup
|
16
|
+
opts = @options
|
17
|
+
|
18
|
+
# Convention. Deal with it.
|
19
|
+
if args.last.is_a?(Hash)
|
20
|
+
args[-1] = opts.merge(args.last)
|
21
|
+
else
|
22
|
+
args << opts.dup
|
23
|
+
end
|
24
|
+
|
25
|
+
@base.new(*args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def merge opts
|
29
|
+
self.class.new @base, @options.merge(opts)
|
30
|
+
end
|
31
|
+
|
32
|
+
def method_missing sym, *args
|
33
|
+
# 1.8
|
34
|
+
if @base.instance_methods.map(&:to_sym).include?(sym)
|
35
|
+
@base.new(@options.dup).send sym, *args
|
36
|
+
else
|
37
|
+
raise NoMethodError, "undefined method `#{sym}' for #{self}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
} unless base.constants.include?(:OptionInitializing)
|
41
|
+
|
42
|
+
base.class_eval do
|
43
|
+
def base.option_initializer *syms
|
44
|
+
oi = self.const_get(:OptionInitializing)
|
45
|
+
|
46
|
+
# Class methods
|
47
|
+
syms.each do |sym|
|
48
|
+
self.class_eval do
|
49
|
+
# define_singleton_method not available on 1.8
|
50
|
+
singleton = class << self; self end
|
51
|
+
singleton.send :define_method, sym do |v|
|
52
|
+
oi.new self, sym => v
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Instance methods
|
58
|
+
oi.class_eval do
|
59
|
+
syms.each do |sym|
|
60
|
+
define_method(sym) do |v|
|
61
|
+
merge(sym => v)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'option_initializer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "option_initializer"
|
8
|
+
gem.version = OptionInitializer::VERSION
|
9
|
+
gem.authors = ["Junegunn Choi"]
|
10
|
+
gem.email = ["junegunn.c@gmail.com"]
|
11
|
+
gem.description = %q{Object construction with method chaining}
|
12
|
+
gem.summary = %q{Object construction with method chaining}
|
13
|
+
gem.homepage = "https://github.com/junegunn/option_initializer"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
$VERBOSE = true
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'minitest/autorun'
|
5
|
+
require 'option_initializer'
|
6
|
+
|
7
|
+
class MyClass
|
8
|
+
include OptionInitializer
|
9
|
+
option_initializer :aaa, :bbb
|
10
|
+
option_initializer :ccc
|
11
|
+
|
12
|
+
attr_reader :a, :b, :options
|
13
|
+
|
14
|
+
def initialize a, b, opts
|
15
|
+
@a = a
|
16
|
+
@b = b
|
17
|
+
@options = opts
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class MyClass2
|
22
|
+
include OptionInitializer
|
23
|
+
option_initializer :aaa, :bbb, :ccc
|
24
|
+
|
25
|
+
def initialize options
|
26
|
+
@options = options
|
27
|
+
end
|
28
|
+
|
29
|
+
def num_options bool
|
30
|
+
@options.length if bool
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class TestOptionInitializer < MiniTest::Unit::TestCase
|
35
|
+
def test_oi
|
36
|
+
o = MyClass.aaa(1).bbb(2).ccc(3).new(1, 2)
|
37
|
+
assert_equal 1, o.options[:aaa]
|
38
|
+
assert_equal 2, o.options[:bbb]
|
39
|
+
assert_equal 3, o.options[:ccc]
|
40
|
+
assert_equal 1, o.a
|
41
|
+
assert_equal 2, o.b
|
42
|
+
|
43
|
+
o = MyClass.aaa(1).bbb(2).ccc(3).aaa(4).new(1, 2, :ddd => 4)
|
44
|
+
assert_equal 4, o.options[:aaa]
|
45
|
+
assert_equal 2, o.options[:bbb]
|
46
|
+
assert_equal 3, o.options[:ccc]
|
47
|
+
assert_equal 4, o.options[:ddd]
|
48
|
+
assert_equal 1, o.a
|
49
|
+
assert_equal 2, o.b
|
50
|
+
|
51
|
+
assert_instance_of MyClass::OptionInitializing, MyClass.aaa(1)
|
52
|
+
|
53
|
+
assert_raises(ArgumentError) { MyClass.aaa(1, 2) }
|
54
|
+
assert_raises(ArgumentError) { MyClass.aaa(1).new(1) }
|
55
|
+
assert_raises(ArgumentError) { MyClass.aaa(1).new(1, 2, 3) }
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_method_missing
|
59
|
+
assert_equal 2, MyClass2.aaa(1).bbb(2).num_options(true)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: option_initializer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Junegunn Choi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Object construction with method chaining
|
15
|
+
email:
|
16
|
+
- junegunn.c@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/option_initializer.rb
|
27
|
+
- lib/option_initializer/version.rb
|
28
|
+
- option_initializer.gemspec
|
29
|
+
- test/test_option_initializer.rb
|
30
|
+
homepage: https://github.com/junegunn/option_initializer
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.24
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Object construction with method chaining
|
54
|
+
test_files:
|
55
|
+
- test/test_option_initializer.rb
|
56
|
+
has_rdoc:
|