option_initializer 1.0.1 → 1.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.
- data/README.md +20 -5
- data/lib/option_initializer.rb +34 -7
- data/lib/option_initializer/version.rb +1 -1
- data/test/test_option_initializer.rb +33 -4
- metadata +2 -2
data/README.md
CHANGED
@@ -15,24 +15,39 @@ require 'option_initializer'
|
|
15
15
|
|
16
16
|
class Person
|
17
17
|
include OptionInitializer
|
18
|
-
option_initializer :id, :name, :age
|
18
|
+
option_initializer :id, :name, :age, :greetings
|
19
19
|
|
20
20
|
def initialize opts
|
21
21
|
@options = opts
|
22
22
|
end
|
23
23
|
|
24
24
|
def say_hello
|
25
|
-
puts
|
25
|
+
puts @options[:greetings].call @options[:name]
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
29
|
# Then
|
30
|
-
john = Person.
|
30
|
+
john = Person.
|
31
|
+
name('John Doe').
|
32
|
+
age(19).
|
33
|
+
greetings { |name| "Hi, I'm #{name}!" }.
|
34
|
+
id(1000).
|
35
|
+
new
|
31
36
|
|
32
37
|
# becomes equivalent to
|
33
|
-
john = Person.new
|
38
|
+
john = Person.new(
|
39
|
+
:id => 1000,
|
40
|
+
:name => 'John Doe',
|
41
|
+
:age => 19,
|
42
|
+
:greetings => proc { |name| "Hi, I'm #{name}!" }
|
43
|
+
)
|
34
44
|
|
35
45
|
# Method call shortcut
|
36
|
-
Person.
|
46
|
+
Person.
|
47
|
+
name('John Doe').
|
48
|
+
age(19).
|
49
|
+
greetings { |name| "Hi, I'm #{name}!" }.
|
50
|
+
id(1000).
|
51
|
+
say_hello
|
37
52
|
```
|
38
53
|
|
data/lib/option_initializer.rb
CHANGED
@@ -11,7 +11,7 @@ module OptionInitializer
|
|
11
11
|
@options = options
|
12
12
|
end
|
13
13
|
|
14
|
-
def new *args
|
14
|
+
def new *args, &block
|
15
15
|
args = args.dup
|
16
16
|
opts = @options
|
17
17
|
|
@@ -22,7 +22,7 @@ module OptionInitializer
|
|
22
22
|
args << opts.dup
|
23
23
|
end
|
24
24
|
|
25
|
-
@base.new(*args)
|
25
|
+
@base.new(*args, &block)
|
26
26
|
end
|
27
27
|
|
28
28
|
def merge opts
|
@@ -37,9 +37,14 @@ module OptionInitializer
|
|
37
37
|
raise NoMethodError, "undefined method `#{sym}' for #{self}"
|
38
38
|
end
|
39
39
|
end
|
40
|
-
} unless base.constants.include?(:OptionInitializing)
|
40
|
+
} unless base.constants.map(&:to_sym).include?(:OptionInitializing)
|
41
41
|
|
42
42
|
base.class_eval do
|
43
|
+
class << self
|
44
|
+
if method_defined?(:option_initializer)
|
45
|
+
undef_method(:option_initializer)
|
46
|
+
end
|
47
|
+
end
|
43
48
|
def base.option_initializer *syms
|
44
49
|
oi = self.const_get(:OptionInitializing)
|
45
50
|
|
@@ -48,8 +53,19 @@ module OptionInitializer
|
|
48
53
|
self.class_eval do
|
49
54
|
# define_singleton_method not available on 1.8
|
50
55
|
singleton = class << self; self end
|
51
|
-
singleton.send :
|
52
|
-
|
56
|
+
singleton.send :undef_method, sym if singleton.method_defined?(sym)
|
57
|
+
singleton.send :define_method, sym do |*v, &b|
|
58
|
+
if b && v.empty?
|
59
|
+
oi.new self, sym => b
|
60
|
+
elsif b && !v.empty?
|
61
|
+
raise ArgumentError,
|
62
|
+
"wrong number of arguments (#{v.length} for 0 when block given)"
|
63
|
+
elsif v.length == 1
|
64
|
+
oi.new self, sym => v.first
|
65
|
+
else
|
66
|
+
raise ArgumentError,
|
67
|
+
"wrong number of arguments (#{v.length} for 1)"
|
68
|
+
end
|
53
69
|
end
|
54
70
|
end
|
55
71
|
end
|
@@ -57,8 +73,19 @@ module OptionInitializer
|
|
57
73
|
# Instance methods
|
58
74
|
oi.class_eval do
|
59
75
|
syms.each do |sym|
|
60
|
-
|
61
|
-
|
76
|
+
undef_method(sym) if method_defined?(sym)
|
77
|
+
define_method(sym) do |*v, &b|
|
78
|
+
if b && v.empty?
|
79
|
+
merge(sym => b)
|
80
|
+
elsif b && !v.empty?
|
81
|
+
raise ArgumentError,
|
82
|
+
"wrong number of arguments (#{v.length} for 0 when block given)"
|
83
|
+
elsif v.length == 1
|
84
|
+
merge(sym => v.first)
|
85
|
+
else
|
86
|
+
raise ArgumentError,
|
87
|
+
"wrong number of arguments (#{v.length} for 1)"
|
88
|
+
end
|
62
89
|
end
|
63
90
|
end
|
64
91
|
end
|
@@ -7,19 +7,24 @@ require 'option_initializer'
|
|
7
7
|
class MyClass
|
8
8
|
include OptionInitializer
|
9
9
|
option_initializer :aaa, :bbb
|
10
|
-
option_initializer :ccc
|
10
|
+
option_initializer :ccc, :ddd
|
11
|
+
option_initializer :ccc, :ddd
|
11
12
|
|
12
|
-
attr_reader :a, :b, :options
|
13
|
+
attr_reader :a, :b, :options, :y
|
13
14
|
|
14
15
|
def initialize a, b, opts
|
15
16
|
@a = a
|
16
17
|
@b = b
|
17
18
|
@options = opts
|
19
|
+
|
20
|
+
@y = yield if block_given?
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
21
24
|
class MyClass2
|
22
25
|
include OptionInitializer
|
26
|
+
include OptionInitializer
|
27
|
+
|
23
28
|
option_initializer :aaa, :bbb, :ccc
|
24
29
|
|
25
30
|
def initialize options
|
@@ -35,25 +40,43 @@ class MyClass2
|
|
35
40
|
end
|
36
41
|
end
|
37
42
|
|
43
|
+
# Excerpt from README
|
44
|
+
class Person
|
45
|
+
include OptionInitializer
|
46
|
+
option_initializer :id, :name, :age, :greetings
|
47
|
+
|
48
|
+
def initialize opts
|
49
|
+
@options = opts
|
50
|
+
end
|
51
|
+
|
52
|
+
def say_hello
|
53
|
+
puts @options[:greetings].call @options[:name]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
38
57
|
class TestOptionInitializer < MiniTest::Unit::TestCase
|
39
58
|
def test_oi
|
40
|
-
o = MyClass.aaa(1).bbb(2).ccc(3).new(1, 2)
|
59
|
+
o = MyClass.aaa(1).bbb(2).ddd { 4 }.ccc(3).new(1, 2)
|
41
60
|
assert_equal 1, o.options[:aaa]
|
42
61
|
assert_equal 2, o.options[:bbb]
|
43
62
|
assert_equal 3, o.options[:ccc]
|
63
|
+
assert_equal 4, o.options[:ddd].call
|
44
64
|
assert_equal 1, o.a
|
45
65
|
assert_equal 2, o.b
|
66
|
+
assert_equal nil, o.y
|
46
67
|
|
47
|
-
o = MyClass.aaa(1).bbb(2).ccc(3).aaa(4).new(1, 2, :ddd => 4)
|
68
|
+
o = MyClass.aaa(1).bbb(2).ccc(3).aaa(4).new(1, 2, :ddd => 4) { :y }
|
48
69
|
assert_equal 4, o.options[:aaa]
|
49
70
|
assert_equal 2, o.options[:bbb]
|
50
71
|
assert_equal 3, o.options[:ccc]
|
51
72
|
assert_equal 4, o.options[:ddd]
|
52
73
|
assert_equal 1, o.a
|
53
74
|
assert_equal 2, o.b
|
75
|
+
assert_equal :y, o.y
|
54
76
|
|
55
77
|
assert_instance_of MyClass::OptionInitializing, MyClass.aaa(1)
|
56
78
|
|
79
|
+
assert_raises(ArgumentError) { MyClass.aaa(1) { 1 } }
|
57
80
|
assert_raises(ArgumentError) { MyClass.aaa(1, 2) }
|
58
81
|
assert_raises(ArgumentError) { MyClass.aaa(1).new(1) }
|
59
82
|
assert_raises(ArgumentError) { MyClass.aaa(1).new(1, 2, 3) }
|
@@ -63,5 +86,11 @@ class TestOptionInitializer < MiniTest::Unit::TestCase
|
|
63
86
|
assert_equal 2, MyClass2.aaa(1).bbb(2).num_options(true)
|
64
87
|
assert_equal 2, MyClass2.aaa(1).bbb(2).echo(1) { |a| a * 2 }
|
65
88
|
end
|
89
|
+
|
90
|
+
def test_readme
|
91
|
+
john = Person.name('John Doe').age(19).greetings { |name| "Hi, I'm #{name}!" }.id(1000).new
|
92
|
+
john = Person.new :id => 1000, :name => 'John Doe', :age => 19, :greetings => proc { |name| "Hi, I'm #{name}!" }
|
93
|
+
Person.name('John Doe').age(19).greetings { |name| "Hi, I'm #{name}!" }.id(1000).say_hello
|
94
|
+
end
|
66
95
|
end
|
67
96
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: option_initializer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-25 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Object construction with method chaining
|
15
15
|
email:
|