Sha256: e2edc01decc42c149aee2213c21490811475798d94aa3d32d203b717118eff55

Contents?: true

Size: 1.17 KB

Versions: 37

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

37 entries across 37 versions & 1 rubygems

Version Path
agilibox-1.5.13 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.12 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.11 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.10 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.9 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.8 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.7 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.6 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.5 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.4 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.3 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.2 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.1 app/serializers/agilibox/serializers/base.rb
agilibox-1.5.0 app/serializers/agilibox/serializers/base.rb
agilibox-1.4.4 app/serializers/agilibox/serializers/base.rb
agilibox-1.4.3 app/serializers/agilibox/serializers/base.rb
agilibox-1.4.2 app/serializers/agilibox/serializers/base.rb
agilibox-1.4.1 app/serializers/agilibox/serializers/base.rb
agilibox-1.4.0 app/serializers/agilibox/serializers/base.rb
agilibox-1.3.6 app/serializers/agilibox/serializers/base.rb