Module: Clerq::Properties

Included in:
Settings
Defined in:
lib/clerq/properties.rb

Instance Method Summary collapse

Instance Method Details

#property(name, options = {}, &validation) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/clerq/properties.rb', line 4

def property(name, options = {}, &validation)
  default_value = options[:default]
  define_method name do
    instance_variable_get("@#{name}") || default_value
  end

  define_method "#{name}=" do |val|
    if validation && !validation.call(val)
      raise ArgumentError, "Invalid property value #{name}: #{val}"
    end
    instance_variable_set("@#{name}", val)
  end
end