Sha256: 59b6adfbbe470b8a2d27aa9495c4448e1acc639bc70ac21c2b3d12ab71a589d6

Contents?: true

Size: 1.28 KB

Versions: 29

Compression:

Stored size: 1.28 KB

Contents

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

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

  def formatted_data
    data.map do |line|
      line.map { |value| self.class.format(value) }
    end
  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

29 entries across 29 versions & 1 rubygems

Version Path
agilibox-1.8.0 app/serializers/agilibox/serializers/base.rb
agilibox-1.7.4 app/serializers/agilibox/serializers/base.rb
agilibox-1.7.3 app/serializers/agilibox/serializers/base.rb
agilibox-1.7.2 app/serializers/agilibox/serializers/base.rb
agilibox-1.7.1 app/serializers/agilibox/serializers/base.rb
agilibox-1.7.0 app/serializers/agilibox/serializers/base.rb
agilibox-1.6.2 app/serializers/agilibox/serializers/base.rb
agilibox-1.6.1 app/serializers/agilibox/serializers/base.rb
agilibox-1.6.0 app/serializers/agilibox/serializers/base.rb