Permalink
Browse files

Ruby 1.9 and DataMapper 0.10

  • Loading branch information...
1 parent 64fda32 commit fecfac6da70a156c317193d62e7ddba8ae080300 Tim Morgan committed Dec 18, 2009
View
2 README
@@ -657,7 +657,7 @@ Let's further assume you have a simple DataMapper object:
class Fortune
include DataMapper::Resource
- property :id, Integer, :serial => true
+ property :id, Serial
property :text, String
end
View
2 README.textile
@@ -676,7 +676,7 @@ Let's further assume you have a simple DataMapper object:
<pre><code>
class Fortune
include DataMapper::Resource
- property :id, Integer, :serial => true
+ property :id, Serial
property :text, String
end
</code></pre>
View
2 leaves/scorekeeper/lib/datamapper_extensions.rb
@@ -12,7 +12,7 @@
def set_timestamps
return unless dirty? || new_record?
TIMESTAMP_PROPERTIES.each do |name,(_type,proc)|
- if model.properties(repository.name).has_property?(name)
+ if model.properties(repository.name).named?(name)
model.properties(repository.name)[name].set(self, proc.call(self, model.properties(repository.name)[name])) unless attribute_dirty?(name)
end
end
View
2 leaves/scorekeeper/models/channel.rb
@@ -4,7 +4,7 @@
class Channel
include DataMapper::Resource
- property :id, Integer, :serial => true
+ property :id, Serial
property :server, String, :key => true
property :name, String, :key => true
View
10 leaves/scorekeeper/models/person.rb
@@ -3,12 +3,12 @@
class Person
include DataMapper::Resource
- property :id, Integer, :serial => true
- property :server, String, :nullable => false, :unique_index => :server_and_name
- property :name, String, :nullable => false, :unique_index => :server_and_name
- property :authorized, Boolean, :nullable => false, :default => true
+ property :id, Serial
+ property :server, String, :required => true, :unique_index => :server_and_name
+ property :name, String, :required => true, :unique_index => :server_and_name
+ property :authorized, Boolean, :required => true, :default => true
has n, :scores, :child_key => [ :receiver_id ]
- has n, :scores_awarded, :class_name => 'Score', :child_key => [ :giver_id ]
+ has n, :scores_awarded, :model => 'Score', :child_key => [ :giver_id ]
has n, :pseudonyms
end
View
6 leaves/scorekeeper/models/pseudonym.rb
@@ -4,9 +4,9 @@
class Pseudonym
include DataMapper::Resource
- property :id, Integer, :serial => true
- property :name, String, :nullable => false, :index => true
- property :person_id, Integer, :nullable => false, :index => true
+ property :id, Serial
+ property :name, String, :required => true, :index => true
+ property :person_id, Integer, :required => true, :index => true
belongs_to :person
end
View
14 leaves/scorekeeper/models/score.rb
@@ -3,16 +3,16 @@
class Score
include DataMapper::Resource
- property :id, Integer, :serial => true
- property :giver_id, Integer, :nullable => false, :index => :giver_and_receiver
- property :receiver_id, Integer, :nullable => false, :index => :giver_and_receiver
- property :channel_id, Integer, :nullable => false, :index => true
- property :change, Integer, :nullable => false, :default => 0
+ property :id, Serial
+ property :giver_id, Integer, :required => true, :index => :giver_and_receiver
+ property :receiver_id, Integer, :required => true, :index => :giver_and_receiver
+ property :channel_id, Integer, :required => true, :index => true
+ property :change, Integer, :required => true, :default => 0
property :note, String
timestamps :created_at
- belongs_to :giver, :class_name => 'Person', :child_key => [ :giver_id ]
- belongs_to :receiver, :class_name => 'Person', :child_key => [ :receiver_id ]
+ belongs_to :giver, :model => 'Person', :child_key => [ :giver_id ]
+ belongs_to :receiver, :model => 'Person', :child_key => [ :receiver_id ]
belongs_to :channel
validates_with_method :cant_give_scores_to_self
View
2 libs/daemon.rb
@@ -196,7 +196,7 @@ def method_missing(meth, *args) # :nodoc:
end
def inspect # :nodoc:
- "#<#{self.class.to_s} #{@@instances.index self}>"
+ "#<#{self.class.to_s} #{@@instances.key self}>"
end
private
View
284 libs/datamapper_hacks.rb
@@ -1,289 +1,11 @@
-# A set of hacks to make DataMapper play more nicely with classes within
-# modules.
-
-module DataMapper # :nodoc:
-
- #HACK Add module names to auto-generated class names in relationships
- #
- # When a class name is automatically inferred from a relationship name (e.g.,
- # guessing that has_many :widgets refers to a Widget class), it is necessary
- # to enclose these class names in the same modules as the calling class. For
- # example, if MyLeaf::Factory has_many :widgets, this hack will ensure the
- # inferred class name is MyLeaf::Widget, instead of just ::Widget.
- #
- # This hack is performed for each of the association types in DataMapper. An
- # :old_behavior option is given to revert to the unhacked method.
-
- module Associations # :nodoc:
- module OneToMany # :nodoc:
- class << self
- alias_method :old_setup, :setup
- def setup(name, model, options={})
- class_name = options.fetch(:class_name, Extlib::Inflection.classify(name))
- if not options[:old_behavior] and not class_name.include?('::') then
- modules = model.to_s.split('::')
- modules.pop
- modules << class_name
- options[:class_name] = modules.join('::')
- end
- old_setup(name, model, options)
- end
- end
- end
-
- module OneToOne # :nodoc:
- class << self
- alias_method :old_setup, :setup
- def setup(name, model, options={})
- class_name = options.fetch(:class_name, Extlib::Inflection.classify(name))
- if not options[:old_behavior] and not class_name.include?('::') then
- modules = model.to_s.split('::')
- modules.pop
- modules << class_name
- options[:class_name] = modules.join('::')
- end
- old_setup(name, model, options)
- end
- end
- end
-
- module ManyToOne # :nodoc:
- class << self
- alias_method :old_setup, :setup
- def setup(name, model, options={})
- class_name = options.fetch(:class_name, Extlib::Inflection.classify(name))
- if not options[:old_behavior] and not class_name.include?('::') then
- modules = model.to_s.split('::')
- modules.pop
- modules << class_name
- options[:class_name] = modules.join('::')
- end
- old_setup(name, model, options)
- end
- end
- end
- end
-end
-
-#HACK Strip module names when auto-generating table names for has-many-through
-# relationships.
-#
-# By default, DataMapper will not strip module names when creating the join
-# tables for has-many-through relationships. So, if MyLeaf::Post has and belongs
-# to many MyLeaf::Category, the join table will be called
-# "my_leaf/categories_my_leaf/posts", which is clearly an invalid table name.
-# This hack strips module components from a class name before generating the
-# join table's name.
-#
-# A side effect of this hack is that no two DataMapper models for the same
-# repository can share the same name, even if they are in separate modules.
-#
-# This also fixes a bug that can occur when script/console is launched. The
-# double assignment of the relationship variable seems to mess up IRb, so it has
-# been split into two assignments.
-
-DataMapper::Associations::ManyToMany.module_eval do # :nodoc:
- def self.setup(name, model, options={})
- class_name = options.fetch(:class_name, Extlib::Inflection.classify(name))
- if not options[:old_behavior] and not class_name.include?('::') then
- modules = model.to_s.split('::')
- modules.pop
- modules << class_name
- options[:class_name] = modules.join('::')
- end
-
- assert_kind_of 'name', name, Symbol
- assert_kind_of 'model', model, DataMapper::Model
- assert_kind_of 'options', options, Hash
-
- repository_name = model.repository.name
-
- model.class_eval <<-EOS, __FILE__, __LINE__
- def #{name}(query = {})
- #{name}_association.all(query)
- end
-
- def #{name}=(children)
- #{name}_association.replace(children)
- end
-
- private
-
- def #{name}_association
- @#{name}_association ||= begin
- unless relationship = model.relationships(#{repository_name.inspect})[#{name.inspect}]
- raise ArgumentError, "Relationship #{name.inspect} does not exist in \#{model}"
- end
- association = Proxy.new(relationship, self)
- parent_associations << association
- association
- end
- end
- EOS
-
- opts = options.dup
- opts.delete(:through)
- opts[:child_model] ||= opts.delete(:class_name) || Extlib::Inflection.classify(name)
- opts[:parent_model] = model
- opts[:repository_name] = repository_name
- opts[:remote_relationship_name] ||= opts.delete(:remote_name) || Extlib::Inflection.tableize(opts[:child_model])
- opts[:parent_key] = opts[:parent_key]
- opts[:child_key] = opts[:child_key]
- opts[:mutable] = true
-
- names = [ opts[:child_model].demodulize, opts[:parent_model].name.demodulize ].sort
- model_name = names.join.gsub("::", "")
- storage_name = Extlib::Inflection.tableize(Extlib::Inflection.pluralize(names[0]) + names[1])
- model_module = model.to_s.split('::')
- model_module.pop
- model_module = model_module.join('::')
- model_module = model_module.empty? ? Object : eval("::#{model_module}")
-
- opts[:near_relationship_name] = Extlib::Inflection.tableize(model_name).to_sym
-
- model.has(model.n, opts[:near_relationship_name], :old_behavior => true)
-
- relationship = DataMapper::Associations::RelationshipChain.new(opts)
- model.relationships(repository_name)[name] = relationship
-
- unless model_module.const_defined?(model_name)
- model = DataMapper::Model.new(storage_name)
-
- model.class_eval <<-EOS, __FILE__, __LINE__
- def self.name; #{model_name.inspect} end
- def self.default_repository_name; #{repository_name.inspect} end
- def self.many_to_many; true end
- EOS
-
- names.each do |n|
- model.belongs_to(Extlib::Inflection.underscore(n).gsub("/", "_").to_sym, :class_name => n)
- end
-
- model_module.const_set(model_name, model)
- end
-
- relationship
- end
-end
-
-#HACK Update methods in RelationshipChain to use the scoped repository.
-#
-# This hack will update methods to use the currently-scoped repository, instead
-# of always using the default repository.
-
-module DataMapper # :nodoc:
- module Associations # :nodoc:
- class RelationshipChain # :nodoc:
- def near_relationship
- parent_model.relationships(repository.name)[@near_relationship_name]
- end
-
- def remote_relationship
- return nil unless near_relationship
- near_relationship.child_model.relationships(repository.name)[@remote_relationship_name] ||
- near_relationship.child_model.relationships(repository.name)[@remote_relationship_name.to_s.singularize.to_sym]
- end
- end
- end
-end
-
-DataMapper::Model.class_eval do
-
- #HACK Determine the child key from the given repository, not the default one.
- #
- # Updates this method to use the hacked child_key method.
-
- def properties_with_subclasses(repository_name = default_repository_name)
- properties = DataMapper::PropertySet.new
- ([ self ].to_set + (respond_to?(:descendants) ? descendants : [])).each do |model|
- model.relationships(repository_name).each_value { |relationship| relationship.child_key(repository_name) }
- model.many_to_one_relationships.each do |relationship| relationship.child_key(repository_name) end
- model.properties(repository_name).each do |property|
- properties << property unless properties.has_property?(property.name)
- end
- end
- properties
- end
-end
-
-DataMapper::Associations::Relationship.class_eval do
-
- #HACK Determine the child key from the given repository, not the default one.
- #
- # Updates this method to take a repository name. The child key will be
- # determined from the properties scoped to the given repository.
- #
- # The @child_key class variable is changed to a hash that maps repository
- # names to the appropriate key.
-
- def child_key(repository_name=nil)
- repository_name ||= repository.name
- @child_key ||= Hash.new
- @child_key[repository_name] ||= begin
- child_key = nil
- repository(repository_name).scope do |r|
- model_properties = child_model.properties(repository_name)
-
- child_key = parent_key(repository_name).zip(@child_properties || []).map do |parent_property,property_name|
- # TODO: use something similar to DM::NamingConventions to determine the property name
- parent_name = Extlib::Inflection.underscore(Extlib::Inflection.demodulize(parent_model.base_model.name))
- property_name ||= "#{parent_name}_#{parent_property.name}".to_sym
-
- if model_properties.has_property?(property_name)
- model_properties[property_name]
- else
- options = {}
-
- [ :length, :precision, :scale ].each do |option|
- options[option] = parent_property.send(option)
- end
-
- # NOTE: hack to make each many to many child_key a true key,
- # until I can figure out a better place for this check
- if child_model.respond_to?(:many_to_many)
- options[:key] = true
- end
-
- child_model.property(property_name, parent_property.primitive, options)
- end
- end
- end
- DataMapper::PropertySet.new(child_key)
- end
- return @child_key[repository_name]
- end
-
- #HACK Determine the parent key from the given repository, not the default one.
- #
- # Updates this method to take a repository name. The parent key will be
- # determined from the properties scoped to the given repository.
- #
- # The @parent_key class variable is changed to a hash that maps repository
- # names to the appropriate key.
-
- def parent_key(repository_name=nil)
- repository_name ||= repository.name
- @parent_key ||= Hash.new
- @parent_key[repository_name] ||= begin
- parent_key = nil
- repository(repository_name).scope do |r|
- parent_key = if @parent_properties
- parent_model.properties(repository_name).slice(*@parent_properties)
- else
- parent_model.key(repository_name)
- end
- end
- DataMapper::PropertySet.new(parent_key)
- end
- return @parent_key[repository_name]
- end
-end
+# # A set of hacks to make DataMapper play more nicely with classes within
+# # modules.
# Add a method to return all models defined for a repository.
DataMapper::Repository.class_eval do
def models
- DataMapper::Resource.descendants.select { |cl| not cl.properties(name).empty? or not cl.relationships(name).empty? }
+ DataMapper::Model.descendants.select { |cl| not cl.properties(name).empty? or not cl.relationships(name).empty? }
#HACK we are assuming that if a model has properties or relationships
# defined for a repository, then it must be contextual to that repo
end
View
2 libs/genesis.rb
@@ -3,6 +3,7 @@
require 'set'
require 'rubygems'
+require 'active_support'
require 'yaml'
require 'logger'
require 'facets'
@@ -134,7 +135,6 @@ def load_databases
return
end
- gem 'addressable', '2.0.2'
require 'dm-core'
require 'libs/datamapper_hacks'
View
12 libs/leaf.rb
@@ -355,10 +355,10 @@ def inspect # :nodoc:
#
# alias_command :google, :g
- def self.alias_command(old, new)
- raise NoMethodError, "Unknown command #{old}" unless instance_methods.include?("#{old}_command")
- alias_method "#{new}_command", "#{old}_command"
- @@view_alias[new] = old
+ def self.alias_command(old, nw)
+ raise NoMethodError, "Unknown command #{old}" unless instance_methods.include?(:"#{old}_command")
+ alias_method :"#{nw}_command", :"#{old}_command"
+ @@view_alias[nw] = old
end
# Adds a filter to the end of the list of filters to be run before a command
@@ -558,7 +558,7 @@ def someone_did_quit(stem, person, msg)
def commands_command(stem, sender, reply_to, msg)
commands = self.class.instance_methods.select { |m| m =~ /^\w+_command$/ }
- commands.map! { |m| m.match(/^(\w+)_command$/)[1] }
+ commands.map! { |m| m.to_s.match(/^(\w+)_command$/)[1] }
commands.reject! { |m| UNADVERTISED_COMMANDS.include? m }
return if commands.empty?
commands.map! { |c| "#{options[:command_prefix]}#{c}" }
@@ -659,7 +659,7 @@ def parse_view(name)
end
def leaf_name
- Foliater.instance.leaves.index self
+ Foliater.instance.leaves.key self
end
def run_before_filters(cmd, stem, channel, sender, command, msg)
View
1 libs/misc.rb
@@ -2,7 +2,6 @@
# Core objects.
require 'thread'
-require 'active_support'
class Numeric # :nodoc:
View
12 libs/stem_facade.rb
@@ -79,7 +79,7 @@ def change_nick(new_nick)
def grant_user_privilege(channel, nick, privilege)
channel = normalized_channel_name(channel)
- privcode = server_type.privilege.index(privilege).chr if server_type.privilege.value? privilege
+ privcode = server_type.privilege.key(privilege).chr if server_type.privilege.value? privilege
privcode ||= privilege
mode channel, "+#{privcode}", nick
end
@@ -91,7 +91,7 @@ def grant_user_privilege(channel, nick, privilege)
def remove_user_privilege(channel, nick, privilege)
channel = normalized_channel_name(channel)
- privcode = server_type.privilege.index(privilege).chr if server_type.privilege.value? privilege
+ privcode = server_type.privilege.key(privilege).chr if server_type.privilege.value? privilege
privcode ||= privilege
mode channel, "-#{privcode}", nick
end
@@ -106,7 +106,7 @@ def remove_user_privilege(channel, nick, privilege)
# grant_usermode 'UpMobility', 'os'
def grant_usermode(nick, property)
- propcode = server_type.usermode.index(property).chr if server_type.usermode.value? property
+ propcode = server_type.usermode.key(property).chr if server_type.usermode.value? property
propcode ||= property
mode nick, "+#{property}"
end
@@ -118,7 +118,7 @@ def grant_usermode(nick, property)
# with the letter code for the usermode.
def remove_usermode(nick, property)
- propcode = server_type.usermode.index(property).chr if server_type.usermode.value? property
+ propcode = server_type.usermode.key(property).chr if server_type.usermode.value? property
propcode ||= property
mode nick, "-#{property}"
end
@@ -135,7 +135,7 @@ def remove_usermode(nick, property)
def set_channel_property(channel, property, argument=nil)
channel = normalized_channel_name(channel)
- propcode = server_type.channel_property.index(property).chr if server_type.channel_property.value? property
+ propcode = server_type.channel_property.key(property).chr if server_type.channel_property.value? property
propcode ||= property
mode channel, "+#{propcode}", argument
end
@@ -148,7 +148,7 @@ def set_channel_property(channel, property, argument=nil)
def unset_channel_property(channel, property, argument=nil)
channel = normalized_channel_name(channel)
- propcode = server_type.channel_property.index(property).chr if server_type.channel_property.value? property
+ propcode = server_type.channel_property.key(property).chr if server_type.channel_property.value? property
propcode ||= property
mode channel, "-#{propcode}", argument
end

0 comments on commit fecfac6

Please sign in to comment.