namedarguments 0.0.2 → 0.0.3
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.txt +16 -5
- data/Rakefile +1 -1
- data/lib/hash_extended_tools.rb +24 -3
- data/lib/named_arguments.rb +49 -37
- metadata +3 -3
data/README.txt
CHANGED
@@ -3,17 +3,28 @@ NamedArguments
|
|
3
3
|
|
4
4
|
== DESCRIPTION:
|
5
5
|
|
6
|
-
|
6
|
+
Adds the ability to call class constructors with a hash of arguments to
|
7
|
+
initialize attributes in the new object.
|
7
8
|
|
8
9
|
== FEATURES/PROBLEMS:
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
11
|
+
class Snark
|
12
|
+
include NamedArguments
|
13
|
+
|
14
|
+
attr_accessor :color, :size, :name
|
15
|
+
|
16
|
+
attribute_defaults :color => 'blue', :size => lambda {|s| some_method_call(s)}
|
17
|
+
required_fields :color, :name
|
18
|
+
required_respond_to :name => :to_s
|
19
|
+
required_kind_of? :size => Fixnum
|
20
|
+
type_conversion :size => Fixnum
|
21
|
+
end
|
22
|
+
|
23
|
+
s = Snark.new :boojum => 7, :color => red
|
13
24
|
|
14
25
|
== INSTALL:
|
15
26
|
|
16
|
-
|
27
|
+
gem install namedarguments
|
17
28
|
|
18
29
|
== LICENSE:
|
19
30
|
|
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ Hoe.new('namedarguments', NamedArguments::VERSION) do |p|
|
|
6
6
|
p.summary = 'Provide named arguments (hashes) to constructors to initialize attributes.'
|
7
7
|
p.author = "James M Moore"
|
8
8
|
p.email = 'james@phonesonrails.com'
|
9
|
-
|
9
|
+
p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
|
10
10
|
# p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
|
11
11
|
p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
|
12
12
|
end
|
data/lib/hash_extended_tools.rb
CHANGED
@@ -1,17 +1,27 @@
|
|
1
|
-
#
|
1
|
+
# Provides several hash utilities.
|
2
2
|
#
|
3
3
|
# Note that you need to extend your hash with this module:
|
4
4
|
#
|
5
5
|
# hash = {}
|
6
6
|
# hash.extend HashExtendedTools
|
7
7
|
# hash = hash.exclude :foo, :bar
|
8
|
+
#
|
9
|
+
# Or create a new class:
|
10
|
+
#
|
11
|
+
# class HashWithExtendedTools < Hash
|
12
|
+
# include HashExtendedTools
|
13
|
+
# end
|
8
14
|
module HashExtendedTools
|
9
15
|
# Change keys in a hash.
|
10
16
|
#
|
11
17
|
# Pass in a hash of:
|
12
18
|
#
|
13
|
-
#
|
14
|
-
|
19
|
+
# old_key => new_key
|
20
|
+
#
|
21
|
+
# Any keys matching +old_key+ will be
|
22
|
+
# deleted and a new entry created with
|
23
|
+
# the same value and the new key.
|
24
|
+
def switch_keys args = {}
|
15
25
|
args.each_pair do
|
16
26
|
|old_key, new_key|
|
17
27
|
if self.has_key?(old_key)
|
@@ -21,6 +31,14 @@ module HashExtendedTools
|
|
21
31
|
end
|
22
32
|
end
|
23
33
|
|
34
|
+
# Return a new hash not including
|
35
|
+
# keys that are contained in
|
36
|
+
# +keys_to_exclude+.
|
37
|
+
#
|
38
|
+
# Keys that match entries in
|
39
|
+
# +keys_to_exclude+ are deleted if
|
40
|
+
# either they match as string or a
|
41
|
+
# symbol (created with to_sym).
|
24
42
|
def exclude *keys_to_exclude
|
25
43
|
result = self.dup
|
26
44
|
keys_to_exclude.each do |k|
|
@@ -34,6 +52,9 @@ module HashExtendedTools
|
|
34
52
|
# return a hash containing
|
35
53
|
# the key/value pairs
|
36
54
|
# for the matching keys.
|
55
|
+
#
|
56
|
+
# Values that are nil are not
|
57
|
+
# returned.
|
37
58
|
def slice *slice_keys
|
38
59
|
result = {}
|
39
60
|
slice_keys.each do |k|
|
data/lib/named_arguments.rb
CHANGED
@@ -9,7 +9,7 @@ require File.dirname(__FILE__) + '/singleton_creator_mixin'
|
|
9
9
|
# * Set default values for arguments (attribute_defaults)
|
10
10
|
# * Require arguments (required_fields)
|
11
11
|
# * Require kind_of? tests for arguments (required_kind_of)
|
12
|
-
# * Change the type of the object passed in (
|
12
|
+
# * Change the type of the object passed in (type_converter)
|
13
13
|
#
|
14
14
|
# =Sample
|
15
15
|
#
|
@@ -22,14 +22,17 @@ require File.dirname(__FILE__) + '/singleton_creator_mixin'
|
|
22
22
|
# required_fields :color, :name
|
23
23
|
# required_respond_to :name => :to_s
|
24
24
|
# required_kind_of? :size => Fixnum
|
25
|
-
#
|
25
|
+
# type_converter :size => Fixnum
|
26
26
|
# end
|
27
27
|
#
|
28
28
|
# = See also
|
29
29
|
#
|
30
|
-
# See NamedArgumentsClassMethods for more methods
|
30
|
+
# See NamedArgumentsClassMethods for more methods:
|
31
|
+
#
|
32
|
+
# * NamedArgumentsClassMethods#type_converter
|
33
|
+
# * NamedArgumentsClassMethods#option_attr
|
31
34
|
module NamedArguments
|
32
|
-
VERSION = '0.0.
|
35
|
+
VERSION = '0.0.3'
|
33
36
|
|
34
37
|
include HashExtendedTools
|
35
38
|
|
@@ -167,9 +170,15 @@ module NamedArguments
|
|
167
170
|
end
|
168
171
|
end
|
169
172
|
|
170
|
-
# For every key/value pair in +args+,
|
173
|
+
# For every key/value pair in +args+, set the
|
174
|
+
# value of the attribute +key+ to +value+.
|
171
175
|
#
|
172
|
-
#
|
176
|
+
# class Snark
|
177
|
+
# include NamedArguments
|
178
|
+
# attr_accessor :boojum
|
179
|
+
# end
|
180
|
+
#
|
181
|
+
# s = Snark.new :boojum => 7
|
173
182
|
def initialize args = {}
|
174
183
|
if kind_of? ActiveRecord::Base
|
175
184
|
super
|
@@ -201,39 +210,42 @@ module NamedArguments
|
|
201
210
|
option_attr_storage.include? k
|
202
211
|
end
|
203
212
|
end
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
213
|
+
|
214
|
+
# Class methods (methods of the class
|
215
|
+
# object itself) provided when
|
216
|
+
# NamedArguments in included.
|
217
|
+
module NamedArgumentsClassMethods
|
218
|
+
def option_attr *array_of_names
|
219
|
+
array_of_names.each { |n|
|
220
|
+
define_method n, lambda {
|
221
|
+
option_attr_get n
|
222
|
+
}
|
223
|
+
define_method "#{n}=", lambda { |v|
|
224
|
+
option_attr_set n, v
|
225
|
+
}
|
214
226
|
}
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
227
|
+
end
|
228
|
+
|
229
|
+
def type_converter field, new_klass
|
230
|
+
alias_name = ('attribute_setter_for_' + field.to_s).to_sym
|
231
|
+
setter_method_name = (field.to_s + '=').to_sym
|
232
|
+
alias_method alias_name, setter_method_name
|
233
|
+
send :define_method, setter_method_name do |rhs|
|
234
|
+
if [Fixnum].member? new_klass
|
235
|
+
v = rhs.to_i
|
236
|
+
elsif new_klass == String
|
237
|
+
v = rhs.to_s
|
238
|
+
elsif new_klass == Symbol
|
239
|
+
v = rhs.to_sym
|
240
|
+
elsif new_klass == :boolean
|
241
|
+
v = !!rhs
|
242
|
+
elsif new_klass.kind_of? Proc
|
243
|
+
v = new_klass.call v
|
244
|
+
else
|
245
|
+
v = new_klass.new rhs
|
246
|
+
end
|
247
|
+
self.send alias_name, v
|
235
248
|
end
|
236
|
-
self.send alias_name, v
|
237
249
|
end
|
238
250
|
end
|
239
251
|
end
|
metadata
CHANGED
@@ -3,15 +3,15 @@ rubygems_version: 0.8.11
|
|
3
3
|
specification_version: 1
|
4
4
|
name: namedarguments
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0.
|
7
|
-
date: 2006-12-
|
6
|
+
version: 0.0.3
|
7
|
+
date: 2006-12-06 00:00:00 -08:00
|
8
8
|
summary: Provide named arguments (hashes) to constructors to initialize attributes.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
11
11
|
email: james@phonesonrails.com
|
12
12
|
homepage: http://www.zenspider.com/ZSS/Products/namedarguments/
|
13
13
|
rubyforge_project: namedarguments
|
14
|
-
description:
|
14
|
+
description: "Adds the ability to call class constructors with a hash of arguments to initialize attributes in the new object. == FEATURES/PROBLEMS: class Snark include NamedArguments attr_accessor :color, :size, :name attribute_defaults :color => 'blue', :size => lambda {|s| some_method_call(s)} required_fields :color, :name required_respond_to :name => :to_s required_kind_of? :size => Fixnum type_conversion :size => Fixnum end s = Snark.new :boojum => 7, :color => red == INSTALL:"
|
15
15
|
autorequire:
|
16
16
|
default_executable:
|
17
17
|
bindir: bin
|