Class: ThroughHierarchy::Hierarchy

Inherits:
BasicObject
Defined in:
lib/through_hierarchy/hierarchy.rb

Instance Method Summary collapse

Constructor Details

#initialize(klass, members) ⇒ Hierarchy

Returns a new instance of Hierarchy.



3
4
5
6
7
8
# File 'lib/through_hierarchy/hierarchy.rb', line 3

def initialize(klass, members)
  @klass = klass
  @members = members

  validate_hierarchy
end

Instance Method Details

#has_many(name, scope = nil, **options) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/through_hierarchy/hierarchy.rb', line 16

def has_many(name, scope = nil, **options)
  options.merge!(scope: scope) if scope.present?
  if options.key?(:uniq)
    assoc = ::ThroughHierarchy::Associations::HasUniq.new(name, @klass, @members, options)
  else
    assoc = ::ThroughHierarchy::Associations::HasMany.new(name, @klass, @members, options)
  end
  ::ThroughHierarchy::Builder.new(@klass).add_association(name, assoc)
end

#has_one(name, scope = nil, **options) ⇒ Object



10
11
12
13
14
# File 'lib/through_hierarchy/hierarchy.rb', line 10

def has_one(name, scope = nil, **options)
  options.merge!(scope: scope) if scope.present?
  assoc = ::ThroughHierarchy::Associations::HasOne.new(name, @klass, @members, options)
  ::ThroughHierarchy::Builder.new(@klass).add_association(name, assoc)
end