Class: Lockstep::RelationArray

Inherits:
Array
  • Object
show all
Defined in:
app/concepts/lockstep/relation_array.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, items, key, class_name) ⇒ RelationArray

Returns a new instance of RelationArray.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'app/concepts/lockstep/relation_array.rb', line 4

def initialize(delegate, items, key, class_name)
  super(items)
  self.delegate = delegate
  self.key = key
  self.class_name = class_name

  class_eval do
    define_method("#{key}_ids") do
      each.map { |item| item.id }
    end
  end
end

Instance Attribute Details

#class_nameObject

Returns the value of attribute class_name.



2
3
4
# File 'app/concepts/lockstep/relation_array.rb', line 2

def class_name
  @class_name
end

#delegateObject

Returns the value of attribute delegate.



2
3
4
# File 'app/concepts/lockstep/relation_array.rb', line 2

def delegate
  @delegate
end

#keyObject

Returns the value of attribute key.



2
3
4
# File 'app/concepts/lockstep/relation_array.rb', line 2

def key
  @key
end

Class Method Details

.has_many_polymorphic_attributes(record, polymorphic_config) ⇒ Object

Polymorphic properties is used to further scope down the has_many association while querying or creating



43
44
45
46
47
48
# File 'app/concepts/lockstep/relation_array.rb', line 43

def self.has_many_polymorphic_attributes(record, polymorphic_config)
  return polymorphic_config if polymorphic_config.is_a?(Hash)
  return record.instance_eval record, &polymorphic_config if polymorphic_config.is_a?(Proc)

  nil
end

Instance Method Details

#<<(object) ⇒ Object

Raises:

  • (Exception)


50
51
52
53
54
55
56
57
58
59
60
# File 'app/concepts/lockstep/relation_array.rb', line 50

def <<(object)
  if object.is_a? Hash
    create(object)
    return object
  end

  raise Exception.new("This relation only stores objects of type #{class_name.to_s.constantize}") if !object.is_a? class_name.to_s.constantize

  super(object)
  object
end

#contains?(object) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
# File 'app/concepts/lockstep/relation_array.rb', line 78

def contains?(object)
  self.each { |item| return true if item.attributes == object.attributes }
  false
end

#create(attributes_hash = {}) ⇒ Object



17
18
19
20
21
22
# File 'app/concepts/lockstep/relation_array.rb', line 17

def create(attributes_hash = {})
  object = new(attributes_hash)
  return false unless object.save

  object
end

#delete(object) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'app/concepts/lockstep/relation_array.rb', line 66

def delete(object)
  if object.is_a? Hash
    object_id = object["objectId"]
  elsif object.is_a? String
    object_id = object
  else
    object_id = object.id
  end

  self.each { |item| super(item) if item.id == object_id }
end

#exists?(query) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
# File 'app/concepts/lockstep/relation_array.rb', line 83

def exists?(query)
  each do |item|
    conditions_met = true
    item.attributes.each do |key, value|
      conditions_met = false if query.has_key?(key) and query[key] != value
      break
    end
    return true if conditions_met
  end
  false
end

#find(id_to_find) ⇒ Object



95
96
97
98
99
# File 'app/concepts/lockstep/relation_array.rb', line 95

def find(id_to_find)
  each do |item|
    return item if id_to_find == item.id
  end
end

#new(attributes_hash = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/concepts/lockstep/relation_array.rb', line 24

def new(attributes_hash = {})
  # Assign the parent records primary_key to the foreign_key of the association
  relation_config = delegate.class.lockstep_has_many_relations[key]
  foreign_key = relation_config[:foreign_key]
  primary_key = relation_config[:primary_key]
  attributes_hash[foreign_key] = delegate.send(primary_key)
  if relation_config[:polymorphic]
    polymorphic_config = Lockstep::RelationArray.has_many_polymorphic_attributes(self, relation_config[:polymorphic])
    attributes_hash.merge!(polymorphic_config) if polymorphic_config.is_a?(Hash)
  end
  object = self.class_name.constantize.new(attributes_hash)
  self.push object

  object
end

#push(object) ⇒ Object



62
63
64
# File 'app/concepts/lockstep/relation_array.rb', line 62

def push(object)
  self << object
end