Permalink
Browse files

Fixed Ruby 1.9 compatibility (use of : shorthand for 'then') in DataM…

…apper::Query

Small bits of cleanup:
  * Updated various documentation (README, FAQ and RDocs) to reflect new
    DM 0.9 syntax.
  * Rewrote RDoc in DataMapper::Property to reflect new DM syntax.
  * Changed tabs to spaces, fixed formatting in DataMapper::Is::Tree
  * Style changes: Changed instances of 'Datamapper' to 'DataMapper' throughout
  * Other, very minor doc / comment fixes.
Added stub ManyToMany Instance class.
  • Loading branch information...
1 parent 486910d commit 0ba594daef574c108caf194ca92224758868f945 @myabc myabc committed Apr 3, 2008
Showing with 254 additions and 189 deletions.
  1. +10 −8 FAQ
  2. +8 −6 README
  3. +1 −1 lib/data_mapper.rb
  4. +1 −1 lib/data_mapper/adapters/postgres_adapter.rb
  5. +2 −2 lib/data_mapper/associations.rb
  6. +17 −1 lib/data_mapper/associations/many_to_many.rb
  7. +2 −2 lib/data_mapper/associations/many_to_one.rb
  8. +2 −2 lib/data_mapper/associations/one_to_many.rb
  9. +3 −3 lib/data_mapper/associations/parent_to_child_association.rb
  10. +2 −2 lib/data_mapper/auto_migrations.rb
  11. +3 −3 lib/data_mapper/cli.rb
  12. +2 −2 lib/data_mapper/hook.rb
  13. +2 −2 lib/data_mapper/identity_map.rb
  14. +43 −36 lib/data_mapper/is/tree.rb
  15. +3 −4 lib/data_mapper/loaded_set.rb
  16. +2 −2 lib/data_mapper/logger.rb
  17. +3 −3 lib/data_mapper/naming_conventions.rb
  18. +90 −64 lib/data_mapper/property.rb
  19. +2 −2 lib/data_mapper/property_set.rb
  20. +6 −6 lib/data_mapper/query.rb
  21. +3 −4 lib/data_mapper/repository.rb
  22. +3 −3 lib/data_mapper/resource.rb
  23. +4 −4 lib/data_mapper/support/aliasinghash.rb
  24. +6 −6 lib/data_mapper/support/blank.rb
  25. +1 −1 lib/data_mapper/support/enumerable.rb
  26. +2 −2 lib/data_mapper/support/errors.rb
  27. +2 −2 lib/data_mapper/support/inflection.rb
  28. +1 −1 lib/data_mapper/support/kernel.rb
  29. +4 −4 lib/data_mapper/support/object.rb
  30. +1 −1 lib/data_mapper/support/struct.rb
  31. +4 −4 lib/data_mapper/type.rb
  32. +1 −1 lib/data_mapper/types/text.rb
  33. +11 −1 spec/associations/many_to_many_spec.rb
  34. +1 −1 spec/associations/one_to_many_spec.rb
  35. +2 −2 spec/associations/relationship_spec.rb
  36. +4 −0 spec/cli_spec.rb
View
18 FAQ
@@ -9,7 +9,7 @@ If you're working with a table that doesn't have a <tt>:id</tt> column, you
can declare your properties as you usually do, and declare one of them as a
natural key.
- property :name, :string, :key => true
+ property :name, String, :key => true
You should now be able to do <tt>Class['name_string']</tt> as well. Remember:
this column should be unique, so treat it that way. This is the equivalent to
@@ -18,7 +18,7 @@ using <tt>set_primary_key</tt> in ActiveRecord.
=== How do I make a model paranoid?
- property :deleted_at, :datetime
+ property :deleted_at, DateTime
If you've got deleted_at, your model is paranoid auto-magically. All of your
calls to <tt>##all()</tt>, <tt>##first()</tt>, and <tt>##count()</tt> will be
@@ -33,8 +33,9 @@ Write me!
Sure does. Here's an example implementation:
- class Task < DataMapper::Base
- has_and_belongs_to_many :tasks,
+ class Task
+ include DataMapper::Resource
+ many_to_many :tasks,
:join_table => "task_relationships", :left_foreign_key => "parent_id",
:right_foreign_key => "child_id"
end
@@ -47,8 +48,9 @@ You'll notice that instead of <tt>foreign_key</tt> and
Oh yes, and particularly well too.
- class Person < Datamapper::Base
- property :type, :class ## other shared properties here
+ class Person
+ include DataMapper::Resource
+ property :type, Class ## other shared properties here
end
class Salesperson < Person; end
@@ -89,7 +91,7 @@ find, instantiate an object for EVERY result, THEN apply your transformations
to each object in turn. Doesn't sound too horrible unless you have a TON of
records; you WILL grind your system to a screeching and bloody halt.
-Datamapper's <tt>#each</tt> works in sets of 500 so the amount of objects
+DataMapper's <tt>#each</tt> works in sets of 500 so the amount of objects
instantiated at a time won't make your computer think it's a victim in a Saw
movie. Once it's done executing your block on the first set of 500, it moves
on to the next.
@@ -118,7 +120,7 @@ messages (and higher) sent to the logger. For more information on how to work
with Logger[http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/], hit up
http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/.
-Incidentally, if you'd like to send a message into the Datamapper logger, do:
+Incidentally, if you'd like to send a message into the DataMapper logger, do:
repository.adapter.logger.info "your message here"
View
14 README
@@ -5,7 +5,7 @@
== Open Development
-Datamapper sports a very accessible code-base and a welcoming community.
+DataMapper sports a very accessible code-base and a welcoming community.
Outside contributions and feedback are welcome and encouraged, especially
constructive criticism. Make your voice heard! Submit a
ticket[http://wm.lighthouseapp.com/projects/4819-datamapper/overview] or
@@ -72,9 +72,10 @@ control you can enable or disable this feature for any column (not just
text-fields) by passing a @lazy@ option to your column mapping with a value of
<tt>true</tt> or <tt>false</tt>.
- class Animal < DataMapper::Base
- property :name, :string
- property :notes, :text, :lazy => false
+ class Animal
+ include DataMapper::Resource
+ property :name, String
+ property :notes, DataMapper::Types::Text, :lazy => false
end
Plus, lazy-loading of text fields happens automatically and intelligently when
@@ -100,9 +101,10 @@ like seeing a method called <tt>col2Name</tt> on your model just because
that's what it's called in an old database you can't afford to change right
now? In DataMapper you control the mappings:
- class Fruit < DataMapper::Base
+ class Fruit
+ include DataMapper::Resource
set_table_name 'frt'
- property :name, :string, :column => 'col2Name'
+ property :name, String, :column => 'col2Name'
end
== All Ruby, All The Time
View
@@ -5,7 +5,7 @@
# * Sets the applications root and environment for compatibility with rails or merb
# * Checks for the database.yml and loads it if it exists
# * Sets up the database using the config from the yaml file or from the environment
-# *
+#
# Require the basics...
require 'pathname'
@@ -8,7 +8,7 @@ class PostgresAdapter < DataObjectsAdapter
def create_with_returning?; true; end
- end # class PostgresqlAdapter
+ end # class PostgresAdapter
end # module Adapters
end # module DataMapper
@@ -35,5 +35,5 @@ def has(name, cardinality, options = {})
when Fixnum then one_to_one(name, options)
end
end
- end
-end
+ end # module Associations
+end # module DataMapper
@@ -1,12 +1,28 @@
+require __DIR__.parent + 'associations'
require __DIR__ + 'relationship'
module DataMapper
module Associations
module ManyToMany
def many_to_many(name, options = {})
+ target = (options[:class_name] || DataMapper::Inflection.camelize(name))
+
+ self.relationships[name] = Relationship.
+ new(name, options[:repository_name] || self.repository.name, [DataMapper::Inflection.demodulize(self.name), nil], [target, nil])
+
+
end
+
+ class Instance
+ def initialize() end
+
+ def save
+ raise NotImplementedError.new
+ end
+
+ end # class Instance
- end # module HasAndBelongsToMany
+ end # module ManyToMany
end # module Associations
end # module DataMapper
@@ -65,7 +65,7 @@ def save
@relationship.attach_parent(@child, @parent)
end
end
- end
- end # module BelongsTo
+ end # class Instance
+ end # module ManyToOne
end # module Associations
end # module DataMapper
@@ -80,7 +80,7 @@ def delete(child)
raise
end
end
- end
- end
+ end # class Instance
+ end # module OneToMany
end # module Associations
end # module DataMapper
@@ -48,6 +48,6 @@ def delete(child)
raise
end
end
- end
- end
-end
+ end # class ParentToChildAssociation
+ end # module Associations
+end # module DataMapper
@@ -32,5 +32,5 @@ def create_table(table)
def modify_table(table, columns)
raise NotImplementedError.new
end
- end
-end
+ end # module AutoMigrations
+end # module DataMapper
View
@@ -173,7 +173,7 @@ def start(argv = ARGV)
begin
configure(argv)
- DataMapper::Database.setup options
+ DataMapper::Repository.setup options
load_models if config[:models]
@@ -188,5 +188,5 @@ def start(argv = ARGV)
end
end
- end
-end
+ end # module CLI
+end # module DataMapper
View
@@ -64,5 +64,5 @@ def run_hook(name, *args)
end
end
end
- end
-end
+ end # module Hook
+end # module DataMapper
@@ -41,5 +41,5 @@ def clear!(resource)
@cache.delete(resource)
end
- end
-end
+ end # class IdentityMap
+end # module DataMapper
View
@@ -1,19 +1,25 @@
module DataMapper
- module Is
- module Tree
+ module Is
+ module Tree
def self.included(base)
base.extend(ClassMethods)
end
- # An extension to DataMapper to easily allow the creation of tree structures from your DataMapper Models.
- # This requires a foreign key property for your model, which by default would be called :parent_id.
+ # An extension to DataMapper to easily allow the creation of tree
+ # structures from your DataMapper models.
+ # This requires a foreign key property for your model, which by default
+ # would be called :parent_id.
+ #
+ # Example:
+ #
+ # class Category
+ # include DataMapper::Resource
+ # include DataMapper::Is::Tree
+ #
+ # property :id, Fixnum
+ # property :parent_id, Fixnum
+ # property :name, String
#
- # Example:
- #
- # class Category < DataMapper::Base
- # property :parent_id, :integer
- # property :name, :string
- #
# is_a_tree :order => "name"
# end
#
@@ -31,24 +37,24 @@ def self.included(base)
# child.parent # => root
# root.children # => [child]
# root.children.first.children.first # => grandchild1
- # Category.first_root # => root
- # Category.roots # => [root]
+ # Category.first_root # => root
+ # Category.roots # => [root]
#
- # The following instance methods are added:
- # * <tt>children</tt> - Returns all nodes with the current node as their parent, in the order specified by
- # <tt>:order</tt> (<tt>[grandchild1, grandchild2]</tt> when called on <tt>child</tt>)
- # * <tt>parent</tt> - Returns the node referenced by the foreign key (<tt>:parent_id</tt> by
- # default) (<tt>root</tt> when called on <tt>child</tt>)
+ # The following instance methods are added:
+ # * <tt>children</tt> - Returns all nodes with the current node as their parent, in the order specified by
+ # <tt>:order</tt> (<tt>[grandchild1, grandchild2]</tt> when called on <tt>child</tt>)
+ # * <tt>parent</tt> - Returns the node referenced by the foreign key (<tt>:parent_id</tt> by
+ # default) (<tt>root</tt> when called on <tt>child</tt>)
# * <tt>siblings</tt> - Returns all the children of the parent, excluding the current node
- # (<tt>[grandchild2]</tt> when called on <tt>grandchild1</tt>)
+ # (<tt>[grandchild2]</tt> when called on <tt>grandchild1</tt>)
# * <tt>generation</tt> - Returns all the children of the parent, including the current node (<tt>
- # [grandchild1, grandchild2]</tt> when called on <tt>grandchild1</tt>)
+ # [grandchild1, grandchild2]</tt> when called on <tt>grandchild1</tt>)
# * <tt>ancestors</tt> - Returns all the ancestors of the current node (<tt>[root, child1]</tt>
- # when called on <tt>grandchild2</tt>)
+ # when called on <tt>grandchild2</tt>)
# * <tt>root</tt> - Returns the root of the current node (<tt>root</tt> when called on <tt>grandchild2</tt>)
- #
- # Author:: Timothy Bennett (http://lanaer.com)
- module ClassMethods
+ #
+ # Author:: Timothy Bennett (http://lanaer.com)
+ module ClassMethods
# Configuration options are:
#
# * <tt>foreign_key</tt> - specifies the column name to use for tracking of the tree (default: +parent_id+)
@@ -61,24 +67,24 @@ def is_a_tree(options = {})
belongs_to :parent, :class_name => name, :foreign_key => configuration[:foreign_key], :counter_cache => configuration[:counter_cache]
has_many :children, :class_name => name, :foreign_key => configuration[:foreign_key], :order => configuration[:order]
- include DataMapper::Is::Tree::InstanceMethods
+ include DataMapper::Is::Tree::InstanceMethods
- class_eval <<-CLASS
+ class_eval <<-CLASS
def self.roots
self.all :#{configuration[:foreign_key]} => nil, :order => #{configuration[:order].inspect}
end
def self.first_root
self.first :#{configuration[:foreign_key]} => nil, :order => #{configuration[:order].inspect}
end
- CLASS
+ CLASS
- class << self
- alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
- end
+ class << self
+ alias_method :root, :first_root # for people used to the ActiveRecord acts_as_tree
+ end
end
- alias_method :can_has_tree, :is_a_tree # just for fun ;)
+ alias_method :can_has_tree, :is_a_tree # just for fun ;)
end
module InstanceMethods
@@ -92,8 +98,8 @@ def ancestors
end
# Returns the root node of the current node’s tree.
- #
- # grandchild1.root # => root
+ #
+ # grandchild1.root # => root
def root
node = self
node = node.parent while node.parent
@@ -114,8 +120,9 @@ def generation
parent ? parent.children : self.class.roots
end
- alias_method :self_and_siblings, :generation # for those used to the ActiveRecord acts_as_tree
+ alias_method :self_and_siblings, :generation # for those used to the ActiveRecord acts_as_tree
end
- end
- end
-end
+
+ end # Tree
+ end # Is
+end # DataMapper
@@ -101,7 +101,7 @@ def entries
@entries.uniq!
@entries.dup
end
- end
+ end # class LoadedSet
class LazyLoadedSet < LoadedSet
@@ -127,6 +127,5 @@ def entries
super
end
- end
-
-end
+ end # class LazyLoadedSet
+end # module DataMapper
@@ -198,6 +198,6 @@ def #{name}?
LEVELMETHODS
end
- end
+ end # class Logger
-end
+end # module DataMapper
Oops, something went wrong.

0 comments on commit 0ba594d

Please sign in to comment.