Module: Halcyon::Config::Helpers::Configurable

Defined in:
lib/halcyon/config/helpers.rb

Overview

Provides dynamic creation of configuration attribute accessors.

Instance Method Summary collapse

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 "    def \#{attribute.to_s}\n      Halcyon.config[:\#{attribute.to_s}]\n    end\n    def \#{attribute.to_s}=(value)\n      value = value.to_mash if value.is_a?(Hash)\n      Halcyon.config[:\#{attribute.to_s}] = value\n    end\n  end;\nend\n"

#configurable_reader(attribute, code = nil, &block) ⇒ Object Also known as: configurable_attr_reader

Defines a dynamic reader for configuration attributes, accepting either a string or a block to perform the action.

Examples:

Halcyon.configurable_reader(:foo) do
  self.config[:foo].to_sym
end

OR

Halcyon.configurable_reader(:foo, "Halcyon.config[%s].to_sym")


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, "      def \#{attribute.to_s}\n        \#{code % [attribute.to_sym.inspect]}\n      end\n    end;\n  else\n    raise ArgumentError.new(\"Either a block or a code string should be supplied.\")\n  end\nend\n")

#configurable_writer(attribute, code = nil, &block) ⇒ Object Also known as: configurable_attr_writer

Defines a dynamic writer for configuration attributes, accepting either a string or a block to perform the action.

Examples:

Halcyon.configurable_writer(:foo) do |val|
  self.config[:foo] = val.to_sym
end

OR

Halcyon.configurable_reader(:foo, "Halcyon.config[%s] = value.to_sym")


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, "      def \#{attribute.to_s}=(value)\n        \#{code % [attribute.to_sym.inspect]}\n      end\n    end;\n  else\n    raise ArgumentError.new(\"Either a block or a code string should be supplied.\")\n  end\nend\n")