Module: Videojuicer::Resource::Relationships::Has::ClassMethods

Defined in:
lib/videojuicer/resource/relationships/has.rb

Instance Method Summary collapse

Instance Method Details

#has(quantifier, name, options = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/videojuicer/resource/relationships/has.rb', line 18

def has(quantifier, name, options={})
  options = {
    :class=>name.to_s.capitalize,
    :foreign_key=>"#{name}_id"
  }.merge(options)
  
  define_method name do
    id = self.send(options[:foreign_key])
    klass = (options[:class].is_a?(String))? Videojuicer.const_get(options[:class]) : options[:class]
    return nil unless id
    begin
      return klass.get(id)
    rescue Videojuicer::Exceptions::NoResource
      return nil
    end
  end
  
  define_method "#{name}=" do |arg|
    self.send("#{options[:foreign_key]}=", arg.id)
  end
end

#nObject

Returns a :many quantifier for the ##has method.



14
15
16
# File 'lib/videojuicer/resource/relationships/has.rb', line 14

def n
  :many
end