Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding support for multiple_word_models in the relationships + more d…

…ocumentation.
  • Loading branch information...
commit c2f2b2b43c07820521595c01519f4ea420f216dc 1 parent b67b5ca
Clay Allsopp authored
Showing with 50 additions and 10 deletions.
  1. +41 −10 lib/remote_model.rb
  2. +9 −0 lib/string.rb
51 lib/remote_model.rb
View
@@ -3,16 +3,33 @@ class RemoteModel
HTTP_METHODS = [:get, :post, :put, :delete]
class << self
+ # These three methods (has_one/many/ + belongs_to)
+ # map a symbol to a class for method_missing lookup
+ # for each :symbol in params.
+ # Can also be used to view the current mappings:
+ # EX
+ # Question.has_one
+ # => {:user => User}
+
+ # EX
+ # self.has_one :question, :answer, :camel_case
+ # => {:question => Question, :answer => Answer, :camel_case => CamelCase}
def has_one(params = [])
- make_fn_lookup "has_one", params, lambda { |sym| sym.to_s.capitalize }
+ make_fn_lookup "has_one", params, lambda { |sym| sym.to_s.split("_").collect {|s| s.capitalize}.join }
end
+ # EX
+ # self.has_many :questions, :answers, :camel_cases
+ # => {:questions => Question, :answers => Answer, :camel_cases => CamelCase}
def has_many(params = [])
- make_fn_lookup "has_many", params, lambda { |sym| sym.to_s[0..-2].capitalize }
+ make_fn_lookup "has_many", params, lambda { |sym| sym.to_s.singularize.split("_").collect {|s| s.capitalize}.join }
end
+ # EX
+ # self.belongs_to :question, :answer, :camel_case
+ # => {:question => Question, :answer => Answer, :camel_case => CamelCase}
def belongs_to(params = [])
- make_fn_lookup "belongs_to", params, lambda { |sym| sym.to_s.capitalize }
+ make_fn_lookup "belongs_to", params, lambda { |sym| sym.to_s.split("_").collect {|s| s.capitalize}.join }
end
def pluralize
@@ -20,6 +37,12 @@ def pluralize
end
private
+ # This is kind of neat.
+ # Because models can be mutually dependent (User has a Question, Question has a User),
+ # sometimes RubyMotion hasn't loaded the classes when this is run.
+ # SO we check to see if the class is loaded; if not, then we just add it to the
+ # namespace to make everything run smoothly and assume that by the time the app is running,
+ # all the classes have been loaded.
def make_klass(klass_str)
begin
klass = Object.const_get(klass_str)
@@ -28,23 +51,31 @@ def make_klass(klass_str)
end
end
+ # How we fake define_method, essentially.
+ # ivar_suffix -> what is the new @ivar called
+ # params -> the :symbols to map to classes
+ # transform -> how we transform the :symbol into a class name
def make_fn_lookup(ivar_suffix, params, transform)
ivar = "@" + ivar_suffix
if !instance_variable_defined? ivar
instance_variable_set(ivar, {})
end
+ sym_to_klass_sym = {}
if params.class == Symbol
- params = [params]
+ sym_to_klass_sym[params] = params
+ elsif params.class == Array
+ params.each {|klass_sym|
+ sym_to_klass_sym[klass_sym] = klass_sym
+ }
+ else
+ sym_to_klass_sym = params
end
- params.each do |klass_sym|
- klass_str = transform.call(klass_sym)
- instance_variable_get(ivar)[klass_sym] = make_klass(klass_str)
- if block_given?
- yield klass_sym
+ sym_to_klass_sym.each do |relation_sym, klass_sym|
+ klass_str = transform.call(klass_sym)
+ instance_variable_get(ivar)[relation_sym] = make_klass(klass_str)
end
- end
instance_variable_get(ivar)
end
9 lib/string.rb
View
@@ -0,0 +1,9 @@
+class String
+ def pluralize
+ self + "s"
+ end
+
+ def singularize
+ self[0..-2]
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.