Class: Trax::Core::Configuration
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/trax/core/configuration.rb
Class Method Summary collapse
- .after_configured(&block) ⇒ Object
- .inherited(subklass) ⇒ Object
- .klass(&block) ⇒ Object
-
.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.
Instance Method Summary collapse
- #cached_hash ⇒ Object
- #compact ⇒ Object
- #hash ⇒ Object
-
#initialize {|_self| ... } ⇒ Configuration
constructor
A new instance of Configuration.
- #merge!(options = {}) ⇒ Object
- #raise_errors ⇒ Object
- #set_default_values ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Configuration
Returns a new instance of Configuration.
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. = subklass.const_set("Options", ::Module.new) subklass.include(subklass.) subklass.class_attribute :configurable_options subklass. = {} 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, = {}) validates_presence_of(attr_name) if(.key?(:required)) if(.key?(:validates)) validates(attr_name, [:validates]) end .module_eval do attr_reader(attr_name) define_method(:"#{attr_name}=") do |val| value = .key?(:setter) ? [:setter].call(val) : val instance_variable_set("@#{attr_name}", value) raise_errors unless self.valid? end if(.key?(:getter)) define_method(attr_name) do |val| [:getter].call(val) end end end self.[attr_name] = end |
Instance Method Details
#cached_hash ⇒ Object
79 80 81 |
# File 'lib/trax/core/configuration.rb', line 79 def cached_hash @cached_hash ||= hash end |
#compact ⇒ Object
75 76 77 |
# File 'lib/trax/core/configuration.rb', line 75 def compact cached_hash.compact end |
#hash ⇒ Object
83 84 85 86 87 |
# File 'lib/trax/core/configuration.rb', line 83 def hash .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!( = {}) .each_pair{ |k,v| __send__("#{k}=", v) } end |
#raise_errors ⇒ Object
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., :source => self.class.name ) end |
#set_default_values ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/trax/core/configuration.rb', line 89 def set_default_values self.class..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 |