0.0
No commit activity in last 3 years
No release in over 3 years
Reads Rails Database schema and creates a class from selected table with getters and setters.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 10.1
~> 2.14
 Project Readme

Schema Reader

This is no longer supported. Please use ActiveMocker instead.

Build Status

Reads Rails Database schema.rb and creates a class from selected table with getters and setters.

This was created for unit testing Rails ActiveRecord. Instead of creating a mock that can become out of date with real objects schema reader creates mocks form the true definition.

Installation

Add this line to your application's Gemfile:

gem 'schema_reader'

And then execute:

$ bundle

Or install it yourself as:

$ gem install schema_reader

Usage

# db/schema.rb

ActiveRecord::Schema.define(version: 20131226214224) do

  create_table "users", force: true do |t|
    t.string   "name"
    t.integer  "age"
    t.string   "email"
  end

  create_table "comments", force: true do |t|
      t.text    "comment"
      t.integer  "user_id"
      t.datetime "created_at"
      t.datetime "updated_at"
    end

end

# /spec/*
class User
  include SchemaReader
  pass_attributes_to_new true # This enables the new method to takes an attributes' hash
  attr_schema table: 'users', file:  File.new('db/schema.rb', 'r')
end

user = User.new(name: 'Fred', age: 37, email: "fred@example.com")

user.name
    => "Fred"
user.name = "Jane"
user.name
    => "Jane"

user.update(age: 23)
user.age
    => 23

class Comment
  include SchemaReader
  pass_attributes_to_new true
  attr_schema table: 'comments', file:  File.new('db/schema.rb', 'r')
end

# Based off field names ending with _id it will create an association

comment = Comment.new(user: user)
comment.user.name
     => "Jane"

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request