method_struct 0.2.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 759afa62052f554daf31d700adf7f69bc3f15655
4
- data.tar.gz: 334a8eaf98c5aa21302f61e35f836c8ced75c0a3
3
+ metadata.gz: faab59cbf9a78b593cae7f255ae43932fa51d829
4
+ data.tar.gz: bb14949700c7b102b51fafbef4f85c403b0cc8a9
5
5
  SHA512:
6
- metadata.gz: 07461e8742e57cf53bea6e84c2a0d1433fe6336858cc094e70186abac378193446936b0a37ea9fa28d0e5150a15d4c574e187b4e86bb7ec9fbf61cf55430b587
7
- data.tar.gz: 38807e3209841db570081cb5e930a100996ee4f72735938801c3f772953184e4c27e23185539a006f162ed88b59b1dcc04a2e20ffff25b67997b71551e0316c3
6
+ metadata.gz: fdf24cfa6f0b92286a1414328853ef3f54c2b991d788a306a0fb3b048ae1052d90e3cef2067eee77cebd97448879ee33137ffb0e4360ec44ed3b43bf1c02713d
7
+ data.tar.gz: 4d14e2c96eca0876d166a0e1e16caab4d607c2ef8bd256c3a9feec30facac3d2d9d07f8114aab2ddc545875fce44c316af0597b018d724cd85d7b4dd8e0f3458
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 0.2.1
2
+ ~ Fixes in how argument verification deals with hashes
3
+
1
4
  0.2.0
2
5
  + Allow user functions to overwrite the generated attr readers
3
6
  + Require_all option that checks if all arguments have been provided on
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
- ArgumentVerifier.new(fields, values, require_all, require_presence).verify
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
- if fields.size > 1 && values.size == 1 && values.first.is_a?(Hash)
39
- fields.each do |field|
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(fields, values, require_all, require_presence)
4
- @fields, @values = fields, values
5
- @require_all, @require_presence = require_all, require_presence
3
+ def initialize(options)
4
+ @options = options
6
5
  end
7
6
 
8
- def verify
9
- if fields.size > 1 && values.size == 1 && values.first.is_a?(Hash)
10
- verify_hash
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 :fields, :values, :require_all, :require_presence
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
- if require_all && !(expected - provided).empty?
24
- raise ArgumentError.new("wrong arguments provided")
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
- if require_presence && !expected.all? { |arg| values.first[arg] || values.first[arg.to_s] }
28
- raise ArgumentError.new("nil arguments provided")
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 verify_normal
33
- if require_all && fields.count != values.count
34
- raise ArgumentError.new("wrong number of arguments (#{values.count} for #{fields.count})")
35
- end
29
+ def fields
30
+ options.fetch(:fields)
31
+ end
36
32
 
37
- if require_presence && fields.count != values.compact.count
38
- raise ArgumentError.new("nil arguments provided")
39
- end
33
+ def arguments
34
+ options.fetch(:arguments)
40
35
  end
41
36
  end
42
37
  end
@@ -1,3 +1,3 @@
1
1
  module MethodStruct
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -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 uneql for unequal arguments" do
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.0
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-21 00:00:00.000000000 Z
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