Class: CalculableAttrs::Calculator
- Inherits:
-
Object
- Object
- CalculableAttrs::Calculator
- Defined in:
- lib/calculable_attrs/calculator.rb
Constant Summary collapse
- CALCULABLE_FOREIGN_KEY =
'__calculable_id__'
Instance Attribute Summary collapse
-
#attrs ⇒ Object
readonly
Returns the value of attribute attrs.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#foreign_key ⇒ Object
readonly
Returns the value of attribute foreign_key.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
- #base_query(attrs, id) ⇒ Object
- #calculable_foreign_key ⇒ Object
- #calculate(attrs, id) ⇒ Object
- #calculate_all(id) ⇒ Object
- #calculate_many(attrs, ids) ⇒ Object
- #calculate_one(attrs, id) ⇒ Object
- #default(attr) ⇒ Object
-
#initialize(relation: nil, foreign_key: nil, attributes: nil) ⇒ Calculator
constructor
A new instance of Calculator.
- #query_with_grouping(attrs, ids) ⇒ Object
- #scoped_relation(id) ⇒ Object
Constructor Details
#initialize(relation: nil, foreign_key: nil, attributes: nil) ⇒ Calculator
Returns a new instance of Calculator.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/calculable_attrs/calculator.rb', line 5 def initialize(relation: nil, foreign_key: nil, attributes: nil) @relation = relation @foreign_key = foreign_key @attrs = [] @formulas = {} @defaults = {} attributes.each do |key, val| key = key.to_sym save_attribute_value(key, val) @attrs << key end end |
Instance Attribute Details
#attrs ⇒ Object (readonly)
Returns the value of attribute attrs.
3 4 5 |
# File 'lib/calculable_attrs/calculator.rb', line 3 def attrs @attrs end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
3 4 5 |
# File 'lib/calculable_attrs/calculator.rb', line 3 def defaults @defaults end |
#foreign_key ⇒ Object (readonly)
Returns the value of attribute foreign_key.
3 4 5 |
# File 'lib/calculable_attrs/calculator.rb', line 3 def foreign_key @foreign_key end |
#relation ⇒ Object (readonly)
Returns the value of attribute relation.
3 4 5 |
# File 'lib/calculable_attrs/calculator.rb', line 3 def relation @relation end |
Instance Method Details
#base_query(attrs, id) ⇒ Object
51 52 53 |
# File 'lib/calculable_attrs/calculator.rb', line 51 def base_query(attrs, id) scoped_relation(id).select(build_select(attrs)) end |
#calculable_foreign_key ⇒ Object
28 29 30 |
# File 'lib/calculable_attrs/calculator.rb', line 28 def calculable_foreign_key CALCULABLE_FOREIGN_KEY end |
#calculate(attrs, id) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/calculable_attrs/calculator.rb', line 20 def calculate(attrs, id) if id.is_a?(Array) calculate_many(attrs, id) else calculate_one(attrs, id) end end |
#calculate_all(id) ⇒ Object
47 48 49 |
# File 'lib/calculable_attrs/calculator.rb', line 47 def calculate_all(id) calculate(attrs, id) end |
#calculate_many(attrs, ids) ⇒ Object
32 33 34 35 36 |
# File 'lib/calculable_attrs/calculator.rb', line 32 def calculate_many(attrs, ids) query = query_with_grouping(attrs, ids) records = query.load normalize_many_records_result(ids, attrs, records) end |
#calculate_one(attrs, id) ⇒ Object
42 43 44 45 |
# File 'lib/calculable_attrs/calculator.rb', line 42 def calculate_one(attrs, id) record = base_query(attrs, id).load.first normalize_one_record_result(attrs, record) end |
#default(attr) ⇒ Object
63 64 65 |
# File 'lib/calculable_attrs/calculator.rb', line 63 def default(attr) @defaults.key?(attr) ? @defaults[attr] : 0 end |
#query_with_grouping(attrs, ids) ⇒ Object
38 39 40 |
# File 'lib/calculable_attrs/calculator.rb', line 38 def query_with_grouping(attrs, ids) base_query(attrs, ids).select("#{ @foreign_key } AS #{ calculable_foreign_key }").group(@foreign_key) end |
#scoped_relation(id) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/calculable_attrs/calculator.rb', line 55 def scoped_relation(id) if id @relation.call.where( @foreign_key => id) else @relation.call end end |