Module: ModFactor::Owner
- Defined in:
- lib/mod_factor/owner.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #calculate(options = {}) ⇒ Object
- #calculate!(options = {}) ⇒ Object
- #changed_for_autosave? ⇒ Boolean
- #factor_changes ⇒ Object
- #factor_hash ⇒ Object
- #property_changes ⇒ Object
- #property_hash ⇒ Object
Class Method Details
.included(base) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/mod_factor/owner.rb', line 4 def self.included base base.extend ClassMethods base.after_initialize do |obj| if base.instance_variable_defined? :@factor_model factor_model = base.instance_variable_get :@factor_model obj.send(factor_model).each do |factor| obj.instance_variable_set "@#{factor.key.to_sym}", factor.amount obj.factor_hash[factor.key.to_sym] = factor.amount end base.factor_define.keys.each do |key| if base.factor_define[key][:default] && obj.send(key).nil? obj.instance_variable_set "@#{key}", base.factor_define[key][:default] end end end if base.instance_variable_defined? :@property_model property_model = base.instance_variable_get :@property_model obj.send(property_model).each do |property| define = base.property_define[property.key.to_sym] value = property.value value = case define[:type] when :decimal BigDecimal.new value.to_s when :integer value.to_i when :boolean if value.is_a?(FalseClass) || value.nil? || value == 0 || value =~ /^(f|false|0)$/i || (value.respond_to? :empty? and value.empty?) false else true end else property.value end obj.instance_variable_set "@#{property.key.to_sym}", value obj.property_hash[property.key.to_sym] = value end base.property_define.keys.each do |key| if base.property_define[key][:default] && obj.send(key).nil? obj.instance_variable_set "@#{key}", base.property_define[key][:default] end end end end base.after_save do |obj| if base.instance_variable_defined? :@factor_model factor_model = base.instance_variable_get :@factor_model factor_changes.each do |key| factor = obj.send(factor_model).find_or_initialize_by_key key factor.amount = BigDecimal.new(obj.send(key).to_s) factor.save! obj.instance_variable_set "@#{key}", factor.amount end obj.reload end if base.instance_variable_defined? :@property_model property_model = base.instance_variable_get :@property_model property_changes.each do |key| property = obj.send(property_model).find_or_initialize_by_key key property.value = obj.send(key) property.save! obj.instance_variable_set "@#{key}", property.value end obj.reload end end end |
Instance Method Details
#calculate(options = {}) ⇒ Object
154 155 156 157 158 159 160 161 162 |
# File 'lib/mod_factor/owner.rb', line 154 def calculate ={} self.class.generate_list.each do |key| if (condition = self.class.factor_define[key][:condition]).nil? || self.instance_exec(, &condition) raise "formula should be defined when generate" unless callable = self.class.factor_define[key][:formula] instance_variable_set "@#{key}", self.instance_exec(, &callable) end end end |
#calculate!(options = {}) ⇒ Object
164 165 166 167 |
# File 'lib/mod_factor/owner.rb', line 164 def calculate! ={} calculate save! end |
#changed_for_autosave? ⇒ Boolean
169 170 171 |
# File 'lib/mod_factor/owner.rb', line 169 def changed_for_autosave? !factor_changes.empty? || !property_changes.empty? || super end |
#factor_changes ⇒ Object
135 136 137 138 139 |
# File 'lib/mod_factor/owner.rb', line 135 def factor_changes self.class.factor_define.keys.select do |key| factor_hash[key] != __send__(key) end end |
#factor_hash ⇒ Object
128 129 130 131 132 133 |
# File 'lib/mod_factor/owner.rb', line 128 def factor_hash @factor_hash ||= self.class.factor_define.keys.inject({}) do |out, item| out[item] = self.class.factor_define[item][:default] out end end |
#property_changes ⇒ Object
148 149 150 151 152 |
# File 'lib/mod_factor/owner.rb', line 148 def property_changes self.class.property_define.keys.select do |key| property_hash[key] != __send__(key) end end |
#property_hash ⇒ Object
141 142 143 144 145 146 |
# File 'lib/mod_factor/owner.rb', line 141 def property_hash @property_hash ||= self.class.factor_define.keys.inject({}) do |out, item| out[item] = self.class.factor_define[item][:default] out end end |