Module: MagicOptions::ClassMethods
- Defined in:
- lib/magic_options.rb
Overview
When an object initializer need do nothing more than implement the magic options pattern, it can be defined in terms of its magic options as follows:
<tt>
class Cow
include MagicOptions
magic_initialize(:require => :name, :only => [:color, :gender])
end
Cow.new(:name => 'Daisy', :color => 'brown', :gender => 'female')
# => #<Cow:0x000000015d02b8 @color="brown", @gender="female", @name="Daisy">
</tt>
When the object initializer must do more than this (for example, if it must call super
), you can define it yourself in terms of MagicOptions#magic_options.
Instance Attribute Summary collapse
-
#magic_options_config ⇒ Object
Class accessor used by #magic_initialize to store the
config
hash it receives, to be passed into MagicOptions#magic_options in the object initializer that #magic_initialize defines.
Instance Method Summary collapse
-
#magic_initialize(config = {}) ⇒ Object
Defines an object initializer that takes an options hash and passes it to MagicOptions#magic_options, along with the optional
config
hash.
Instance Attribute Details
#magic_options_config ⇒ Object
Class accessor used by #magic_initialize to store the config
hash it receives, to be passed into MagicOptions#magic_options in the object initializer that #magic_initialize defines.
88 89 90 |
# File 'lib/magic_options.rb', line 88 def @magic_options_config end |
Instance Method Details
#magic_initialize(config = {}) ⇒ Object
Defines an object initializer that takes an options hash and passes it to MagicOptions#magic_options, along with the optional config
hash.
92 93 94 95 96 97 98 99 |
# File 'lib/magic_options.rb', line 92 def magic_initialize(config = {}) self. = config class_eval %{ def initialize(options = {}) magic_options(options, self.class.magic_options_config) end } end |