Module: NamedArguments::ClassSettingsMixin::ClassMethods
- Includes:
- MethodExtensions
- Defined in:
- lib/named_arguments/class_settings_mixin.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#create_class_settings_method(name) ⇒ Object
Allow you to define methods that set a class value hash and an accessor for that hash.
Class Method Details
.included(target) ⇒ Object
13 14 15 16 17 |
# File 'lib/named_arguments/class_settings_mixin.rb', line 13 def self.included target target.class_eval do include NamedArguments::MethodExtensions end end |
Instance Method Details
#create_class_settings_method(name) ⇒ Object
Allow you to define methods that set a class value hash and an accessor for that hash.
Example:
create_class_settings_method :settings
creates:
- settings
-
A class method that allows you to set variables
- settings?
-
The current value of those variables
class BlueCar
create_class_settings_method :settings
create_class_settings_method :has_these
settings :color => :blue, :another_settting => 10
settings :painted => true
has_these :doors, :windows
has_these :wheels
end
class Convertable < BlueCar
has_these :poptop
end
BlueCar.color
=> :blue
BlueCar.new.settings?
=> {:color => :blue, :painted => true, :another_settting => 10}
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/named_arguments/class_settings_mixin.rb', line 52 def create_class_settings_method name # Build the class methods first l = class_setting_lambda name define_method_with_context name, &l # Allow #name? to be called as an instance method # and default its return value to nil. # This will be replaced on any call to the # setter. value_name = value_field_identifier(name) instance_method = lambda {nil} define_method value_name, instance_method end |