Module: NamedArguments
- Defined in:
- lib/named_arguments.rb,
lib/named_arguments/method_extensions.rb,
lib/named_arguments/hash_extended_tools.rb,
lib/named_arguments/class_settings_mixin.rb
Overview
Adds the following features to a class:
-
Pass a hash to new and matching attributes will be set.
-
Set default values for arguments (attribute_defaults)
-
Require arguments (required_fields)
-
Require kind_of? tests for arguments (required_kind_of)
-
Change the type of the object passed in (type_converter)
Sample
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_converter :size => Fixnum
end
See also
See ClassMethods for more methods:
-
ClassMethods#type_converter
-
ClassMethods#option_attr
Defined Under Namespace
Modules: ClassMethods, ClassSettingsMixin, HashExtendedTools, MethodExtensions Classes: Error, MustRespondTo, ParameterRequired, WrongClass
Constant Summary collapse
- VERSION =
'0.0.5'
Class Method Summary collapse
-
.included(target) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#attribute_defaults ⇒ Object
Set defaults for the given attributes.
-
#attributes_set(args) ⇒ Object
Set the attributes for this object.
-
#check_required_field(fields_set) ⇒ Object
Checks to make sure all the required fields are passed in.
-
#check_required_kind ⇒ Object
Checks to make sure all the fields specifed in required_kind_of have the right kind of objects.
-
#check_required_respond_to ⇒ Object
:nodoc:.
-
#initialize(args = {}) ⇒ Object
For every key/value pair in
args
, set the value of the attributekey
tovalue
. -
#option_attr_get(k) ⇒ Object
:nodoc:.
-
#option_attr_set(k, v) ⇒ Object
:nodoc:.
-
#option_attr_storage ⇒ Object
:nodoc:.
-
#option_attrs_keys ⇒ Object
:nodoc:.
-
#option_attrs_keys_set ⇒ Object
:nodoc:.
-
#required_fields ⇒ Object
Requires that the given arguments be present on the call to new.
-
#required_kind_of ⇒ Object
Requires that object.snark.kind_of? String be true on the call to initialize.
-
#required_respond_to ⇒ Object
Requires that the given objects respond to the method call.
-
#set_default_attributes(args) ⇒ Object
:nodoc:.
Class Method Details
.included(target) ⇒ Object
:nodoc:
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/named_arguments.rb', line 37 def self.included target # :nodoc: target.class_eval do include HashExtendedTools include ClassSettingsMixin extend ClassMethods create_class_settings_method :required_fields create_class_settings_method :required_kind_of create_class_settings_method :required_respond_to create_class_settings_method :attribute_defaults end end |
Instance Method Details
#attribute_defaults ⇒ Object
Set defaults for the given attributes.
Values can be:
-
A class. The #new method is called with no arguments.
-
A Proc. The proc is called with one argument.
-
[]. A new array is created.
-
{}. A new hash is created.
attribute_defaults :snark => lambda {|s| String.new s.to_s}
attribute_defaults :snark => [], :boojum => ObjectTypeFromSomewhereElse
attribute_defaults :snark => {}
93 94 95 |
# File 'lib/named_arguments.rb', line 93 def attribute_defaults # Dummy for rdoc end |
#attributes_set(args) ⇒ Object
Set the attributes for this object. Normally called by initialize.
100 101 102 103 104 105 106 107 |
# File 'lib/named_arguments.rb', line 100 def attributes_set(args) # :nodoc: set_default_attributes(args) args_plus_defaults = (attribute_defaults? || {}).merge(args) check_required_field args_plus_defaults check_required_kind check_required_respond_to end |
#check_required_field(fields_set) ⇒ Object
Checks to make sure all the required fields are passed in.
See also: required_fields
147 148 149 150 151 |
# File 'lib/named_arguments.rb', line 147 def check_required_field(fields_set) # :nodoc: (required_fields? || []).each do |f| raise ParameterRequired, "Must set parameter: " + f.to_s unless fields_set.has_key? f.to_sym end end |
#check_required_kind ⇒ Object
Checks to make sure all the fields specifed in required_kind_of have the right kind of objects.
See also:
-
#required_kind_of
-
#set_default_attributes
160 161 162 163 164 |
# File 'lib/named_arguments.rb', line 160 def check_required_kind # :nodoc: (required_kind_of? || {}).each_pair do |k, v| raise WrongClass.new("Wrong class: #{k.to_s}; should have been #{v.to_s}, object is #{self.send(k).inspect}") unless v === self.send(k) end end |
#check_required_respond_to ⇒ Object
:nodoc:
166 167 168 169 170 |
# File 'lib/named_arguments.rb', line 166 def check_required_respond_to # :nodoc: (required_respond_to? || {}).each_pair do |k, v| raise MustRespondTo.new("#{k} must respond to #{v}; the object is #{self.send(k).inspect}") unless self.send(k).respond_to?(v) end end |
#initialize(args = {}) ⇒ Object
For every key/value pair in args
, set the value of the attribute key
to value
.
class Snark
include NamedArguments
attr_accessor :boojum
end
s = Snark.new :boojum => 7
181 182 183 184 185 186 187 188 |
# File 'lib/named_arguments.rb', line 181 def initialize args = {} if kind_of? ActiveRecord::Base super else super() end attributes_set args end |
#option_attr_get(k) ⇒ Object
:nodoc:
190 191 192 |
# File 'lib/named_arguments.rb', line 190 def option_attr_get k # :nodoc: option_attr_storage[k] end |
#option_attr_set(k, v) ⇒ Object
:nodoc:
194 195 196 |
# File 'lib/named_arguments.rb', line 194 def option_attr_set k, v # :nodoc: option_attr_storage[k] = v end |
#option_attr_storage ⇒ Object
:nodoc:
198 199 200 |
# File 'lib/named_arguments.rb', line 198 def option_attr_storage # :nodoc: self. ||= {} end |
#option_attrs_keys ⇒ Object
:nodoc:
202 203 204 |
# File 'lib/named_arguments.rb', line 202 def option_attrs_keys # :nodoc: option_attr_storage.keys end |
#option_attrs_keys_set ⇒ Object
:nodoc:
206 207 208 209 210 |
# File 'lib/named_arguments.rb', line 206 def option_attrs_keys_set # :nodoc: option_attrs_keys.select do |k| option_attr_storage.include? k end end |
#required_fields ⇒ Object
Requires that the given arguments be present on the call to new.
required_fields [:snark, :boojum]
or
required_fields :snark
67 68 69 |
# File 'lib/named_arguments.rb', line 67 def required_fields # Dummy for rdoc end |
#required_kind_of ⇒ Object
Requires that object.snark.kind_of? String be true on the call to initialize.
Throws a NamedArgumentException if that test fails.
required_kind_of :snark => String, :boojum => Fixnum
55 56 57 |
# File 'lib/named_arguments.rb', line 55 def required_kind_of # Dummy for rdoc end |
#required_respond_to ⇒ Object
Requires that the given objects respond to the method call.
required_respond_to :snark => 'hunt'
Raises a NamedArgumentException on failure.
77 78 79 |
# File 'lib/named_arguments.rb', line 77 def required_respond_to # Dummy for rdoc end |
#set_default_attributes(args) ⇒ Object
:nodoc:
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/named_arguments.rb', line 109 def set_default_attributes(args) # :nodoc: defaults = {} (attribute_defaults? || {}).each_pair do |k, v| if Class === v result = v.new elsif Proc === v result = v.call self elsif v.class == Array and v.empty? result = Array.new elsif v.class == Hash and v.empty? result = Hash.new else result = v end defaults[k] = result end args = defaults.merge args args.each_pair do |k, v| m = "#{k}=" send m, v if respond_to? m end end |