Class: BMC::Serializers::Base

Inherits:
Object
  • Object
show all
Defined in:
app/serializers/bmc/serializers/base.rb

Direct Known Subclasses

XLSX

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options = {})
  @data    = data
  @options = options
end

Class Attribute Details

.decimals_precisionObject



24
25
26
# File 'app/serializers/bmc/serializers/base.rb', line 24

def decimals_precision
  @decimals_precision ||= 2
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



2
3
4
# File 'app/serializers/bmc/serializers/base.rb', line 2

def data
  @data
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'app/serializers/bmc/serializers/base.rb', line 2

def options
  @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_dataObject



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

Raises:

  • (NotImplementedError)


19
20
21
# File 'app/serializers/bmc/serializers/base.rb', line 19

def render_file(_file_path)
  raise NotImplementedError
end

#render_inlineObject

Raises:

  • (NotImplementedError)


15
16
17
# File 'app/serializers/bmc/serializers/base.rb', line 15

def render_inline
  raise NotImplementedError
end