Module: NamedArguments::ClassMethods

Defined in:
lib/named_arguments.rb

Overview

Class methods (methods of the class object itself) provided when NamedArguments in included.

Instance Method Summary collapse

Instance Method Details

#option_attr(*array_of_names) ⇒ Object



216
217
218
219
220
221
222
223
224
225
# File 'lib/named_arguments.rb', line 216

def option_attr *array_of_names
  array_of_names.each { |n| 
    define_method n, lambda {
      option_attr_get n
    }
    define_method "#{n}=", lambda { |v|
      option_attr_set n, v
    }
  }
end

#type_converter(field, new_klass) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/named_arguments.rb', line 227

def type_converter field, new_klass
  alias_name = ('attribute_setter_for_' + field.to_s).to_sym
  setter_method_name = (field.to_s + '=').to_sym
  alias_method alias_name, setter_method_name
  send :define_method, setter_method_name do |rhs|
    if Integer === new_klass
      v = rhs.to_i
    elsif new_klass == String
      v = rhs.to_s
    elsif new_klass == Symbol
      v = rhs.to_sym
    elsif new_klass == :boolean
      v = !!rhs
    elsif Proc === new_klass
      v = new_klass.call v
    else
      v = new_klass.new rhs
    end
    self.send alias_name, v
  end
end