lib/og/relation.rb in og-0.22.0 vs lib/og/relation.rb in og-0.23.0
- old
+ new
@@ -1,40 +1,60 @@
-require 'facet/object/constant'
-require 'facet/string/plural'
-require 'facet/string/demodulize'
-require 'facet/string/underscore'
+require 'nano/object/constant'
+require 'nano/string/capitalized%3F'
+require 'mega/orm_support'
+
module Og
-# A relation between Entities.
+# A relation between Entities.
class Relation
+ # The parameters of this relation.
+
attr_accessor :options
+ # Is this a polymorphic relation?
+
attr_accessor :is_polymorphic
# A generalized initialize method for all relations.
# Contains common setup code.
def initialize(args, options = {})
@options = options
@options.update(args.pop) if args.last.is_a?(Hash)
- if args.empty? or (not (args.last.is_a?(Class) or args.last.is_a?(Symbol)))
- raise 'Class of target not defined'
- end
-
target_name = if collection
:target_plural_name
else
:target_singular_name
end
- @options[:target_class] = args.pop
- @options[target_name] = args.first unless args.empty?
+ # Check that all needed options are provided.
+ if args.empty? or (not (args.last.is_a?(Class) or args.last.is_a?(Symbol)))
+ raise 'Class of target not defined'
+ end
+
+ # Try to set the target class. Checks for class and
+ # class symbol.
+
+ if args.last.to_s.capitalized?
+ @options[:target_class] = args.pop
+ end
+
+ # Try to set the target name.
+
+ if args.last.is_a? Symbol
+ @options[target_name] = args.pop
+ end
+
+ # Inflect target_class if not provided.
+
+ @options[:target_class] ||= @options[target_name].to_s.singular.camelize.intern
+
setup()
end
def setup
if target_class == Object
@@ -191,22 +211,24 @@
module ClassMethods
# === Examples
#
- # belongs_to Article
+ # belongs_to :article # inflects Article
+ # belongs_to Article # inflects :article
# belongs_to :article, Article
# belongs_to :article, Article, :view => 'lala'
def belongs_to(*args)
require 'og/relation/belongs_to'
meta :relations, Og::BelongsTo.new(args, :owner_class => self)
end
# === Examples
#
- # refers_to Topic
+ # refers_to :topic # inflects Topic
+ # refers_to Topic # inflects :topic
def refers_to(*args)
require 'og/relation/refers_to'
meta :relations, Og::RefersTo.new(args, :owner_class => self)
end
@@ -228,13 +250,17 @@
def has_many(*args)
require 'og/relation/has_many'
meta :relations, Og::HasMany.new(args, :owner_class => self, :collection => true)
end
+ # ..
+
def joins_many(*args)
require 'og/relation/joins_many'
meta :relations, Og::JoinsMany.new(args, :owner_class => self, :collection => true)
end
+
+ # ..
def many_to_many(*args)
require 'og/relation/many_to_many'
meta :relations, Og::ManyToMany.new(args, :owner_class => self, :collection => true)
end