Class: LazyXmlModel::CollectionProxy

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/lazy_xml_model/collection_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(association_name, xml_parent_element, options = {}) ⇒ CollectionProxy

Returns a new instance of CollectionProxy.



10
11
12
13
14
# File 'lib/lazy_xml_model/collection_proxy.rb', line 10

def initialize(association_name, xml_parent_element, options = {})
  @association_name = association_name
  @xml_parent_element = xml_parent_element
  @options = options
end

Instance Attribute Details

#association_nameObject (readonly)

Returns the value of attribute association_name.



6
7
8
# File 'lib/lazy_xml_model/collection_proxy.rb', line 6

def association_name
  @association_name
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/lazy_xml_model/collection_proxy.rb', line 6

def options
  @options
end

#xml_parent_elementObject (readonly)

Returns the value of attribute xml_parent_element.



6
7
8
# File 'lib/lazy_xml_model/collection_proxy.rb', line 6

def xml_parent_element
  @xml_parent_element
end

Instance Method Details

#<<(item) ⇒ Object Also known as: push



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/lazy_xml_model/collection_proxy.rb', line 16

def <<(item)
  item.xml_document = nil
  item.xml_parent_element = xml_parent_element

  if collection.any?
    collection.last.xml_element.add_next_sibling(item.xml_element)
  else
    xml_parent_element.add_child(item.xml_element)
  end

  @collection << item
end

#[]=(index, new_item) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/lazy_xml_model/collection_proxy.rb', line 30

def []=(index, new_item)
  original_item = self[index]

  new_item.xml_document = nil
  new_item.xml_parent_element = xml_parent_element

  # 1. Replace the xml with new_item.to_xml
  original_item.xml_element.replace(new_item.xml_element)

  # 2. Replace the original object with the new object
  @collection[index] = new_item
end

#attributes=(attributes) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lazy_xml_model/collection_proxy.rb', line 74

def attributes=(attributes)
  indexes_to_delete = []

  # 1. Update the existing items and create new ones
  attributes.each do |i, object_params|
    i = i.to_i
    object_params = object_params.with_indifferent_access

    if self[i].present?
      self[i].assign_attributes(object_params.except(:_destroy)) # Update the object
    else
      build(object_params.except(:_destroy)) # Build the object
    end

    # Keep track of which items will be deleted
    indexes_to_delete << i if [true, 1, '1'].include?(object_params[:_destroy])
  end

  # 2. Delete any items marked for deletion, must come after the first step and
  #    must be in reverse order
  indexes_to_delete.sort.reverse.each do |i|
    delete(self[i])
  end
end

#build(params = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/lazy_xml_model/collection_proxy.rb', line 62

def build(params = {})
  new_object =
    begin
      klass.new(params)
    # Incase the class does not include ActiveModel::AttributeAssignment
    rescue ArgumentError
      klass.new
    end

  self << new_object
end

#concat(item) ⇒ Object

TODO:

Raises:

  • (NotImplementedError)


44
45
46
# File 'lib/lazy_xml_model/collection_proxy.rb', line 44

def concat(item)
  raise NotImplementedError
end

#delete(item) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/lazy_xml_model/collection_proxy.rb', line 48

def delete(item)
  # Delete the object thats wrapping this xml element
  @collection.delete(item)

  # Delete from the xml document
  item.xml_element.remove
end

#delete_allObject Also known as: clear



56
57
58
59
# File 'lib/lazy_xml_model/collection_proxy.rb', line 56

def delete_all
  @collection = []
  xml_elements.each(&:remove)
end