From cfdd09ae21dc4e342249eaf0e5c86ff6116e6906 Mon Sep 17 00:00:00 2001 From: Aaron Quint Date: Fri, 22 Oct 2010 12:03:20 -0400 Subject: [PATCH] Fixes for ruby 1.9.2 compatability --- Rakefile | 2 +- lib/static_model.rb | 2 +- lib/static_model/base.rb | 48 +++++++-------- static_model.gemspec | 2 +- test/test_generator_helper.rb | 6 +- test/test_helper.rb | 18 +++--- test/test_static_model.rb | 84 ++++++++++++-------------- test/test_static_model_associations.rb | 4 +- test/test_static_model_generator.rb | 3 +- test/test_static_model_scope.rb | 8 +-- 10 files changed, 83 insertions(+), 94 deletions(-) diff --git a/Rakefile b/Rakefile index ee1cb5b..d7be86c 100644 --- a/Rakefile +++ b/Rakefile @@ -13,7 +13,7 @@ begin s.summary = 'ActiveRecord like functionalities for reading from YAML with a simple class implementation' s.description = %q{StaticModel provides a Base class much like ActiveRecord which supports reading from a YAML file and basic associations to ActiveRecord} s.rubyforge_project = %q{quirkey} - s.add_runtime_dependency(%q, [">= 2.2.0"]) + s.add_runtime_dependency(%q, ["~>2.3.8"]) s.add_runtime_dependency(%q, [">= 1.5.1"]) s.add_development_dependency(%q, [">= 1.2.0"]) end diff --git a/lib/static_model.rb b/lib/static_model.rb index 1cd6346..8824b5a 100644 --- a/lib/static_model.rb +++ b/lib/static_model.rb @@ -4,6 +4,7 @@ require 'yaml' unless defined?(YAML) require 'erb' unless defined?(ERB) +gem 'activesupport', '~>2.3.8' require 'active_support' module StaticModel @@ -16,4 +17,3 @@ module StaticModel require 'static_model/comparable' require 'static_model/base' require 'static_model/rails' - diff --git a/lib/static_model/base.rb b/lib/static_model/base.rb index c269e1d..caf2a3e 100644 --- a/lib/static_model/base.rb +++ b/lib/static_model/base.rb @@ -1,13 +1,13 @@ -module StaticModel +module StaticModel class Base include StaticModel::Associations include StaticModel::ActiveRecord include StaticModel::Comparable - + @@load_path = File.join('config', 'data') attr_reader :id - + def initialize(attribute_hash = {}, force_load = true) self.class.load if force_load raise(StaticModel::BadOptions, "Initializing a model is done with a Hash {} given #{attribute_hash.inspect}") unless attribute_hash.is_a?(Hash) @@ -17,7 +17,7 @@ def initialize(attribute_hash = {}, force_load = true) end def to_s - self.inspect + "<#{self.class} #{@attributes.inspect}>" end def self.attribute(name, options = {}) @@ -27,24 +27,24 @@ def self.attribute(name, options = {}) }.merge(options) @defined_attributes ||= {} @defined_attributes[name.to_s] = options - + module_eval <<-EOT def #{name} @attributes['#{name}'] || #{options[:default].inspect} end - + def #{name}=(value) if !#{options[:freeze].inspect} @attributes['#{name}'] = value end end - + def #{name}? !!#{name} end EOT end - + def self.defined_attributes @defined_attributes || {} end @@ -60,7 +60,7 @@ def attributes=(attribute_hash) def attribute_names (attributes.keys | self.class.class_attributes.keys | self.class.defined_attributes.keys).collect {|k| k.to_s } end - + def has_attribute?(name) name.to_s == 'id' || attribute_names.include?(name.to_s) end @@ -95,7 +95,7 @@ def find_first records[0] end alias_method :first, :find_first - + def find_last records[records.length-1] end @@ -109,7 +109,7 @@ def find_first_by(attribute, value) records.find {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false } end alias_method :find_by, :find_first_by - + def find_last_by(attribute, value) records.reverse.find {|r| r.has_attribute?(attribute) ? (r.send(attribute) == value) : false } end @@ -134,7 +134,7 @@ def load(reload = false) @records = records && !records.empty? ? records.dup.collect {|r| new(r, false) } : [] @loaded = true end - + def reload! load(true) end @@ -154,29 +154,29 @@ def set_data_file(file_path) @loaded = false @records = nil end - + def count records.length end - + def class_attributes load @class_attributes ||= {} end - + def class_attribute(name) class_attributes[name] end - + def records load @records end - + def next_id last_id + 1 end - + def last_id @last_id ||= 0 end @@ -184,12 +184,12 @@ def last_id def last_id=(new_last_id) @last_id = new_last_id if new_last_id > self.last_id end - + protected def default_data_file_path File.join(@@load_path, "#{self.to_s.tableize}.yml") end - + private def method_missing(meth, *args) meth_name = meth.to_s @@ -202,13 +202,13 @@ def method_missing(meth, *args) end super end - end + end protected def set_attribute(name, value) self.attributes[name] = value end - + def get_attribute(name) attributes.has_key?(name) ? attributes[name] : self.class.class_attribute(name) end @@ -227,6 +227,6 @@ def method_missing(meth, *args) end super end - + end -end \ No newline at end of file +end diff --git a/static_model.gemspec b/static_model.gemspec index 6a2d641..8d28cb7 100644 --- a/static_model.gemspec +++ b/static_model.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Aaron Quint"] - s.date = %q{2009-12-03} + s.date = %q{2010-05-06} s.description = %q{StaticModel provides a Base class much like ActiveRecord which supports reading from a YAML file and basic associations to ActiveRecord} s.email = %q{aaron at quirkey dot com} s.extra_rdoc_files = [ diff --git a/test/test_generator_helper.rb b/test/test_generator_helper.rb index 9d9debf..7a49d39 100644 --- a/test/test_generator_helper.rb +++ b/test/test_generator_helper.rb @@ -1,8 +1,4 @@ -begin - require File.dirname(__FILE__) + '/test_helper' -rescue LoadError - require 'test/unit' -end +require 'test_helper' require 'fileutils' # Must set before requiring generator libs. diff --git a/test/test_helper.rb b/test/test_helper.rb index 116c237..4395df0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,11 +3,11 @@ require 'shoulda' require 'mocha' -require File.dirname(__FILE__) + '/../lib/static_model' +require File.expand_path(File.join(File.dirname(__FILE__), '../lib/static_model')) class Book < StaticModel::Base set_data_file File.join(File.dirname(__FILE__), 'data', 'books.yml') - + attribute :rating, :default => 3 attribute :read, :default => false, :freeze => true end @@ -26,7 +26,7 @@ class Article < ActiveRecord::Base class Publisher < StaticModel::Base set_data_file File.join(File.dirname(__FILE__), 'data', 'publishers.yml') - + has_many :authors end @@ -39,12 +39,12 @@ class Author < StaticModel::Base class Page < StaticModel::Base set_data_file File.join(File.dirname(__FILE__), 'data', 'pages.yml') - + end - + class Store < StaticModel::Base set_data_file File.join(File.dirname(__FILE__), 'data', 'stores.yml') - + end class Project < StaticModel::Base @@ -53,7 +53,7 @@ class Project < StaticModel::Base end class Test::Unit::TestCase - + def assert_all(collection) collection.each do |one| assert yield(one), "#{one} is not true" @@ -82,9 +82,9 @@ def assert_ordered(array_of_ordered_items, message = nil, &block) i += 1 end end - + def assert_set_of(klass, set) assert set.respond_to?(:each), "#{set} is not a set (does not include Enumerable)" assert_all(set) {|a| a.is_a?(klass) } end -end \ No newline at end of file +end diff --git a/test/test_static_model.rb b/test/test_static_model.rb index 660ac52..e705b89 100644 --- a/test/test_static_model.rb +++ b/test/test_static_model.rb @@ -1,9 +1,9 @@ -require File.dirname(__FILE__) + '/test_helper.rb' +require 'test_helper' class TestStaticModel < Test::Unit::TestCase context "StaticModel" do - context "A class that inherits from Base" do + context "A class that inherits from Base" do context "an instance" do setup do @@ -20,7 +20,7 @@ class TestStaticModel < Test::Unit::TestCase assert_raise(StaticModel::BadOptions) do Book.new("im bad") end - end + end end context "attributes" do @@ -34,8 +34,8 @@ class TestStaticModel < Test::Unit::TestCase assert_equal 'New Title', @book.title end end - - context "attribute" do + + context "attribute" do should "define methods for the attribute" do book = Book[1] assert book.respond_to?(:rating) @@ -43,7 +43,7 @@ class TestStaticModel < Test::Unit::TestCase assert book.respond_to?(:rating?) assert_equal 5, book.rating end - + should "return the default if attribute is not set" do book = Book[2] assert book.respond_to?(:rating) @@ -51,27 +51,21 @@ class TestStaticModel < Test::Unit::TestCase assert book.respond_to?(:rating?) assert_equal 3, book.rating end - + should "freeze the attribute" do book = Book[1] assert book.read? book.read = false assert book.read? end - - end - context "to_s" do - should "inspect" do - assert_equal @book.inspect, @book.to_s - end end - + context "has_attribute?" do should "return false if attribute doesn't exist" do assert !@book.has_attribute?(:blurgh) end - + should "return true if attribute exists" do assert @book.has_attribute?(:id) assert @book.has_attribute?(:title) @@ -113,7 +107,7 @@ class TestStaticModel < Test::Unit::TestCase end should "reload with next find" do - assert @author.attributes, @new_book.attributes + assert_equal @author.attributes, @new_book.attributes end teardown do @@ -127,9 +121,9 @@ class TestStaticModel < Test::Unit::TestCase should "be length + 1" do Store.load assert_equal Store.count + 1, Store.next_id - end + end end - + context "if ids were manualy assigned" do should "be 1 after the last id" do Publisher.load @@ -137,63 +131,63 @@ class TestStaticModel < Test::Unit::TestCase end end end - + context "where the records are defined without ids" do setup do Store.reload! end - + context "loading" do setup do @stores = Store.all end - + should "automaticaly assign ids" do @stores.each do |store| assert store.id assert store.id.is_a?(Fixnum) end end - + should "assign next id" do assert Store.next_id assert_equal 3, Store.next_id end end - + context "initializing without id" do setup do @store = Store.new({:name => 'Metro Comics', :city => 'New York'}) end - + should "return instance" do assert @store.is_a?(Store) end - + should "set attributes" do assert_equal 'New York', @store.city end - + should "assign id from next id" do assert_equal 3, @store.id end - + should "increment next_id" do assert_equal 4, Store.next_id end - + end end - + context "loading a data_file with embedded erb" do setup do @projects = Project.all end - + should "evaluate erb expressions at load time" do assert_equal 1.day.ago.strftime('%m/%d/%Y %I:%M%p'), @projects.first.created_at end - + should "evaluate erb in current context" do assert_equal Author.first, @projects[1].author end @@ -282,11 +276,11 @@ class TestStaticModel < Test::Unit::TestCase setup do @book = Book.find_last end - + should "return the last instance from all records" do assert_equal Book.find_all.last, @book end - + should "return an instance of klass" do assert @book.is_a?(Book) end @@ -349,21 +343,21 @@ class TestStaticModel < Test::Unit::TestCase end end end - + context "find_last_by" do setup do @author = 'Chuck Palahniuk' @book = Book.find_last_by(:author, @author) end - + should "return an instance of klass" do assert @book.is_a?(Book) end - + should "return record matching search" do assert @author, @book.author end - + context "when there is no match" do should "return nil" do assert_nil Book.find_last_by(:author, 'Michael R. Bernstein') @@ -454,40 +448,40 @@ class TestStaticModel < Test::Unit::TestCase assert_equal Book.all.length, Book.count end end - + context "with a class with yaml class vars" do setup do @pages = Page.all end - + should "load :records into @records" do assert_set_of Page, @pages assert Page.loaded? end - + should "give access to top level attributes as class methods" do assert_equal 'http://www.quirkey.com', Page.url assert_equal 'The Best Ever', Page.title end - + should "return a hash for class attribute" do assert Page.settings.is_a?(Hash) assert_equal 'test', Page.settings['username'] end - + should "have class attributes appear as record accessor defaults if none exist" do assert_equal 'http://www.quirkey.com', Page[1].url end - + should "not overwrite record specific methods" do assert_equal 'http://github.com', Page[2].url end - + context "class_attributes" do setup do @attributes = Page.class_attributes end - + should "return a hash of all top level settings" do assert @attributes.is_a?(Hash) assert_equal 3, @attributes.length diff --git a/test/test_static_model_associations.rb b/test/test_static_model_associations.rb index 29adaec..730c6c9 100644 --- a/test/test_static_model_associations.rb +++ b/test/test_static_model_associations.rb @@ -1,4 +1,4 @@ -require File.dirname(__FILE__) + '/test_helper.rb' +require 'test_helper' class TestStaticModelAssociations < Test::Unit::TestCase @@ -90,4 +90,4 @@ class TestStaticModelAssociations < Test::Unit::TestCase end end -end \ No newline at end of file +end diff --git a/test/test_static_model_generator.rb b/test/test_static_model_generator.rb index 735c0a4..6d06717 100644 --- a/test/test_static_model_generator.rb +++ b/test/test_static_model_generator.rb @@ -1,5 +1,4 @@ -require File.join(File.dirname(__FILE__), "test_generator_helper.rb") - +require 'test_helper' class TestStaticModelGenerator < Test::Unit::TestCase include RubiGen::GeneratorTestHelper diff --git a/test/test_static_model_scope.rb b/test/test_static_model_scope.rb index f5fd31a..a23cc14 100644 --- a/test/test_static_model_scope.rb +++ b/test/test_static_model_scope.rb @@ -1,14 +1,14 @@ -require File.dirname(__FILE__) + '/test_helper.rb' +require 'test_helper' class TestStaticModelScope < Test::Unit::TestCase # context "a static model class" do # context "A class with scope" do - # + # # end # end - + def test_truth assert true end -end \ No newline at end of file +end