named_parameters 1.0.2 → 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 CHANGED
@@ -136,6 +136,7 @@ Kudos to Juris Galang and his gem named-parameters. (http://www.rubygems.org/gem
136
136
  Defenitely have a look at this gem also, if you want to use named parameters.
137
137
 
138
138
  == Changelog
139
+ [1.1.0] * Added ghost methods "my_method_has_named_parameters"
139
140
  [1.0.2] * Removed unimportant methods from documentation (again)
140
141
  [1.0.1] * Removed unimportant methods from documentation
141
142
  [1.0.0] * Added strict and super modes
@@ -3,6 +3,13 @@
3
3
  #
4
4
  module NamedParameters
5
5
 
6
+ # Makes the next method callable via named parameters (options hash)
7
+ # The caller hash may contain keys that are not contained in the parameters of the next method
8
+ # @param [Hash[Symbol,Object]] optionals Optional default values for the parameters
9
+ def named_parameters(optionals = { })
10
+ _named_parameters optionals, false, false
11
+ end
12
+
6
13
  # Makes the next method callable via named parameters (options hash)
7
14
  # The caller hash may only contain keys that are in the parameters of the next method
8
15
  # @param [Hash[Symbol,Object]] optionals Optional default values for the parameters
@@ -26,6 +33,7 @@ module NamedParameters
26
33
  _named_parameters optionals, true, true
27
34
  end
28
35
 
36
+
29
37
  alias_method :named_parameters_super_strict, :named_parameters_strict_super
30
38
 
31
39
  private
@@ -36,11 +44,31 @@ module NamedParameters
36
44
  end
37
45
 
38
46
  #noinspection RubyArgCount
39
- self.class.class_eval do # :nodoc:
40
- alias :method_added_before_named_parameters :method_added #:nodoc:
47
+ self.class.class_eval do
48
+ alias :method_added_before_named_parameters :method_added
41
49
  private :method_added_before_named_parameters
42
50
  end
43
51
 
52
+ # Enables the <method>_has_named_parameter ghost methods
53
+ def method_missing method, *args, &block
54
+ # catch only methods like <method_name> + _has_named_parameters + <mode definition>
55
+ if /\A(?<method_name>.+)_has_named_parameters(?<mode>_super_strict|_strict_super|_super|_strict)?\z/ =~ method.to_s
56
+ # find out mode
57
+ mode = "" if mode.nil?
58
+ call_super = mode.include? 'super'
59
+ # find out strict
60
+ strict = mode.include? 'strict'
61
+ # redefine
62
+ _redefine_with_named_parameters(method_name,
63
+ args[0] || { },
64
+ strict,
65
+ call_super)
66
+ else
67
+ # if method is not like above pattern, do the original stuff
68
+ super method, *args, &block
69
+ end
70
+ end
71
+
44
72
  # Makes sure a method is redefined as soon as it is defined. Only when named_parameters was called right before
45
73
  def method_added(method_name)
46
74
  # redefine the method
@@ -55,14 +83,6 @@ module NamedParameters
55
83
  method_added_before_named_parameters method_name
56
84
  end
57
85
 
58
- # Makes the next method callable via named parameters (options hash)
59
- # The caller hash may contain keys that are not contained in the parameters of the next method
60
- # @param [Hash[Symbol,Object]] optionals Optional default values for the parameters
61
- # Parameters, that are not
62
- def named_parameters(optionals = { })
63
- _named_parameters optionals, false, false
64
- end
65
-
66
86
  # @param [Hash[Symbol,Object]] optionals Optional default values for the parameters
67
87
  # @param [Boolean] strict Shall keys in the caller options hash, that are not in the parameter list, throw an error?
68
88
  # @param [Boolean] call_super Shall super be called with the caller option hash as the only parameter?
@@ -60,14 +60,64 @@ class Book < Item
60
60
  instance_variable_set("@#{attribute}", yield(send(attribute)))
61
61
  end
62
62
  end
63
+ end
64
+
65
+ class Magazine < Item
66
+ include NamedParameters
67
+
68
+ attr_reader :author, :title
69
+
70
+ def edit_attributes(author, title)
71
+ @author = author
72
+ @title = title
73
+ end
74
+
75
+ edit_attributes_has_named_parameters(author: 'unknown')
63
76
 
77
+
78
+ def edit_only_valid_attributes(author, title)
79
+ @author = author if author.is_a? String
80
+ @title = title if title.is_a? String
81
+ end
82
+
83
+ edit_only_valid_attributes_has_named_parameters_strict(author: 'unknown')
84
+
85
+
86
+ def setup(author, title)
87
+ edit_attributes(author: author,
88
+ title: title)
89
+ end
90
+
91
+ setup_has_named_parameters_super(author: 'unknown')
92
+
93
+ def setup_only_valid_attributes(author, title, id)
94
+ edit_attributes(author: author,
95
+ title: title)
96
+ end
97
+
98
+ setup_only_valid_attributes_has_named_parameters_strict_super(author: 'unknown')
99
+
100
+ def change_attributes(attributes)
101
+ attributes.each do |attribute|
102
+ instance_variable_set("@#{attribute}", yield(send(attribute)))
103
+ end
104
+ end
105
+
106
+ change_attributes_has_named_parameters(attributes: [])
107
+
108
+ def check_for_attributes(author, title)
109
+ @author == author && @title == title
110
+ end
111
+
112
+ check_for_attributes_has_named_parameters
64
113
  end
65
114
 
66
115
  describe NamedParameters do
67
116
 
68
- attr_reader :book
117
+ attr_reader :book, :magazine
69
118
  before :each do
70
- @book = Book.new
119
+ @book = Book.new
120
+ @magazine = Magazine.new
71
121
  end
72
122
 
73
123
  describe 'default' do
@@ -170,7 +220,124 @@ describe NamedParameters do
170
220
  book.author.should == 'ORSON SCOTT CARD'
171
221
  book.title.should == 'ENDERS GAME'
172
222
  end
223
+ end
224
+
225
+ describe 'ghost_methods' do
226
+
227
+ describe 'default' do
228
+ it 'should work with all parameters given' do
229
+ magazine.edit_attributes(author: 'Orson Scott Card',
230
+ title: 'Enders Game')
231
+ magazine.title.should == 'Enders Game'
232
+ magazine.author.should == 'Orson Scott Card'
233
+ end
234
+
235
+ it 'should work with only mandatory parameters given' do
236
+ magazine.edit_attributes(title: 'Enders Game')
237
+ magazine.title.should == 'Enders Game'
238
+ magazine.author.should == 'unknown'
239
+ end
240
+
241
+ it 'should throw an error when a mandatory parameter misses' do
242
+ -> { magazine.edit_attributes(author: 'Orson Scott Card') }.should raise_error(RuntimeError)
243
+ end
244
+
245
+ it 'should ignore options that are not part of the parameters' do
246
+ magazine.edit_attributes(author: 'Orson Scott Card',
247
+ title: 'Enders Game',
248
+ year: 1985)
249
+ magazine.title.should == 'Enders Game'
250
+ magazine.author.should == 'Orson Scott Card'
251
+ end
252
+ end
253
+
254
+ describe 'strict named parameters' do
255
+
256
+ it 'should work with all parameters given' do
257
+ magazine.edit_only_valid_attributes(author: 'Orson Scott Card',
258
+ title: 'Enders Game')
259
+ magazine.title.should == 'Enders Game'
260
+ magazine.author.should == 'Orson Scott Card'
261
+ end
262
+
263
+ it 'should work with only mandatory parameters given' do
264
+ magazine.edit_only_valid_attributes(title: 'Enders Game')
265
+ magazine.title.should == 'Enders Game'
266
+ magazine.author.should == 'unknown'
267
+ end
268
+
269
+ it 'should throw an error when a mandatory parameter misses' do
270
+ -> { magazine.edit_only_valid_attributes(author: 'Orson Scott Card') }.should raise_error(RuntimeError)
271
+ end
272
+
273
+ it 'should throw an error when there are options that are not part of the parameters' do
274
+ -> { magazine.edit_only_valid_attributes(author: 'Orson Scott Card',
275
+ title: 'Enders Game',
276
+ year: 1985) }.should raise_error(RuntimeError)
277
+ end
278
+
279
+ end
173
280
 
281
+
282
+ describe 'with super' do
283
+
284
+ it 'should call super with all options as the only parameter' do
285
+ magazine.setup(author: 'Orson Scott Card',
286
+ title: 'Enders Game',
287
+ year: 1985,
288
+ id: 1)
289
+ magazine.title.should == 'Enders Game'
290
+ magazine.author.should == 'Orson Scott Card'
291
+ magazine.id.should == 1
292
+ end
293
+
294
+ end
295
+
296
+ describe 'with super and strict' do
297
+
298
+ it 'should call super' do
299
+ magazine.setup_only_valid_attributes(author: 'Orson Scott Card',
300
+ title: 'Enders Game',
301
+ id: 1)
302
+ magazine.title.should == 'Enders Game'
303
+ magazine.author.should == 'Orson Scott Card'
304
+ magazine.id.should == 1
305
+ end
306
+
307
+ it 'should throw error when there are unwanted options' do
308
+ -> { magazine.setup_only_valid_attributes(author: 'Orson Scott Card',
309
+ title: 'Enders Game',
310
+ year: 1985,
311
+ id: 1) }.should raise_error(RuntimeError)
312
+ end
313
+
314
+ end
315
+
316
+ describe 'with block' do
317
+ it 'should forward blocks' do
318
+ magazine.edit_attributes(author: 'Orson Scott Card',
319
+ title: 'Enders Game')
320
+ magazine.change_attributes(attributes: [:author, :title]) do |attribute|
321
+ attribute.to_s.upcase
322
+ end
323
+ magazine.author.should == 'ORSON SCOTT CARD'
324
+ magazine.title.should == 'ENDERS GAME'
325
+ end
326
+ end
327
+
328
+ describe 'missing methods' do
329
+ it 'should not catch any other missing methods' do
330
+ -> { magazine.some_weird_method }.should raise_error(NoMethodError)
331
+ end
332
+ end
333
+
334
+ it 'should handle no optionals' do
335
+ -> { magazine.check_for_attributes(author: 'Orson Scott Card') }.should raise_error(RuntimeError)
336
+ magazine.setup(author: 'Orson Scott Card',
337
+ title: 'Enders Game')
338
+ magazine.check_for_attributes(author: 'Orson Scott Card',
339
+ title: 'Enders Game').should be_true
340
+ end
174
341
  end
175
342
 
176
343
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: named_parameters
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
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: 2012-08-05 00:00:00.000000000 Z
12
+ date: 2012-08-06 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec