Skip to content

Commit

Permalink
Added basics of find/create/initialize.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Dec 28, 2010
1 parent 6624f28 commit b1e1547
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/scam.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
module Scam
# Your code goes here...
def self.included(base)
base.extend(ClassMethods)
base.class.send(:include, Enumerable)
base.class_eval do
attr_accessor(:id)
end
end

module ClassMethods
def create(attrs={})
new(attrs).tap { |i| instances << i }
end

def each
instances.each { |i| yield(i) }
end

def find(id)
detect { |i| i.id == id.to_i }
end

def instances
@instances ||= []
end
private :instances
end

def initialize(attrs={})
attrs.each { |key, value| send("#{key}=", value) }
end
end
17 changes: 17 additions & 0 deletions spec/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
$:.unshift(File.expand_path('../../lib', __FILE__))

require 'rubygems'
require 'bundler'

Bundler.require(:default, :development)

require 'scam'
require 'support/constants'

Rspec.configure do |config|
config.include(Support::Constants)

config.before(:each) do

end
end
68 changes: 68 additions & 0 deletions spec/scam_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'helper'

describe Scam do
uses_constants('FeedTemplate')

context "including Scam" do
it "adds id attr accessor" do
template = FeedTemplate.new
template.id = 5
template.id.should == 5
end
end

context "class enumerable" do
it "works" do
t1 = FeedTemplate.create(:id => 1)
t2 = FeedTemplate.create(:id => 2)
FeedTemplate.inject([]) { |acc, t| acc << t; acc }.should == [t1, t2]
FeedTemplate.detect { |t| t.id == 1 }.should == t1
end
end

describe ".initialize" do
before do
@template = FeedTemplate.new(:id => 5)
end

it "sets attributes" do
@template.id.should == 5
end
end

describe ".find" do
before do
@template = FeedTemplate.create(:id => 1)
end

context "with integer id" do
it "returns instance if found" do
FeedTemplate.find(1).should == @template
end
end

context "with string id" do
it "returns instance if found" do
FeedTemplate.find('1').should == @template
end
end

it "returns nil if not found" do
FeedTemplate.find(20000000000).should be_nil
end
end

describe ".create" do
before do
@template = FeedTemplate.create(:id => 1)
end

it "returns instance" do
@template.should be_instance_of(FeedTemplate)
end

it "adds model to list" do
FeedTemplate.should include(@template)
end
end
end
40 changes: 40 additions & 0 deletions spec/support/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Support
module Constants
def self.included(base)
base.extend(ClassMethods)
end

module ClassMethods
def uses_constants(*constants)
before { create_constants(*constants) }
end
end

def create_constants(*constants)
constants.each { |constant| create_constant(constant) }
end

def remove_constants(*constants)
constants.each { |constant| remove_constant(constant) }
end

def create_constant(constant)
remove_constant(constant)
Object.const_set(constant, Model(constant))
end

def remove_constant(constant)
Object.send(:remove_const, constant) if Object.const_defined?(constant)
end

def Model(name=nil)
Class.new.tap do |model|
model.class_eval """
def self.name; '#{name}' end
def self.to_s; '#{name}' end
""" if name
model.send(:include, Scam)
end
end
end
end

0 comments on commit b1e1547

Please sign in to comment.