method_struct 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e0b38691d962c63d8ebb037565c81361f44f3b2b
4
- data.tar.gz: f3bcf9cf908e6ead363a94ef5fb954ce88f3dbd1
3
+ metadata.gz: 759afa62052f554daf31d700adf7f69bc3f15655
4
+ data.tar.gz: 334a8eaf98c5aa21302f61e35f836c8ced75c0a3
5
5
  SHA512:
6
- metadata.gz: 6cfec0c5eb4782d6455bf98273b1225c69810197f097f4a37d1e58690f9b5e7844fee9de430c4fc9837cc9ee2cf275a04553b983dfe7247b7749678b6f10288c
7
- data.tar.gz: fb802dde5e883e65fe7ccb69a9b589b1ab9efaeaa05643f395ae9a41195e3511e119536a94da2630453cdefeec8feef9506d07e5d44b32a81350bba7566329d6
6
+ metadata.gz: 07461e8742e57cf53bea6e84c2a0d1433fe6336858cc094e70186abac378193446936b0a37ea9fa28d0e5150a15d4c574e187b4e86bb7ec9fbf61cf55430b587
7
+ data.tar.gz: 38807e3209841db570081cb5e930a100996ee4f72735938801c3f772953184e4c27e23185539a006f162ed88b59b1dcc04a2e20ffff25b67997b71551e0316c3
data/CHANGELOG CHANGED
@@ -1,3 +1,11 @@
1
+ 0.2.0
2
+ + Allow user functions to overwrite the generated attr readers
3
+ + Require_all option that checks if all arguments have been provided on
4
+ initialization
5
+ + Require_presence option that checks if all arguments are non-nil on
6
+ initialization
7
+ + Configurable defaults for the options
8
+
1
9
  0.1.5
2
10
  + Support blocks
3
11
 
data/README.md CHANGED
@@ -79,7 +79,16 @@ class UsersController
79
79
  end
80
80
  ```
81
81
 
82
- One hopes the benefits will be more obvious for more complex methods
82
+ You can use `:require_all => true` if you want to verify that all arguments
83
+ have been specified or `:require_presence => true` to verify that all arguments
84
+ are non-nil. Global defaults for these options can be changed like so:
85
+
86
+ ```ruby
87
+ MethodStruct::Defaults.set(
88
+ :require_presence => true,
89
+ :method_name => :do_it
90
+ )
91
+ ```
83
92
 
84
93
  ## Contributing
85
94
 
@@ -0,0 +1,42 @@
1
+ module MethodStruct
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
6
+ end
7
+
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
14
+ end
15
+
16
+ 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
22
+
23
+ if require_all && !(expected - provided).empty?
24
+ raise ArgumentError.new("wrong arguments provided")
25
+ end
26
+
27
+ if require_presence && !expected.all? { |arg| values.first[arg] || values.first[arg.to_s] }
28
+ raise ArgumentError.new("nil arguments provided")
29
+ end
30
+ end
31
+
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
36
+
37
+ if require_presence && fields.count != values.compact.count
38
+ raise ArgumentError.new("nil arguments provided")
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,31 @@
1
+ require "singleton"
2
+
3
+ module MethodStruct
4
+ class Defaults
5
+ include Singleton
6
+
7
+ def self.set(options)
8
+ instance.set(options)
9
+ end
10
+
11
+ def self.get
12
+ instance.get
13
+ end
14
+
15
+ def initialize
16
+ @defaults = {
17
+ :method_name => :call,
18
+ :require_all => false,
19
+ :require_presence => false,
20
+ }
21
+ end
22
+
23
+ def set(options)
24
+ @defaults = @defaults.merge(options)
25
+ end
26
+
27
+ def get
28
+ @defaults
29
+ end
30
+ end
31
+ end
@@ -1,3 +1,3 @@
1
1
  module MethodStruct
2
- VERSION = "0.1.5"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/method_struct.rb CHANGED
@@ -1,14 +1,20 @@
1
1
  require "method_struct/version"
2
+ require "method_struct/defaults"
3
+ require "method_struct/argument_verifier"
2
4
 
3
5
  module MethodStruct
4
6
  def self.new(*fields, &block)
5
7
  if fields.last.is_a?(Hash)
6
- method_name = fields.last[:method_name]
8
+ options = fields.last
7
9
  fields = fields.take(fields.size - 1)
8
10
  else
9
- method_name = :call
11
+ options = {}
10
12
  end
11
13
 
14
+ method_name = options.fetch(:method_name, Defaults.get[:method_name])
15
+ require_all = options.fetch(:require_all, Defaults.get[:require_all])
16
+ require_presence = options.fetch(:require_presence, Defaults.get[:require_presence])
17
+
12
18
  Class.new do
13
19
  singleton_class = (class << self; self; end)
14
20
 
@@ -27,6 +33,8 @@ module MethodStruct
27
33
  end
28
34
 
29
35
  define_method(:initialize) do |*values|
36
+ ArgumentVerifier.new(fields, values, require_all, require_presence).verify
37
+
30
38
  if fields.size > 1 && values.size == 1 && values.first.is_a?(Hash)
31
39
  fields.each do |field|
32
40
  instance_variable_set("@#{field}", values.first[field])
@@ -49,12 +57,12 @@ module MethodStruct
49
57
  fields.map { |field| send(field).hash }.inject(&:^)
50
58
  end
51
59
 
52
- class_eval(&block) if block_given?
53
-
54
- protected
55
60
  fields.each do |field|
56
61
  attr_reader(field)
62
+ protected field
57
63
  end
64
+
65
+ class_eval(&block) if block_given?
58
66
  end
59
67
  end
60
68
  end
@@ -16,6 +16,16 @@ describe MethodStruct do
16
16
  end
17
17
  end
18
18
 
19
+ WithDefault = MethodStruct.new(:x, :y) do
20
+ def call
21
+ [x, y]
22
+ end
23
+
24
+ def x
25
+ @x ||= 'default'
26
+ end
27
+ end
28
+
19
29
  it "creates a class method which calls the declared instance method with the given context" do
20
30
  verifier.should_receive(:poke).with(argument1, argument2)
21
31
  create_poker(verifier).call(argument1, argument2)
@@ -71,6 +81,38 @@ describe MethodStruct do
71
81
  end
72
82
  end
73
83
 
84
+ context "when :require_all => true" do
85
+ let(:klass) { MethodStruct.new(:x, :y, :require_all => true) }
86
+
87
+ it "does not allow creation without all arguments" do
88
+ expect { klass.new(nil) }.to raise_error(ArgumentError)
89
+ end
90
+
91
+ it "does not allow creation without all hash arguments" do
92
+ expect { klass.new(:y => nil) }.to raise_error(ArgumentError)
93
+ end
94
+
95
+ it "allows creation with all nil arguments" do
96
+ expect { klass.new(nil, nil) }.not_to raise_error
97
+ end
98
+ end
99
+
100
+ context "when :require_presence => true" do
101
+ let(:klass) { MethodStruct.new(:x, :y, :require_presence => true) }
102
+
103
+ it "does not allow creation without all arguments being non-nil" do
104
+ expect { klass.new(1, nil) }.to raise_error(ArgumentError)
105
+ end
106
+
107
+ 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)
109
+ end
110
+
111
+ it "allows creation with all arguments provided" do
112
+ expect { klass.new(1, 2) }.not_to raise_error
113
+ end
114
+ end
115
+
74
116
  it "allows for additional methods defined with a block" do
75
117
  klass = MethodStruct.new(:x) do
76
118
  def something
@@ -135,5 +177,10 @@ describe MethodStruct do
135
177
  poker.call(argument1)
136
178
  end
137
179
  end
180
+
181
+ it 'allows overriding default attr_readers' do
182
+ WithDefault.call(:x => argument1, :y => argument2).should eq([argument1, argument2])
183
+ WithDefault.call(:y => argument2).should eq(['default', argument2])
184
+ end
138
185
  end
139
186
  end
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.1.5
4
+ version: 0.2.0
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-20 00:00:00.000000000 Z
11
+ date: 2014-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -67,6 +67,8 @@ files:
67
67
  - README.md
68
68
  - Rakefile
69
69
  - lib/method_struct.rb
70
+ - lib/method_struct/argument_verifier.rb
71
+ - lib/method_struct/defaults.rb
70
72
  - lib/method_struct/version.rb
71
73
  - method_struct.gemspec
72
74
  - spec/method_struct_spec.rb