Class: Amfetamine::Relationship

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/amfetamine/relationship.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Relationship

Returns a new instance of Relationship.



7
8
9
10
11
12
13
# File 'lib/amfetamine/relationship.rb', line 7

def initialize(opts)
  @type             = opts[:type]
  @on_resource_name = opts[:on_resource_name]                          # Target resource name
  @on_class_name    = opts.fetch(:on_class_name) { @on_resource_name } # Target class name
  @from             = opts[:from]                                      # Receiving object
  @foreign_key      = opts.fetch(:foreign_key) { build_foreign_key }   # Foreign key
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



5
6
7
# File 'lib/amfetamine/relationship.rb', line 5

def from
  @from
end

#on_class_nameObject (readonly)

Returns the value of attribute on_class_name.



5
6
7
# File 'lib/amfetamine/relationship.rb', line 5

def on_class_name
  @on_class_name
end

#on_resource_nameObject (readonly)

Returns the value of attribute on_resource_name.



5
6
7
# File 'lib/amfetamine/relationship.rb', line 5

def on_resource_name
  @on_resource_name
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/amfetamine/relationship.rb', line 5

def type
  @type
end

Instance Method Details

#<<(other) ⇒ Object



15
16
17
18
19
20
# File 'lib/amfetamine/relationship.rb', line 15

def << (other)
  other.send("#{@foreign_key}=", @from.id)
  other.instance_variable_set("@#{from_singular_name}", Amfetamine::Relationship.new(on_resource_name: @from, on_class_name: @from, foreign_key: @foreign_key, from: other, type: :belongs_to))
  @children ||= [] # No need to do a request here, but it needs to be an array if it isn't yet.
  @children << other
end

#all(opts = {}) ⇒ Object

Delegates the all method to child class with a nested path set



87
88
89
# File 'lib/amfetamine/relationship.rb', line 87

def all(opts={})
  on_class.all({ :nested_path => rest_path }.merge(opts))
end

#eachObject



82
83
84
# File 'lib/amfetamine/relationship.rb', line 82

def each
  all.each { |c| yield c }
end

#find(id, opts = {}) ⇒ Object

Delegates the find method to child class with a nested path set



92
93
94
# File 'lib/amfetamine/relationship.rb', line 92

def find(id, opts={})
  on_class.find(id, {:nested_path => find_path(id)}.merge(opts))
end

#find_path(id) ⇒ Object



64
65
66
# File 'lib/amfetamine/relationship.rb', line 64

def find_path(id)
  on_class.find_path(id, :relationship => self)
end

#from_idObject

Id of the receiving object



40
41
42
# File 'lib/amfetamine/relationship.rb', line 40

def from_id
  @from.id
end

#from_plural_nameObject



44
45
46
# File 'lib/amfetamine/relationship.rb', line 44

def from_plural_name
  @from.class.name.to_s.downcase.pluralize
end

#from_singular_nameObject



48
49
50
# File 'lib/amfetamine/relationship.rb', line 48

def from_singular_name
  @from.class.name.to_s.downcase
end

#full_pathObject



72
73
74
75
76
77
78
79
80
# File 'lib/amfetamine/relationship.rb', line 72

def full_path
  if @type == :has_many
    raise InvalidPath if from_id == nil
    "#{from_plural_name}/#{from_id}/#{on_plural_name}"
  elsif @type == :belongs_to
    raise InvalidPath if parent_id == nil
    "#{on_plural_name}/#{parent_id}/#{from_plural_name}"
  end
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
# File 'lib/amfetamine/relationship.rb', line 96

def include?(other)
  self.all
  @children.include?(other)
end

#on_classObject



22
23
24
25
26
27
28
# File 'lib/amfetamine/relationship.rb', line 22

def on_class
  if @on_class_name.is_a?(Symbol) or @on_class_name.is_a?(String)
    Amfetamine.parent.const_get(@on_class_name.to_s.singularize.split('/').map { |s| s.split('_').map(&:capitalize).join }.join('::'))
  else
    @on_class_name.class
  end
end

#on_plural_nameObject



52
53
54
55
56
57
58
# File 'lib/amfetamine/relationship.rb', line 52

def on_plural_name
  if @on_resource_name.is_a?(Symbol) or @on_resource_name.is_a?(String)
    @on_resource_name.to_s.pluralize
  else
    @on_resource_name.class.name.to_s.pluralize.downcase
  end
end

#parent_idObject

Id of object this relationship references



31
32
33
34
35
36
37
# File 'lib/amfetamine/relationship.rb', line 31

def parent_id
  if @on_class_name.is_a?(Symbol) or @on_class_name.is_a?(String)
    @from.send(@foreign_key) if @type == :belongs_to
  else
    @on_class_name.id
  end
end

#rest_pathObject



60
61
62
# File 'lib/amfetamine/relationship.rb', line 60

def rest_path
  on_class.rest_path(:relationship => self)
end

#singular_pathObject



68
69
70
# File 'lib/amfetamine/relationship.rb', line 68

def singular_path
  find_path(@from.id)
end