Class: CalculableAttrs::ModelCalculableAttrsScope
- Inherits:
-
Object
- Object
- CalculableAttrs::ModelCalculableAttrsScope
- Defined in:
- lib/calculable_attrs/model_calculable_attrs_scope.rb
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#ids ⇒ Object
readonly
Returns the value of attribute ids.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Instance Method Summary collapse
- #add_all_attrs ⇒ Object
- #add_attr(attribute) ⇒ Object
- #add_attrs(attrs) ⇒ Object
- #add_id(id) ⇒ Object
- #calculate ⇒ Object
- #calculated_attrs_values(id) ⇒ Object
- #calculators_to_use ⇒ Object
- #has_attrs ⇒ Object
-
#initialize(model) ⇒ ModelCalculableAttrsScope
constructor
A new instance of ModelCalculableAttrsScope.
Constructor Details
#initialize(model) ⇒ ModelCalculableAttrsScope
Returns a new instance of ModelCalculableAttrsScope.
4 5 6 7 8 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 4 def initialize(model) @model = model @attrs = [] @ids = [] end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
2 3 4 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 2 def attrs @attrs end |
#ids ⇒ Object (readonly)
Returns the value of attribute ids.
2 3 4 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 2 def ids @ids end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
2 3 4 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 2 def model @model end |
Instance Method Details
#add_all_attrs ⇒ Object
23 24 25 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 23 def add_all_attrs @attrs = model.calculable_attrs end |
#add_attr(attribute) ⇒ Object
18 19 20 21 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 18 def add_attr(attribute) attribute = attribute.to_sym @attrs.push(attribute) unless @attrs.include?(attribute) end |
#add_attrs(attrs) ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 10 def add_attrs(attrs) if attrs == true || attrs == [ true ] add_all_attrs else attrs.each { |attr| add_attr(attr) } end end |
#add_id(id) ⇒ Object
31 32 33 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 31 def add_id(id) @ids.push(id.to_i) end |
#calculate ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 35 def calculate @calculable_attrs_values = nil calculators_to_use.each do |calculator| calculated_values = calculator.calculate_many(calculator.attrs & @attrs, ids) merge_calculated_values(calculated_values) end end |
#calculated_attrs_values(id) ⇒ Object
43 44 45 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 43 def calculated_attrs_values(id) @calculable_attrs_values[id.to_i] end |
#calculators_to_use ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 48 def calculators_to_use calculators_to_use = [] @attrs.each do |attribute| calculator = @model.calculable_attrs_calculators[attribute] calculators_to_use.push(calculator) unless calculators_to_use.include?(calculator) end calculators_to_use end |
#has_attrs ⇒ Object
27 28 29 |
# File 'lib/calculable_attrs/model_calculable_attrs_scope.rb', line 27 def has_attrs @attrs.size > 0 end |