Module: ClassSettingsMixinClassMethods
- Includes:
- SingletonMethodCreatorMixin
- Defined in:
- lib/class_settings_mixin.rb
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.
Methods included from SingletonMethodCreatorMixin
#create_singleton_method, #create_singleton_value_method
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}
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/class_settings_mixin.rb', line 45 def create_class_settings_method name # Build the class methods first l = class_setting_lambda name create_singleton_method 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 |