Class: Dashboards::Metric
- Inherits:
-
Object
- Object
- Dashboards::Metric
- Defined in:
- lib/dashboards/dsl/metric.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name_or_options, options = {}) ⇒ Metric
constructor
A new instance of Metric.
- #render(context) ⇒ Object
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(, = {}) if .is_a?(Hash) @name = nil = else @name = end @value = [:value] @options = .except(:value) @format_big_numbers = [:format_big_numbers] || false @percentage = [:percentage] || false @currency = [:currency] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/dashboards/dsl/metric.rb', line 5 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/dashboards/dsl/metric.rb', line 5 def @options end |
#value ⇒ Object (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 |