Project

entasis

0.01
Repository is archived
No release in over 3 years
Low commit activity in last 3 years
Entasis provides a few useful methods for building a basic class. Handy for models without a database.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.0

Runtime

 Project Readme

entasis

Build Status Code Climate

Entasis provides a few neat methods for building a basic class. Handy for models without a database.

Example:

class Person
  include Entasis::Model

  attributes :name, :age, :city

  validates :name, presence: true

  def age=(years)
    @age = years.to_i
  end
end

person = Person.new name: 'Hilda', age: '23', city: 'Stockholm'

person.attribute_names # => ["name", "age", "city"]
person.attributes      # => {"name"=>"Hilda", "age"=>23, "city"=>"Stockholm"}

anon = Person.new
anon.valid?           # => false
anon.errors           # => {:name=>["can't be blank"]}>

Relations

You can build simple relations between objects.

Example:

class Person
  include Entasis::Model

  has_many :friends

  attributes :name
end

class Friend
  include Entasis::Model

  belongs_to :best_friend, class: 'Person'

  attributes :name
end


person = Person.new name: 'Anna', friends: [{ name: 'Emma' }, { name: 'Johan' }]

person.friends                           # => [#<Friend:0x0 @name="Emma">, #<Friend:0x1 @name="Johan">]
person.friends[0].best_friend == person  # => true

Strict checking of attributes

Default behavior is to ignore any key in the hash given to .new or #attributes= that's not in the list of attribute names. By setting passing the option strict: true to the attribute definition it will raise an UnknownAttributeError for that class every time an unknown key is in the given hash.

Transposing keys

If you include the module Entasis::TransposeKeys after you have included Entasis::Model, keys in the given hash will be downcased and underscored before calling the setter methods.

This can be very useful when you got hash with camelcased keys, for example from an external service serving XML soup.

Rails versions

Rails 4.x supported from 2.x and on. Use entasis 1.x for Rails 3.x.

Contributors

  • Ingemar Edsborn (ingemar)
  • Gabriel Reis (greis)
  • Jack Christensen (jackc)
  • Jaime Iniesta (aimeiniesta)
  • Johnny Winn (nurugger07)
  • Joshua Davey (jgdavey)