Class: BMC::Serializers::Base
- Inherits:
-
Object
- Object
- BMC::Serializers::Base
- Defined in:
- app/serializers/bmc/serializers/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .format(value) ⇒ Object
- .format_boolean(value) ⇒ Object
- .format_date_or_time(value) ⇒ Object
- .format_decimal(value) ⇒ Object
- .format_default(value) ⇒ Object
- .format_integer(value) ⇒ Object
- .formatter_for(value) ⇒ Object
Instance Method Summary collapse
- #formatted_data ⇒ Object
-
#initialize(data, options = {}) ⇒ Base
constructor
A new instance of Base.
- #render_file(_file_path) ⇒ Object
- #render_inline ⇒ Object
Constructor Details
#initialize(data, options = {}) ⇒ Base
Returns a new instance of Base.
4 5 6 7 |
# File 'app/serializers/bmc/serializers/base.rb', line 4 def initialize(data, = {}) @data = data @options = end |
Class Attribute Details
.decimals_precision ⇒ Object
24 25 26 |
# File 'app/serializers/bmc/serializers/base.rb', line 24 def decimals_precision @decimals_precision ||= 2 end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
2 3 4 |
# File 'app/serializers/bmc/serializers/base.rb', line 2 def data @data end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
2 3 4 |
# File 'app/serializers/bmc/serializers/base.rb', line 2 def @options end |
Class Method Details
.format(value) ⇒ Object
58 59 60 61 |
# File 'app/serializers/bmc/serializers/base.rb', line 58 def format(value) formatter = "format_" + formatter_for(value).to_s public_send(formatter, value) end |
.format_boolean(value) ⇒ Object
38 39 40 |
# File 'app/serializers/bmc/serializers/base.rb', line 38 def format_boolean(value) I18n.t(value.to_s) end |
.format_date_or_time(value) ⇒ Object
42 43 44 |
# File 'app/serializers/bmc/serializers/base.rb', line 42 def format_date_or_time(value) value end |
.format_decimal(value) ⇒ Object
34 35 36 |
# File 'app/serializers/bmc/serializers/base.rb', line 34 def format_decimal(value) value.to_f.round(decimals_precision) # Fix BigDecimal and 0.1 + 0.2 end |
.format_default(value) ⇒ Object
46 47 48 |
# File 'app/serializers/bmc/serializers/base.rb', line 46 def format_default(value) value.to_s end |
.format_integer(value) ⇒ Object
30 31 32 |
# File 'app/serializers/bmc/serializers/base.rb', line 30 def format_integer(value) value end |
.formatter_for(value) ⇒ Object
50 51 52 53 54 55 56 |
# File 'app/serializers/bmc/serializers/base.rb', line 50 def formatter_for(value) return :integer if value.is_a?(Integer) return :decimal if value.is_a?(Numeric) return :boolean if value.is_a?(TrueClass) || value.is_a?(FalseClass) return :date_or_time if value.is_a?(Date) || value.is_a?(Time) return :default end |
Instance Method Details
#formatted_data ⇒ Object
9 10 11 12 13 |
# File 'app/serializers/bmc/serializers/base.rb', line 9 def formatted_data data.map do |line| line.map { |value| self.class.format(value) } end end |
#render_file(_file_path) ⇒ Object
19 20 21 |
# File 'app/serializers/bmc/serializers/base.rb', line 19 def render_file(_file_path) raise NotImplementedError end |
#render_inline ⇒ Object
15 16 17 |
# File 'app/serializers/bmc/serializers/base.rb', line 15 def render_inline raise NotImplementedError end |