Class: Dashboards::Metric

Inherits:
Object
  • Object
show all
Defined in:
lib/dashboards/dsl/metric.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name_or_options, options = {}) ⇒ Metric

Returns a new instance of Metric.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/dashboards/dsl/metric.rb', line 7

def initialize(name_or_options, options = {})
  if name_or_options.is_a?(Hash)
    @name = nil
    options = name_or_options
  else
    @name = name_or_options
  end
  @value = options[:value]
  @options = options.except(:value)
  @format_big_numbers = options[:format_big_numbers] || false
  @percentage = options[:percentage] || false
  @currency = options[:currency]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/dashboards/dsl/metric.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/dashboards/dsl/metric.rb', line 5

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/dashboards/dsl/metric.rb', line 5

def value
  @value
end

Instance Method Details

#render(context) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/dashboards/dsl/metric.rb', line 21

def render(context)
  value_content = @value.is_a?(Proc) ? context.instance_exec(&@value) : @value
  formatted_value = value_content.nil? ? 'N/A' : format_number(value_content)
  formatted_value = "#{@currency}#{formatted_value}" if @currency && !value_content.nil?
  formatted_value += '%' if @percentage && !value_content.nil?
  if @name
    "<div class='metric'>
      <h3>#{@name}</h3>
      <div class='metric-value'>#{formatted_value}</div>
    </div>"
  else
    "<div class='metric'>
      <div class='metric-value'>#{formatted_value}</div>
    </div>"
  end
end