Skip to content
This repository has been archived by the owner on Sep 30, 2018. It is now read-only.

Commit

Permalink
Add RecordArray for Model.all to store class models
Browse files Browse the repository at this point in the history
Record Arrays will also be used by associations and will have the
ability to fire events for adding/removing of records.
adambeynon committed Aug 16, 2013
1 parent ee80771 commit 689e906
Showing 4 changed files with 101 additions and 1 deletion.
4 changes: 3 additions & 1 deletion opal/vienna/persistence.rb
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ def identity_map

# Return a simple array of all models
def all
identity_map.values
@all ||= RecordArray.new
end

def find(id, &block)
@@ -60,6 +60,7 @@ def fetch(options = {}, &block)

def reset!
@identity_map = {}
@all = nil
end
end

@@ -114,6 +115,7 @@ def did_destroy
def did_create
@new_record = false
self.class.identity_map[self.id] = self
self.class.all.push self

trigger_events(:create)
end
32 changes: 32 additions & 0 deletions opal/vienna/record_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Vienna
# A RecordArray acts as a wrapper and delegator around an array of
# record instances.
#
# NOTE: RecordArray implements many methods required so that it can
# be run with method_missing turned off.
class RecordArray < BasicObject
attr_accessor :records

def initialize
@records = []
end

def method_missing(sym, *args, &block)
@records.__send__(sym, *args, &block)
end

def each(&block)
@records.each(&block)
end

def size
@records.size
end

alias length size

def push(obj)
@records.push obj
end
end
end
16 changes: 16 additions & 0 deletions spec/model/persistence_spec.rb
Original file line number Diff line number Diff line change
@@ -2,6 +2,8 @@

describe Vienna::Model do

before { SimpleModel.reset! }

let(:model) { SimpleModel.new }

describe "#did_destroy" do
@@ -39,6 +41,12 @@
model.class.identity_map[863].should eq(model)
end

it "adds record to class record array" do
model.class.all.should == []
model.did_create
model.class.all.should == [model]
end

it "triggers a :create event on the record" do
called = false
model.on(:create) { called = true }
@@ -53,4 +61,12 @@
called.should be_true
end
end

describe ".all" do
it "is a record array of all models" do
model.class.all.should == []
model.did_create
model.class.all.should == [model]
end
end
end
50 changes: 50 additions & 0 deletions spec/record_array_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'spec_helper'
require 'vienna/record_array'

describe Vienna::RecordArray do
let(:array) { Vienna::RecordArray.new }

describe "#initialize" do
it "should have an empty array of records" do
array.records.should eq([])
end
end

describe "#method_missing" do
it "should delegate all method calls to @record" do
array.class.should eq(Array)
end
end

describe "#each" do
it "should enumerate over array passing each item to block" do
array.records = [:foo, :bar, :baz]
result = []
array.each { |a| result << a }
result.should eq([:foo, :bar, :baz])
end
end

describe "#size" do
it "returns the size of the array" do
array.size.should == 0
array.records = [1, 2, 3]
array.size.should == 3
end
end

describe "#length" do
it "returns the size of the array" do
array.length.should eq(0)
end
end

describe "#push" do
it "pushes the object onto the array" do
array.push :foo
array.records.should == [:foo]
array.push :bar
array.records.should == [:foo, :bar]
end
end
end

0 comments on commit 689e906

Please sign in to comment.