Class: Discogs::Resource

Inherits:
Object
  • Object
show all
Includes:
ResourceMappings
Defined in:
lib/wrapper/resource.rb

Instance Method Summary collapse

Methods included from ResourceMappings

included

Constructor Details

#initialize(content) ⇒ Resource

Returns a new instance of Resource.



9
10
11
# File 'lib/wrapper/resource.rb', line 9

def initialize(content)
  @content = content
end

Instance Method Details

#build!(remove_resp = true) ⇒ Object

Builds the resource with it’s content.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/wrapper/resource.rb', line 18

def build!(remove_resp=true)
  document = REXML::Document.new(@content, :ignore_whitespace_nodes => :all)
  root_node = document.root

  # Ignore the <resp> element if necessary.
  if remove_resp and document.root.expanded_name == "resp"
    root_node = root_node[0]
  end

  set_accessors_from_attributes(root_node)

  # Traverse node children.
  root_node.each_element do |element|
    name = element.expanded_name.to_sym
    setter = (name.to_s + "=").to_sym

    singular = find_resource_for_name(name, :singular)
    plural = singular ? nil : find_resource_for_name(name, :plural)

    if !singular.nil?
      nested_object = singular.send(:new, element.to_s)
      self.send(setter, nested_object.build!)
    elsif !plural.nil?
      set_accessors_from_attributes(element)

      self.send(setter, [])
      element.each_element do |sub_element|
        nested_object = plural.send(:new, sub_element.to_s)
        self.send(name) << nested_object.build!
      end
    elsif self.respond_to? setter
      self.send(setter, element.text)
    end
  end
 
  self
end

#build_with_resp!Object



56
57
58
# File 'lib/wrapper/resource.rb', line 56

def build_with_resp!
  build!(false)
end

#original_contentObject



13
14
15
# File 'lib/wrapper/resource.rb', line 13

def original_content
  @content
end