Class: Graphoid::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/graphoid/operators/relation.rb

Direct Known Subclasses

BelongsTo, EmbedsMany, EmbedsOne, HasMany, HasOne, ManyToMany

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ Relation

Returns a new instance of Relation.



6
7
8
9
10
11
12
# File 'lib/graphoid/operators/relation.rb', line 6

def initialize(relation)
  @name = relation.name.to_s
  @camel_name = Utils.camelize(@name)
  @inverse_name = Graphoid.driver.inverse_name_of(relation)
  @klass = relation.class_name.constantize
  @type = Graphoid.driver.relation_type(relation)
end

Instance Attribute Details

#inverse_name ⇒ Object (readonly)

Returns the value of attribute inverse_name.



4
5
6
# File 'lib/graphoid/operators/relation.rb', line 4

def inverse_name
  @inverse_name
end

#klass ⇒ Object (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/graphoid/operators/relation.rb', line 4

def klass
  @klass
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/graphoid/operators/relation.rb', line 4

def name
  @name
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/graphoid/operators/relation.rb', line 4

def type
  @type
end

Class Method Details

.relations_of(model) ⇒ Object



63
64
65
66
67
# File 'lib/graphoid/operators/relation.rb', line 63

def relations_of(model)
  # return a list of relation objects
  # Graphoid.driver.relations_of(model).map { |_, relation| Relation.new(relation) }
  Graphoid.driver.relations_of(model)
end

Instance Method Details

#belongs? ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/graphoid/operators/relation.rb', line 34

def belongs?
  belongs_to? || embedded_in?
end

#create(_, _, _) ⇒ Object



49
50
# File 'lib/graphoid/operators/relation.rb', line 49

def create(_,_,_)
end

#embedded? ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/graphoid/operators/relation.rb', line 42

def embedded?
  embeds_one? || embeds_many? || embedded_in?
end

#many? ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
# File 'lib/graphoid/operators/relation.rb', line 29

def many?
  # TODO: "through" can be one or many, we only support many at the moment.
  has_many? || has_and_belongs_to_many? || through? || embeds_many?
end

#many_to_many? ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/graphoid/operators/relation.rb', line 38

def many_to_many?
  has_and_belongs_to_many? || through?
end

#one? ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/graphoid/operators/relation.rb', line 25

def one?
  belongs_to? || has_one? || embeds_one?
end

#precreate(_) ⇒ Object



46
47
# File 'lib/graphoid/operators/relation.rb', line 46

def precreate(_)
end

#relation? ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/graphoid/operators/relation.rb', line 21

def relation?
  true
end

#resolve(operation) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/graphoid/operators/relation.rb', line 52

def resolve(operation)
  if one?
    return operation.operand.exec(operation.scope, operation.value)
  end

  if many?
    return Graphoid.driver.relate_many(o.scope, o.operand, o.value, o.operator)
  end
end