Class: AttributeDependsCalculator::Parameter
- Inherits:
-
Object
- Object
- AttributeDependsCalculator::Parameter
- Defined in:
- lib/attribute_depends_calculator/parameter.rb
Constant Summary collapse
- OPERATORS =
%i(+ *)
- METHODS =
%i(sum average count minimum maximum)
Instance Attribute Summary collapse
-
#depend_association_name ⇒ Object
Returns the value of attribute depend_association_name.
-
#depend_column ⇒ Object
Returns the value of attribute depend_column.
-
#operator ⇒ Object
Returns the value of attribute operator.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #expression ⇒ Object
- #fetch ⇒ Object
-
#initialize(params) ⇒ Parameter
constructor
A new instance of Parameter.
- #operator_filter ⇒ Object
Constructor Details
#initialize(params) ⇒ Parameter
Returns a new instance of Parameter.
9 10 11 12 13 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 9 def initialize(params) self.operator = params.values_at(:operator).first || :sum self.params = params fetch end |
Instance Attribute Details
#depend_association_name ⇒ Object
Returns the value of attribute depend_association_name.
4 5 6 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 4 def depend_association_name @depend_association_name end |
#depend_column ⇒ Object
Returns the value of attribute depend_column.
4 5 6 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 4 def depend_column @depend_column end |
#operator ⇒ Object
Returns the value of attribute operator.
4 5 6 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 4 def operator @operator end |
#params ⇒ Object
Returns the value of attribute params.
4 5 6 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 4 def params @params end |
Instance Method Details
#expression ⇒ Object
20 21 22 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 20 def expression operator_filter end |
#fetch ⇒ Object
15 16 17 18 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 15 def fetch self.depend_association_name = params.keys.first self.depend_column = params.values.first end |
#operator_filter ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/attribute_depends_calculator/parameter.rb', line 24 def operator_filter if OPERATORS.include? operator "pluck(:#{depend_column}).compact.reduce(0, :#{operator})" elsif METHODS.include? operator "#{operator}(:#{depend_column})" else raise 'The operator of depends calculator are incorrect' end end |