Class: E9Rails::ActiveRecord::InheritableOptions::IndifferentOptions
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- E9Rails::ActiveRecord::InheritableOptions::IndifferentOptions
- Extended by:
- ActiveModel::Naming, ActiveModel::Translation
- Defined in:
- lib/e9_rails/active_record/inheritable_options.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Class Method Summary collapse
Instance Method Summary collapse
- #extractable_options? ⇒ Boolean
-
#initialize(hash, base) ⇒ IndifferentOptions
constructor
A new instance of IndifferentOptions.
-
#method_missing(mid, *args) ⇒ Object
If OpenStruct first generates the new_ostruct_member methods with a setter, it does not use the newly generated methods to set the attribute the first time, but rather does this:.
- #new_ostruct_member(name) ⇒ Object
-
#respond_to?(method_name) ⇒ Boolean
we respond to anything!.
- #to_hash ⇒ Object
Constructor Details
#initialize(hash, base) ⇒ IndifferentOptions
Returns a new instance of IndifferentOptions.
110 111 112 113 114 115 116 117 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 110 def initialize(hash, base) @table = {} for k,v in hash new_ostruct_member(k) @table[k.to_sym] = v end @base = base end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(mid, *args) ⇒ Object
If OpenStruct first generates the new_ostruct_member methods with a setter, it does not use the newly generated methods to set the attribute the first time, but rather does this:
modifiable[new_ostruct_member(mname)] = args[0]
Because of this it’s necessary to explicitly call the newly created setter after new_ostruct_member does its job to ensure that
153 154 155 156 157 158 159 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 153 def method_missing(mid, *args) super if mid.id2name =~ /=$/ send(mid, *args) end end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
99 100 101 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 99 def base @base end |
Class Method Details
.i18n_scope ⇒ Object
103 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 103 def i18n_scope; :activerecord end |
.name ⇒ Object
102 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 102 def name; 'Options' end |
Instance Method Details
#extractable_options? ⇒ Boolean
106 107 108 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 106 def false end |
#new_ostruct_member(name) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 128 def new_ostruct_member(name) name = name.to_sym unless @table.keys.member?(name) class << self; self; end.class_eval do define_method(name) { @table[name] } define_method("#{name}=") do |x| modifiable[name] = x @base. = to_hash modifiable[name] end end end name end |
#respond_to?(method_name) ⇒ Boolean
we respond to anything!
124 125 126 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 124 def respond_to?(method_name) true end |
#to_hash ⇒ Object
119 120 121 |
# File 'lib/e9_rails/active_record/inheritable_options.rb', line 119 def to_hash @table end |