Permalink
Browse files

Added basics of find/create/initialize.

  • Loading branch information...
1 parent 6624f28 commit b1e1547b171c2513689fe4ce45bfe8b547598f69 @jnunemaker committed Dec 28, 2010
Showing with 155 additions and 1 deletion.
  1. +30 −1 lib/scam.rb
  2. +17 −0 spec/helper.rb
  3. +68 −0 spec/scam_spec.rb
  4. +40 −0 spec/support/constants.rb
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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.