No release in over a year
This ActiveResource extension which integrates ActiveModel::Attributes for type casting and default values.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Active Resource Attributes

This gem is a monkey-patch an extension for ActiveResource which integrates ActiveModel::Attributes module and allows to define attributes for type casting and default values.

Installation

Add gem to your Gemfile

gem "activeresource-attributes"

Usage

Include the module in the ActiveResource model and define your attributes

class Person < ActiveResource::Base
  self.site = "http://example.com"

  include ActiveResourse::Attributes

  attribute :integer_field, :integer
  attribute :string_field, :string
  attribute :float_field, :float
  attribute :date_field, :date
  attribute :boolean_field, :boolean
  attribute :field_with_default_value, :integer, default: 123
end

The values are casted to the defined types and default values are supported.

person = Person.new

person.integer_field = "2.3"
person.integer_field # => 2

person.float_field = "12.3"
person.float_field # => 12.3

person.date_field = "2016-01-01"
person.date_field # => Fri, 01 Jan 2016

person.boolean_field = "0"
person.boolean_field # => false

person.field_with_default_value # => 123

Contribution

Please, feel free to contribute and report any bugs.