Class: AttributeDependsCalculator::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/attribute_depends_calculator/parameter.rb

Constant Summary collapse

OPERATORS =
%i(+ *)
METHODS =
%i(sum average count minimum maximum)

Instance Attribute Summary collapse

Instance Method Summary collapse

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_nameObject

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_columnObject

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

#operatorObject

Returns the value of attribute operator.



4
5
6
# File 'lib/attribute_depends_calculator/parameter.rb', line 4

def operator
  @operator
end

#paramsObject

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

#expressionObject



20
21
22
# File 'lib/attribute_depends_calculator/parameter.rb', line 20

def expression
  operator_filter
end

#fetchObject



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_filterObject



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