This repository has been archived by the owner on Sep 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RecordArray for Model.all to store class models
Record Arrays will also be used by associations and will have the ability to fire events for adding/removing of records.
1 parent
ee80771
commit 689e906
Showing
4 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |