Class: Socialcastr::SAX::ActiveResource

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/socialcastr/sax/active_resource.rb

Constant Summary collapse

HASH =

‘h’

104
ARRAY =

‘a’

97
INTEGER =

‘i’

105
BOOLEAN =

‘b’

98
STRING =

‘s’

115

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActiveResource

Returns a new instance of ActiveResource.



30
31
32
33
34
# File 'lib/socialcastr/sax/active_resource.rb', line 30

def initialize
  @types = []
  @values = []
  @data= nil
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



28
29
30
# File 'lib/socialcastr/sax/active_resource.rb', line 28

def data
  @data
end

Instance Method Details

#add_to_container(name, element) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/socialcastr/sax/active_resource.rb', line 86

def add_to_container(name, element)
  if container_type == ARRAY
    container_value ? @values[-1].push(element) : @values[-1] = [element]
  else # Hash
    container_value ? @values[-1][name]=element : @values[-1] = { name => element }
  end
end

#cdata_block(s) ⇒ Object



36
37
38
# File 'lib/socialcastr/sax/active_resource.rb', line 36

def cdata_block(s)
  characters(s)
end

#characters(string) ⇒ Object



101
102
103
104
# File 'lib/socialcastr/sax/active_resource.rb', line 101

def characters string
  return if nil_element? || (string.all_spaces? && (container_type != STRING || container_value.nil?))
  update_string_element(string)
end

#container_typeObject



40
41
42
# File 'lib/socialcastr/sax/active_resource.rb', line 40

def container_type
  @types[-1]
end

#container_valueObject



44
45
46
# File 'lib/socialcastr/sax/active_resource.rb', line 44

def container_value
  @values[-1]
end

#end_element(name) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/socialcastr/sax/active_resource.rb', line 106

def end_element name
  return end_nil_element if nil_element?

  (value, type) = pop_element
  case type
  when HASH
    element = Socialcastr.const_get(Socialcastr.to_class_name(name), false).from_hash(value || {})
  when INTEGER
    element = value.to_i
  when BOOLEAN
    element = Boolean.new(value)
  when ARRAY
    element = value || []
  else
    element = value
  end

  if container_type
    add_to_container(name, element)
  else # Root Node
    self.data = element
  end
end

#end_nil_elementObject



62
63
64
# File 'lib/socialcastr/sax/active_resource.rb', line 62

def end_nil_element
  @nil = false
end

#nil_element!Object



54
55
56
# File 'lib/socialcastr/sax/active_resource.rb', line 54

def nil_element!
  @nil = true 
end

#nil_element?Boolean

Returns:



58
59
60
# File 'lib/socialcastr/sax/active_resource.rb', line 58

def nil_element?
  @nil
end

#parse_attrs_and_get_type(attribute_array = []) ⇒ Object



66
67
68
69
70
# File 'lib/socialcastr/sax/active_resource.rb', line 66

def parse_attrs_and_get_type(attribute_array=[])
  attributes = attribute_array.to_hash
  return nil_element! if attributes["nil"]
  attributes["type"] ? attributes["type"][0] : HASH
end

#pop_elementObject



77
78
79
# File 'lib/socialcastr/sax/active_resource.rb', line 77

def pop_element
  return [@values.pop, @types.pop]
end

#push_element(type) ⇒ Object



72
73
74
75
# File 'lib/socialcastr/sax/active_resource.rb', line 72

def push_element(type)
  @types.push type
  @values.push nil
end

#start_element(name, attrs = []) ⇒ Object



94
95
96
97
98
99
# File 'lib/socialcastr/sax/active_resource.rb', line 94

def start_element name, attrs = []
  return nil_element! if name.contains_dot? # [FIXME] we can't evaluate strings inside elements like <html.title>
  type = parse_attrs_and_get_type(attrs)
  return if nil_element?
  push_element(type)
end

#update_string_element(string) ⇒ Object



81
82
83
84
# File 'lib/socialcastr/sax/active_resource.rb', line 81

def update_string_element(string)
  @types[-1]  = STRING if container_type == HASH
  @values[-1] = container_value ? container_value + string : string 
end