Sha256: e935bc8db67635e64aa469c496a5e4799cb665bcb61611b93974228097e71d7f

Contents?: true

Size: 836 Bytes

Versions: 1

Compression:

Stored size: 836 Bytes

Contents

# frozen_string_literal: true

require 'ams_types/version'

# adds types to ActiveModel::Serializers attributes
# @example
#   class DataSerializer < ActiveModel::Serializer
#     attributes_integer :id
#     attributes_float :price_usd, :price_btc
module AMSTypes
  def self.included(base)
    base.extend ClassMethods
  end

  # extension methods
  module ClassMethods
    def attributes_float(*attrs)
      attrs.each do |attr|
        attribute(attr) unless _attributes_data.fetch(attr, nil)
        define_method(attr) do
          object.send(attr.to_sym).to_f
        end
      end
    end

    def attributes_integer(*attrs)
      attrs.each do |attr|
        attribute(attr) unless _attributes_data.fetch(attr, nil)
        define_method(attr) do
          object.send(attr.to_sym).to_i
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ams_types-0.1.0 lib/ams_types.rb