Class: Trax::Core::Configuration

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/trax/core/configuration.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Configuration

Returns a new instance of Configuration.

Yields:

  • (_self)

Yield Parameters:



65
66
67
68
69
70
71
72
73
# File 'lib/trax/core/configuration.rb', line 65

def initialize
  set_default_values

  yield self if block_given?

  raise_errors unless self.valid?

  self.class.instance_variable_get("@_after_configured_block").call(self) if self.class.instance_variable_defined?("@_after_configured_block")
end

Class Method Details

.after_configured(&block) ⇒ Object



12
13
14
# File 'lib/trax/core/configuration.rb', line 12

def self.after_configured(&block)
  self.instance_variable_set("@_after_configured_block", block)
end

.inherited(subklass) ⇒ Object



16
17
18
19
20
21
22
23
24
25
# File 'lib/trax/core/configuration.rb', line 16

def self.inherited(subklass)
  super(subklass)

  subklass.class_attribute :options_module
  subklass.options_module = subklass.const_set("Options", ::Module.new)
  subklass.include(subklass.options_module)

  subklass.class_attribute :configurable_options
  subklass.configurable_options = {}
end

.klass(&block) ⇒ Object



58
59
60
61
62
63
# File 'lib/trax/core/configuration.rb', line 58

def self.klass(&block)
  #so we can properly resolve super lookups, we define new module and include
  mod = ::Module.new
  mod.module_eval(&block)
  include(mod)
end

.option(attr_name, options = {}) ⇒ Object

the reason for defining the methods in the options module which gets dynamically created is to preserve the inheritance chain so def something= can be overwritten, and call super, an example is in Trax::Model::UniqueId there probably is a cleaner way to do it though.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trax/core/configuration.rb', line 32

def self.option(attr_name, options = {})
  validates_presence_of(attr_name) if(options.key?(:required))
  if(options.key?(:validates))
    validates(attr_name, options[:validates])
  end

  options_module.module_eval do
    attr_reader(attr_name)

    define_method(:"#{attr_name}=") do |val|
      value = options.key?(:setter) ? options[:setter].call(val) : val
      instance_variable_set("@#{attr_name}", value)

      raise_errors unless self.valid?
    end

    if(options.key?(:getter))
      define_method(attr_name) do |val|
        options[:getter].call(val)
      end
    end
  end

  self.configurable_options[attr_name] = options
end

Instance Method Details

#cached_hashObject



79
80
81
# File 'lib/trax/core/configuration.rb', line 79

def cached_hash
  @cached_hash ||= hash
end

#compactObject



75
76
77
# File 'lib/trax/core/configuration.rb', line 75

def compact
  cached_hash.compact
end

#hashObject



83
84
85
86
87
# File 'lib/trax/core/configuration.rb', line 83

def hash
  configurable_options.keys.each_with_object({}) do |key, result|
    result[key] =  __send__(key)
  end
end

#merge!(options = {}) ⇒ Object



98
99
100
# File 'lib/trax/core/configuration.rb', line 98

def merge!(options = {})
  options.each_pair{ |k,v| __send__("#{k}=", v) }
end

#raise_errorsObject



102
103
104
105
106
107
# File 'lib/trax/core/configuration.rb', line 102

def raise_errors
  raise Trax::Core::Errors::ConfigurationError.new(
    :messages => self.errors.full_messages,
    :source => self.class.name
  )
end

#set_default_valuesObject



89
90
91
92
93
94
95
96
# File 'lib/trax/core/configuration.rb', line 89

def set_default_values
  self.class.configurable_options.select{|attr_name, hash| hash.key?(:default) }.each_pair do |attr_name, hash|
    parent_context = self.class.parent_name.try(:constantize) if self.class.try(:parent_name)

    default_value_for_option = hash[:default].is_a?(Proc) ? self.instance_exec(parent_context, &hash[:default]) : hash[:default]
    __send__("#{attr_name}=", default_value_for_option)
  end
end