Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Simple, ActiveModel-compliant models.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 .gitignore
Octocat-spinner-32 .travis.yml
Octocat-spinner-32 Gemfile
Octocat-spinner-32 MIT-LICENSE
Octocat-spinner-32 README.md
Octocat-spinner-32 Rakefile
Octocat-spinner-32 modest_model.gemspec
README.md

ModestModel Build Status

Overview

Inspired by Crafting Rails Applications, ModestModel provides an ActiveModel-compliant class that allows you to quickly create simple, table-less models. The intended use is to back interactions with external APIs with Ruby-friendly models rather than raw structured data (such as hashes).

Notice: we're extending ModestModel's features significantly to give developers more flexibility and freedom when working with table-less models. Some of the upcoming features include:

  • ActiveRecord support: including simple delegation of methods like save and destroy, associations (has_many, belongs_to and friends) and other commonly used ActiveRecord features
  • Basic objects: to make working with simple key/value data sets easier and friendlier (think of a blend of a Hash and a Struct bundled with a collection of helpful methods)

Example

json = MyExternalApi.call('/some/path.json')
attributes_hash = JSON.decode(json)

# => {'name' => 'Michael', 'email' => 'michael@example.com'}

class SampleModel < ModestModel::Base
  attributes :name, :email
end

SampleModel.new(attributes_hash)

# => #<SampleModel @name="Michael"...

Installation

ModestModel has been tested and works on MRI 1.8.7 and 1.9.2.

Rubygems

gem install modest_model

Bundler

gem 'modest_model'

Usage

Similar to ActiveRecord models, simply create a class which inherits from ModestModel::Base. You'll need to define some attributes - this is achieved by calling the attributes method passing in the attribute names you require:

class SampleModel < ModestModel::Base
  attributes :name, :email
end

Features

ModestModel includes the following ActiveModel modules:

  • ActiveModel::Conversion
  • ActiveModel::Naming
  • ActiveModel::Translation
  • ActiveModel::Serialization
  • ActiveModel::Validations
  • ActiveModel::AttributeMethods

These allow your ModestModel models to act almost the same as an ActiveRecord model, but without the database. You can mass-assign attributes, add validations, add translations, and call familiar methods like model_name.human and to_json.

ModestModel was extracted from the Mail Form gem described in chapter 2, "Building Models with Active Model", of Crafting Rails Applications by Jose Valim. This chapter therefore provides an excellent in-depth explanation of the inner workings of ModestModel.

Credits

This code has been derived from Crafting Rails Applications by Jose Valim, so all credit goes to Jose and Plataforma for granting permission to release this gem.

Something went wrong with that request. Please try again.