ActiveHashExt

This is an extension module to ActiveHash

ActiveHashExt includes the ability to read a schema.rb file from ActiveRecord and set those attributes on a class. It also adds methods from ActiveRecord like update and destroy_all with more to come.

Installation

Add this line to your application's Gemfile:

gem 'active_hash_ext'

And then execute:

$ bundle

Or install it yourself as:

$ gem install active_hash_ext

Usage

describe 'ActiveHashExt' do

  before do

    class TestHash <  ActiveHash::Base
      include ActiveHashExt
      fields :name, :age
    end

    TestHash.delete_all
  end

  it '#update' do

    test_hash = TestHash.new
    test_hash.update(age: 34, name: 'Sam')
    expect(test_hash.age).to eq 34
    expect(test_hash.name).to eq 'Sam'
  end

  it '#destroy_all' do
    TestHash.create(name: 'Jane', age: 56)
    expect(TestHash.count).to eq 1
    TestHash.destroy_all
    expect(TestHash.count).to eq 0
  end

  context 'read_schema' do

    it 'returns attributes from schema file' do
      expect(TestHash.read_schema('custodians', 'schema.rb')).to eq [:name, :age]
    end

    it 'when passed to fields it will set attributes on class from schema' do
      class SchemaHash <  ActiveHash::Base
        include ActiveHashExt
        fields *read_schema('custodians', File.new('schema.rb', 'r'))
      end
      SchemaHash.create(name: 'Fred')
      expect(SchemaHash.first.name).to eq 'Fred'
    end

     it 'will raise an exception if table does not exist' do
       -> {TestHash.read_schema('custodian','schema.rb')}.should raise_error('Table Name not Found!')
     end

  end

end

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