Class: ActiveConfiguration::Base
- Inherits:
-
Object
- Object
- ActiveConfiguration::Base
- Defined in:
- lib/active_configuration/base.rb
Overview
Holds a set of configuration details. An instance of this object is created when the #configure block is used on an ActiveRecord model. For more details see ActiveRecord::Configuration#configure.
Instance Attribute Summary collapse
-
#options_hash ⇒ Object
Returns the value of attribute options_hash.
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
Initializes an empty options hash to store ActiveConfiguration::Option instances containing configuration details.
-
#option(key, &block) ⇒ Object
Creates and accepts the configuration details for an Option.
- #options(namespace = nil) ⇒ Object
Constructor Details
#initialize ⇒ Base
Initializes an empty options hash to store ActiveConfiguration::Option instances containing configuration details.
13 14 15 |
# File 'lib/active_configuration/base.rb', line 13 def initialize @options_hash = HashWithIndifferentAccess.new end |
Instance Attribute Details
#options_hash ⇒ Object
Returns the value of attribute options_hash.
9 10 11 |
# File 'lib/active_configuration/base.rb', line 9 def @options_hash end |
Instance Method Details
#option(key, &block) ⇒ Object
Creates and accepts the configuration details for an Option.
An example of setting an option with a block:
option :sort do
default 'alphabetical'
restrict 'alphabetical', 'manual'
end
Here, the #option method is called and passed the key of :sort and then the the block that follows. The block given to #option is then evaluated against a new instance of ActiveConfiguration::Option.
32 33 34 35 36 |
# File 'lib/active_configuration/base.rb', line 32 def option(key, &block) opt = Option.new(key) opt.instance_eval(&block) @options_hash[key.to_sym] = opt end |
#options(namespace = nil) ⇒ Object
38 39 40 41 |
# File 'lib/active_configuration/base.rb', line 38 def ( namespace = nil ) return @options_hash if namespace == :all @options_hash.select{|key, option| option.option_namespace == namespace }.with_indifferent_access end |