Module: EfoNelfo::HasMany::ClassMethods

Defined in:
lib/efo_nelfo/has_many.rb

Instance Method Summary collapse

Instance Method Details

#association(post_type) ⇒ Object



62
63
64
65
# File 'lib/efo_nelfo/has_many.rb', line 62

def association(post_type)
  type = post_type.is_a?(EfoNelfo::PostType) ? post_type.post_type : post_type.to_s.upcase
  @associations[type]
end

#associationsObject



58
59
60
# File 'lib/efo_nelfo/has_many.rb', line 58

def associations
  @associations
end

#has_many(name, options) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/efo_nelfo/has_many.rb', line 41

def has_many(name, options)
  post_type = options[:post_type]

  @associations ||= {}
  @associations[post_type] = name

  define_method name do
    instance_variable_get("@#{name}") || instance_variable_set("@#{name}", EfoNelfo::Collection.new(self, post_type))
  end

  define_method "#{name}=" do |values|
    instance_variable_set "@#{name}", EfoNelfo::Collection.new(self, post_type)
    values.each { |item| instance_variable_get("@#{name}") << item } if values
  end

end