method_struct 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG +3 -0
- data/lib/method_struct.rb +14 -9
- data/lib/method_struct/argument_parser.rb +41 -0
- data/lib/method_struct/argument_verifier.rb +20 -25
- data/lib/method_struct/version.rb +1 -1
- data/spec/method_struct_spec.rb +26 -5
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: faab59cbf9a78b593cae7f255ae43932fa51d829
|
4
|
+
data.tar.gz: bb14949700c7b102b51fafbef4f85c403b0cc8a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdf24cfa6f0b92286a1414328853ef3f54c2b991d788a306a0fb3b048ae1052d90e3cef2067eee77cebd97448879ee33137ffb0e4360ec44ed3b43bf1c02713d
|
7
|
+
data.tar.gz: 4d14e2c96eca0876d166a0e1e16caab4d607c2ef8bd256c3a9feec30facac3d2d9d07f8114aab2ddc545875fce44c316af0597b018d724cd85d7b4dd8e0f3458
|
data/CHANGELOG
CHANGED
data/lib/method_struct.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require "method_struct/version"
|
2
2
|
require "method_struct/defaults"
|
3
3
|
require "method_struct/argument_verifier"
|
4
|
+
require "method_struct/argument_parser"
|
4
5
|
|
5
6
|
module MethodStruct
|
6
7
|
def self.new(*fields, &block)
|
@@ -11,6 +12,11 @@ module MethodStruct
|
|
11
12
|
options = {}
|
12
13
|
end
|
13
14
|
|
15
|
+
unless fields.all?{ |f| f.is_a?(Symbol) }
|
16
|
+
invalid_fields = fields.select{ |f| !f.is_a?(Symbol) }
|
17
|
+
raise ArgumentError, "only symbol fields allowed: #{invalid_fields.inspect}"
|
18
|
+
end
|
19
|
+
|
14
20
|
method_name = options.fetch(:method_name, Defaults.get[:method_name])
|
15
21
|
require_all = options.fetch(:require_all, Defaults.get[:require_all])
|
16
22
|
require_presence = options.fetch(:require_presence, Defaults.get[:require_presence])
|
@@ -33,16 +39,15 @@ module MethodStruct
|
|
33
39
|
end
|
34
40
|
|
35
41
|
define_method(:initialize) do |*values|
|
36
|
-
|
42
|
+
arguments = ArgumentParser.new(
|
43
|
+
:fields => fields,
|
44
|
+
:raw_arguments => values,
|
45
|
+
:require_all => require_all,
|
46
|
+
:require_presence => require_presence
|
47
|
+
).call
|
37
48
|
|
38
|
-
|
39
|
-
|
40
|
-
instance_variable_set("@#{field}", values.first[field])
|
41
|
-
end
|
42
|
-
else
|
43
|
-
fields.zip(values).each do |field, value|
|
44
|
-
instance_variable_set("@#{field}", value)
|
45
|
-
end
|
49
|
+
arguments.each do |field, value|
|
50
|
+
instance_variable_set("@#{field}", value)
|
46
51
|
end
|
47
52
|
end
|
48
53
|
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module MethodStruct
|
2
|
+
class ArgumentParser
|
3
|
+
def initialize(options)
|
4
|
+
@options = options
|
5
|
+
end
|
6
|
+
|
7
|
+
def call
|
8
|
+
parsed_arguments.tap do |args|
|
9
|
+
ArgumentVerifier.new(options.merge(:arguments => args)).call
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
attr_reader :options
|
15
|
+
|
16
|
+
def fields
|
17
|
+
options.fetch(:fields)
|
18
|
+
end
|
19
|
+
|
20
|
+
def raw_arguments
|
21
|
+
options.fetch(:raw_arguments)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parsed_arguments
|
25
|
+
{}.tap do |h|
|
26
|
+
fields.each do |field|
|
27
|
+
h[field] = raw_arguments_hash[field] if raw_arguments_hash.key?(field)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def raw_arguments_hash
|
33
|
+
@raw_arguments_hash ||= if fields.size > 1 && raw_arguments.size == 1 && raw_arguments.first.is_a?(Hash)
|
34
|
+
raw_arguments.first
|
35
|
+
else
|
36
|
+
zipped_fields = fields.take(raw_arguments.count)
|
37
|
+
Hash[*zipped_fields.zip(raw_arguments).flatten(1)]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,42 +1,37 @@
|
|
1
1
|
module MethodStruct
|
2
2
|
class ArgumentVerifier
|
3
|
-
def initialize(
|
4
|
-
@
|
5
|
-
@require_all, @require_presence = require_all, require_presence
|
3
|
+
def initialize(options)
|
4
|
+
@options = options
|
6
5
|
end
|
7
6
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
else
|
12
|
-
verify_normal
|
13
|
-
end
|
7
|
+
def call
|
8
|
+
verify_require_all
|
9
|
+
verify_require_presence
|
14
10
|
end
|
15
11
|
|
16
12
|
private
|
17
|
-
attr_reader :
|
18
|
-
|
19
|
-
def verify_hash
|
20
|
-
expected = fields.map(&:to_s).sort
|
21
|
-
provided = values.first.keys.map(&:to_s).sort
|
13
|
+
attr_reader :options
|
22
14
|
|
23
|
-
|
24
|
-
|
15
|
+
def verify_require_all
|
16
|
+
if options.fetch(:require_all)
|
17
|
+
missing_arguments = fields - arguments.keys
|
18
|
+
raise ArgumentError.new("missing arguments: #{missing_arguments.sort.inspect}") unless missing_arguments.empty?
|
25
19
|
end
|
20
|
+
end
|
26
21
|
|
27
|
-
|
28
|
-
|
22
|
+
def verify_require_presence
|
23
|
+
if options.fetch(:require_presence)
|
24
|
+
nil_arguments = arguments.map{ |k, v| k if v.nil? }.compact
|
25
|
+
raise ArgumentError.new("nil arguments: #{nil_arguments.sort.inspect}") unless nil_arguments.empty?
|
29
26
|
end
|
30
27
|
end
|
31
28
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
35
|
-
end
|
29
|
+
def fields
|
30
|
+
options.fetch(:fields)
|
31
|
+
end
|
36
32
|
|
37
|
-
|
38
|
-
|
39
|
-
end
|
33
|
+
def arguments
|
34
|
+
options.fetch(:arguments)
|
40
35
|
end
|
41
36
|
end
|
42
37
|
end
|
data/spec/method_struct_spec.rb
CHANGED
@@ -26,6 +26,11 @@ describe MethodStruct do
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
it 'does not allow definition with strings' do
|
30
|
+
expect{ MethodStruct.new('x', 'y') }.to raise_error(
|
31
|
+
ArgumentError, 'only symbol fields allowed: ["x", "y"]')
|
32
|
+
end
|
33
|
+
|
29
34
|
it "creates a class method which calls the declared instance method with the given context" do
|
30
35
|
verifier.should_receive(:poke).with(argument1, argument2)
|
31
36
|
create_poker(verifier).call(argument1, argument2)
|
@@ -85,11 +90,15 @@ describe MethodStruct do
|
|
85
90
|
let(:klass) { MethodStruct.new(:x, :y, :require_all => true) }
|
86
91
|
|
87
92
|
it "does not allow creation without all arguments" do
|
88
|
-
expect { klass.new(nil) }.to raise_error(ArgumentError)
|
93
|
+
expect { klass.new(nil) }.to raise_error(ArgumentError, 'missing arguments: [:y]')
|
89
94
|
end
|
90
95
|
|
91
96
|
it "does not allow creation without all hash arguments" do
|
92
|
-
expect { klass.new(:y => nil) }.to raise_error(ArgumentError)
|
97
|
+
expect { klass.new(:y => nil) }.to raise_error(ArgumentError, 'missing arguments: [:x]')
|
98
|
+
end
|
99
|
+
|
100
|
+
it "allows creation with all nil hash arguments" do
|
101
|
+
expect { klass.new(:x => nil, :y => nil) }.not_to raise_error
|
93
102
|
end
|
94
103
|
|
95
104
|
it "allows creation with all nil arguments" do
|
@@ -101,11 +110,23 @@ describe MethodStruct do
|
|
101
110
|
let(:klass) { MethodStruct.new(:x, :y, :require_presence => true) }
|
102
111
|
|
103
112
|
it "does not allow creation without all arguments being non-nil" do
|
104
|
-
expect { klass.new(1, nil) }.to raise_error(ArgumentError)
|
113
|
+
expect { klass.new(1, nil) }.to raise_error(ArgumentError, 'nil arguments: [:y]')
|
105
114
|
end
|
106
115
|
|
107
116
|
it "does not allow creation without all hash arguments being non-nil" do
|
108
|
-
expect { klass.new(:x => 1, :y => nil) }.to raise_error(ArgumentError)
|
117
|
+
expect { klass.new(:x => 1, :y => nil) }.to raise_error(ArgumentError, 'nil arguments: [:y]')
|
118
|
+
end
|
119
|
+
|
120
|
+
it "allows creation with boolean hash arguments provided" do
|
121
|
+
expect { klass.new(:x => false, :y => false) }.not_to raise_error
|
122
|
+
end
|
123
|
+
|
124
|
+
it "allows creation with boolean hash arguments provided" do
|
125
|
+
expect { klass.new(false, false) }.not_to raise_error
|
126
|
+
end
|
127
|
+
|
128
|
+
it "allows creation with all hash symbol arguments provided" do
|
129
|
+
expect { klass.new(:x => 1, :y => 2) }.not_to raise_error
|
109
130
|
end
|
110
131
|
|
111
132
|
it "allows creation with all arguments provided" do
|
@@ -142,7 +163,7 @@ describe MethodStruct do
|
|
142
163
|
expect(struct.new(argument1, argument2) == struct.new(argument2, argument1)).to be_false
|
143
164
|
end
|
144
165
|
|
145
|
-
it "is
|
166
|
+
it "is unequal for unequal arguments" do
|
146
167
|
expect(struct.new(argument1, argument2).eql?(struct.new(argument2, argument1))).to be_false
|
147
168
|
end
|
148
169
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: method_struct
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paweł Obrok
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -67,6 +67,7 @@ files:
|
|
67
67
|
- README.md
|
68
68
|
- Rakefile
|
69
69
|
- lib/method_struct.rb
|
70
|
+
- lib/method_struct/argument_parser.rb
|
70
71
|
- lib/method_struct/argument_verifier.rb
|
71
72
|
- lib/method_struct/defaults.rb
|
72
73
|
- lib/method_struct/version.rb
|