Code Monkey home page Code Monkey logo

json_model's Introduction

JSON Model for Rails

ActiveRecord replacement for pure JSON models.

Installation

Put the following line in your Gemfile:

gem 'json_model', :git => 'git://github.com/pch/json_model.git'

And run bundle install afterwards.

Usage

class Car
  include JsonModel

  attribute :make,  String, :default => "Audi"
  attribute :model, String
  attribute :color, String
  attribute :bought_on, TimeStamp
end

class Address
  include JsonModel

  attribute :street, String
  attribute :postal_code, String
end

class Person
  include JsonModel

  attribute :name, String
  attribute :id, Integer
  attribute :address, Address, :default => Address.new

  has_many :cars
  has_many :red_cars, :class => Car

  before_dump :scientologist_filter
  after_load  :add_nobiliary_particle

  def scientologist_filter
    # One of the main principles of the Church of Scientology
    # states that its members cannot be dumped to JSON
    false if self.name == "Tom Cruise"
  end

  def add_nobliary_particle
    self.name = self.name.gsub(" ", " von ")
  end
end

person = Person.new(:id => 1, :address => {:street => '5th Ave', :postal_code => '00-000'})
person.to_json

Loading objects from JSON:

person = Person.from_json(json_string)

Updating attributes:

person.update_attributes(:name => "Henry", :address => {:street => '5th Ave', :postal_code => '00-000'})

Using JsonModel with Rails 3 validators and form helpers

In order to ensure Rails 3 compatibility, you'll need to include some ActiveModel modules in your JsonModel classes:

class User include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming include JsonModel include JsonModel::ValidationHelper

  attribute :name,  String
  attribute :email, String

  validates :name,  :presence => true
  validates :email, :presence => true
end

Note on Patches/Pull Requests

  • Fork the project.
  • Make your feature addition or bug fix.
  • Add tests for it. This is important so I don't break it in a future version unintentionally.
  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
  • Send me a pull request. Bonus points for topic branches.

Credits

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.