Class: SmugMugAPI::XMLStruct

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/smugmug/xmlstruct.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ XMLStruct

Returns a new instance of XMLStruct.



10
11
12
13
14
15
16
# File 'lib/smugmug/xmlstruct.rb', line 10

def initialize(xml)
  if xml.is_a?(REXML::Document)
    @xml = xml.root.elements[2]
  else
    @xml = xml
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/smugmug/xmlstruct.rb', line 70

def method_missing(*args)
  method = XMLStruct.camalize(args.shift)
  path = "#{method}"
  if node = @xml.elements[path]
    XMLStruct.new(node)
  else
    self[method]
  end
end

Class Method Details

.attrib_key(key) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/smugmug/xmlstruct.rb', line 18

def self.attrib_key(key)
  case key
  when "Id", "id", :id
    "id"
  when Fixnum
    key
  else
    key = XMLStruct.camalize(key)
    key = key.gsub(/ID$/i, "ID")
    key = key.gsub(/EXIF/i, "EXIF")
    key.gsub(/X(.?)Large/i) { "X#{$1}Large" }
  end
end

.camalize(str) ⇒ Object



80
81
82
# File 'lib/smugmug/xmlstruct.rb', line 80

def self.camalize(str)
  str.to_s.gsub(/(^|_)(.)/) { $2.upcase }
end

Instance Method Details

#[](attrib) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/smugmug/xmlstruct.rb', line 32

def [](attrib)
  if attrib.is_a?(Fixnum)
    # puts "Finding #{attrib} hmmm: #{@xml.elements[attrib]}"
    result = XMLStruct.new(@xml.elements[attrib])
    # puts "Found #{result}"
    result
  else
    # key = XMLStruct.attrib_key(attrib)
    @xml.attributes[XMLStruct.attrib_key(attrib)]
  end
end

#[]=(attrib, value) ⇒ Object



60
61
62
# File 'lib/smugmug/xmlstruct.rb', line 60

def []=(attrib,value)
  @xml.attributes[XMLStruct.attrib_key(attrib)] = value
end

#eachObject



64
65
66
67
68
# File 'lib/smugmug/xmlstruct.rb', line 64

def each
  @xml.elements.each do |xml|
    yield XMLStruct.new(xml)
  end
end

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/smugmug/xmlstruct.rb', line 52

def empty?
  size == 0
end

#firstObject



44
45
46
# File 'lib/smugmug/xmlstruct.rb', line 44

def first
  XMLStruct.new(@xml.elements[1])
end

#id(*args) ⇒ Object



6
7
8
# File 'lib/smugmug/xmlstruct.rb', line 6

def id(*args)
  method_missing(:id, *args)
end

#lastObject



56
57
58
# File 'lib/smugmug/xmlstruct.rb', line 56

def last
  XMLStruct.new(@xml.elements[@xml.elements.size])
end

#sizeObject



48
49
50
# File 'lib/smugmug/xmlstruct.rb', line 48

def size
  @xml.elements.size
end

#to_sObject



84
85
86
# File 'lib/smugmug/xmlstruct.rb', line 84

def to_s
  @xml.to_s
end