Sha256: c8d242c07dc974a9e5efb943867bc508146f17d590660a450e4df90fa4361ab1

Contents?: true

Size: 1.05 KB

Versions: 2

Compression:

Stored size: 1.05 KB

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

    def attributes_boolean(*attrs)
      attrs.each do |attr|
        attribute(attr) unless _attributes_data.fetch(attr, nil)
        define_method(attr) do
          object.send(attr.to_sym).to_s == 'true'
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ams_types-0.1.2 lib/ams_types.rb
ams_types-0.1.1 lib/ams_types.rb