Module: Halcyon::Config::Helpers::Configurable
- Defined in:
- lib/halcyon/config/helpers.rb
Overview
Provides dynamic creation of configuration attribute accessors.
Instance Method Summary collapse
-
#configurable(attribute) ⇒ Object
(also: #configurable_attr)
Defines a dynamic accessor for configuration attributes.
-
#configurable_reader(attribute, code = nil, &block) ⇒ Object
(also: #configurable_attr_reader)
Defines a dynamic reader for configuration attributes, accepting either a string or a block to perform the action.
-
#configurable_writer(attribute, code = nil, &block) ⇒ Object
(also: #configurable_attr_writer)
Defines a dynamic writer for configuration attributes, accepting either a string or a block to perform the action.
Instance Method Details
#configurable(attribute) ⇒ Object Also known as: configurable_attr
Defines a dynamic accessor for configuration attributes.
Examples:
Halcyon.configurable(:db)
Halcyon.db = {...}
Halcyon.config[:db] #=> {...}
Halcyon.config[:db] = true
Halcyon.db #=> true
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/halcyon/config/helpers.rb', line 107 def configurable(attribute) eval <<-"end;" def #{attribute.to_s} Halcyon.config[:#{attribute.to_s}] end def #{attribute.to_s}=(value) value = value.to_mash if value.is_a?(Hash) Halcyon.config[:#{attribute.to_s}] = value end end; end |
#configurable_reader(attribute, code = nil, &block) ⇒ Object Also known as: configurable_attr_reader
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/halcyon/config/helpers.rb', line 133 def configurable_reader(attribute, code=nil, &block) if block_given? and not code Halcyon.class.send(:define_method, attribute.to_sym, block) elsif code and not block_given? Halcyon.class.send(:eval, <<-"end;") def #{attribute.to_s} #{code % [attribute.to_sym.inspect]} end end; else raise ArgumentError.new("Either a block or a code string should be supplied.") end end |
#configurable_writer(attribute, code = nil, &block) ⇒ Object Also known as: configurable_attr_writer
161 162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'lib/halcyon/config/helpers.rb', line 161 def configurable_writer(attribute, code=nil, &block) if block_given? and not code Halcyon.class.send(:define_method, :"#{attribute}=", block) elsif code and not block_given? Halcyon.class.send(:eval, <<-"end;") def #{attribute.to_s}=(value) #{code % [attribute.to_sym.inspect]} end end; else raise ArgumentError.new("Either a block or a code string should be supplied.") end end |