Sha256: e5ab7b07211f301d703780de28a5fdad1f853499607aa228c1c445d4438b9b64

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

class Agilibox::Serializers::Base
  attr_reader :data, :options

  def initialize(data, options = {})
    @data    = data
    @options = options
  end

  def render_inline
    raise NotImplementedError
  end

  def render_file(file_path)
    raise NotImplementedError
  end

  class << self
    def decimals_precision
      @decimals_precision ||= 2
    end

    attr_writer :decimals_precision

    def format_integer(value)
      value
    end

    def format_decimal(value)
      value.to_f.round(decimals_precision) # Fix BigDecimal and 0.1 + 0.2
    end

    def format_boolean(value)
      I18n.t(value.to_s)
    end

    def format_datetime(value)
      Agilibox::AllHelpers.date(value)
    end

    def format_default(value)
      value.to_s
    end

    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 :datetime if value.is_a?(Date) || value.is_a?(Time)
      return :default
    end

    def format(value)
      formatter = "format_" + formatter_for(value).to_s
      public_send(formatter, value)
    end
  end # class << self
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
agilibox-1.0.9 app/serializers/agilibox/serializers/base.rb
agilibox-1.0.8 app/serializers/agilibox/serializers/base.rb