Class: MeterBox::Meter

Inherits:
Object
  • Object
show all
Defined in:
lib/meter_box/meter.rb

Constant Summary collapse

AGGREGATIONS =
%i[sum count latest max min mean count_distinct].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, dimensions:, aggregation: :sum) ⇒ Meter

Returns a new instance of Meter.



9
10
11
12
13
14
15
16
17
# File 'lib/meter_box/meter.rb', line 9

def initialize(name:, dimensions:, aggregation: :sum)
  @name = name.to_sym
  @dimensions = dimensions.transform_keys(&:to_sym)
  @aggregation = aggregation.to_sym
  unless AGGREGATIONS.include?(@aggregation)
    raise ArgumentError,
      "unknown aggregation :#{@aggregation} for meter '#{@name}' (allowed: #{AGGREGATIONS.inspect})"
  end
end

Instance Attribute Details

#aggregationObject (readonly)

Returns the value of attribute aggregation.



7
8
9
# File 'lib/meter_box/meter.rb', line 7

def aggregation
  @aggregation
end

#dimensionsObject (readonly)

Returns the value of attribute dimensions.



7
8
9
# File 'lib/meter_box/meter.rb', line 7

def dimensions
  @dimensions
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/meter_box/meter.rb', line 7

def name
  @name
end

Instance Method Details

#declared_keysObject



19
20
21
# File 'lib/meter_box/meter.rb', line 19

def declared_keys
  @dimensions.keys
end

#validate_dimensions!(supplied) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/meter_box/meter.rb', line 23

def validate_dimensions!(supplied)
  supplied = supplied.transform_keys(&:to_sym)

  check_required!(supplied)
  check_unknown_keys!(supplied)
  check_values!(supplied)
end