Skip to content

Commit

Permalink
Fixes for ruby 1.9.2 compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Oct 22, 2010
1 parent e4eb977 commit cfdd09a
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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<activesupport>, [">= 2.2.0"])
s.add_runtime_dependency(%q<activesupport>, ["~>2.3.8"])
s.add_runtime_dependency(%q<rubigen>, [">= 1.5.1"])
s.add_development_dependency(%q<Shoulda>, [">= 1.2.0"])
end
Expand Down
2 changes: 1 addition & 1 deletion lib/static_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'yaml' unless defined?(YAML)
require 'erb' unless defined?(ERB)

gem 'activesupport', '~>2.3.8'
require 'active_support'

module StaticModel
Expand All @@ -16,4 +17,3 @@ module StaticModel
require 'static_model/comparable'
require 'static_model/base'
require 'static_model/rails'

48 changes: 24 additions & 24 deletions lib/static_model/base.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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 = {})
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -95,7 +95,7 @@ def find_first
records[0]
end
alias_method :first, :find_first

def find_last
records[records.length-1]
end
Expand All @@ -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
Expand All @@ -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
Expand All @@ -154,42 +154,42 @@ 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

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
Expand All @@ -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
Expand All @@ -227,6 +227,6 @@ def method_missing(meth, *args)
end
super
end

end
end
end
2 changes: 1 addition & 1 deletion static_model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
6 changes: 1 addition & 5 deletions test/test_generator_helper.rb
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
18 changes: 9 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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
end
Loading

0 comments on commit cfdd09a

Please sign in to comment.