option_initializer 1.1.0 → 1.1.1
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 +8 -0
- data/lib/option_initializer.rb +23 -2
- data/lib/option_initializer/version.rb +1 -1
- data/test/test_option_initializer.rb +20 -0
- metadata +2 -2
data/README.md
CHANGED
@@ -16,6 +16,14 @@ require 'option_initializer'
|
|
16
16
|
class Person
|
17
17
|
include OptionInitializer
|
18
18
|
option_initializer :id, :name, :age, :greetings
|
19
|
+
option_validator do |k, v|
|
20
|
+
case k
|
21
|
+
when :age
|
22
|
+
raise ArgumentError, "invalid age" if age < 0
|
23
|
+
when :name
|
24
|
+
raise ArgumentError, "invalid name" if name.empty?
|
25
|
+
end
|
26
|
+
end
|
19
27
|
|
20
28
|
def initialize opts
|
21
29
|
@options = opts
|
data/lib/option_initializer.rb
CHANGED
@@ -6,7 +6,10 @@ module OptionInitializer
|
|
6
6
|
attr_reader :options
|
7
7
|
alias to_h options
|
8
8
|
|
9
|
+
const_set :VALIDATORS, []
|
10
|
+
|
9
11
|
def initialize base, options
|
12
|
+
validate options
|
10
13
|
@base = base
|
11
14
|
@options = options
|
12
15
|
end
|
@@ -17,6 +20,7 @@ module OptionInitializer
|
|
17
20
|
|
18
21
|
# Convention. Deal with it.
|
19
22
|
if args.last.is_a?(Hash)
|
23
|
+
validate args.last
|
20
24
|
args[-1] = opts.merge(args.last)
|
21
25
|
else
|
22
26
|
args << opts.dup
|
@@ -26,9 +30,18 @@ module OptionInitializer
|
|
26
30
|
end
|
27
31
|
|
28
32
|
def merge opts
|
33
|
+
validate opts
|
29
34
|
self.class.new @base, @options.merge(opts)
|
30
35
|
end
|
31
36
|
|
37
|
+
def validate hash
|
38
|
+
self.class.const_get(:VALIDATORS).each do |validator|
|
39
|
+
hash.each do |k, v|
|
40
|
+
validator.call k, v
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
32
45
|
def method_missing sym, *args, &block
|
33
46
|
# 1.8
|
34
47
|
if @base.instance_methods.map(&:to_sym).include?(sym)
|
@@ -41,10 +54,18 @@ module OptionInitializer
|
|
41
54
|
|
42
55
|
base.class_eval do
|
43
56
|
class << self
|
44
|
-
|
45
|
-
undef_method(
|
57
|
+
[:option_initializer, :option_validator].each do |m|
|
58
|
+
undef_method(m) if method_defined?(m)
|
46
59
|
end
|
47
60
|
end
|
61
|
+
|
62
|
+
def base.option_validator &block
|
63
|
+
raise ArgumentError, "block must be given" unless block
|
64
|
+
raise ArgumentError, "invalid arity (expected: 2)" unless block.arity == 2
|
65
|
+
oi = self.const_get(:OptionInitializing)
|
66
|
+
oi.const_get(:VALIDATORS).push block
|
67
|
+
end
|
68
|
+
|
48
69
|
def base.option_initializer *syms
|
49
70
|
oi = self.const_get(:OptionInitializing)
|
50
71
|
|
@@ -26,6 +26,18 @@ class MyClass2
|
|
26
26
|
include OptionInitializer
|
27
27
|
|
28
28
|
option_initializer :aaa, :bbb, :ccc
|
29
|
+
option_validator do |k, v|
|
30
|
+
case k
|
31
|
+
when :aaa
|
32
|
+
raise ArgumentError if v == 0
|
33
|
+
end
|
34
|
+
end
|
35
|
+
option_validator do |k, v|
|
36
|
+
case k
|
37
|
+
when :aaa
|
38
|
+
raise ArgumentError if v < 0
|
39
|
+
end
|
40
|
+
end
|
29
41
|
|
30
42
|
def initialize options
|
31
43
|
@options = options
|
@@ -87,6 +99,14 @@ class TestOptionInitializer < MiniTest::Unit::TestCase
|
|
87
99
|
assert_equal 2, MyClass2.aaa(1).bbb(2).echo(1) { |a| a * 2 }
|
88
100
|
end
|
89
101
|
|
102
|
+
def test_validator
|
103
|
+
assert_raises(ArgumentError) { MyClass2.aaa(0) }
|
104
|
+
assert_raises(ArgumentError) { MyClass2.aaa(-1) }
|
105
|
+
assert_raises(ArgumentError) { MyClass2.aaa(1).aaa(-1) }
|
106
|
+
assert_raises(ArgumentError) { MyClass2.aaa(1).aaa(1).new(:aaa => -2) }
|
107
|
+
assert_raises(ArgumentError) { MyClass2.aaa(1).aaa(1).new(:aaa => -2) }
|
108
|
+
end
|
109
|
+
|
90
110
|
def test_readme
|
91
111
|
john = Person.name('John Doe').age(19).greetings { |name| "Hi, I'm #{name}!" }.id(1000).new
|
92
112
|
john = Person.new :id => 1000, :name => 'John Doe', :age => 19, :greetings => proc { |name| "Hi, I'm #{name}!" }
|
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.1.
|
4
|
+
version: 1.1.1
|
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-27 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Object construction with method chaining
|
15
15
|
email:
|