-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basics of find/create/initialize.
- Loading branch information
1 parent
6624f28
commit b1e1547
Showing
4 changed files
with
155 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
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 |
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,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 |
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,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 |
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,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 |