Module: FatSecret::Relations::HasMany::ClassMethods

Defined in:
lib/fat_secret/relations/has_many.rb

Instance Method Summary collapse

Instance Method Details

#has_many(type, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fat_secret/relations/has_many.rb', line 7

def has_many(type, options = {})

  define_method type do
    @servings ||= HasManyProxy.new([])
    if options[:autoload] && @servings.blank? && id
      self.class.get(id).servings
    else
      @servings
    end
  end

  define_method "#{type}=" do |array|
    klass = "FatSecret::#{type.to_s.singularize.classify}".constantize
    servings = array[type.to_s.singularize]
    servings = [servings] unless servings.is_a?(Array)
    servings = servings.map do |attrs|
      klass.new(attrs)
    end
    @servings = HasManyProxy.new(servings)
  end
end