Class: Ecircle::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ecircle/base.rb

Direct Known Subclasses

Group, Member, Message, User

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



13
14
15
# File 'lib/ecircle/base.rb', line 13

def initialize
  @id, @all_fields, @named_attrs = "", {}, {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Handle all assignments, everything else is propagated to super.



30
31
32
33
34
35
36
37
38
# File 'lib/ecircle/base.rb', line 30

def method_missing(method, *args, &block)
  case method.to_s
  when /\[\]=/ then super
  when /(.+)=/
    @all_fields[$1.to_sym] = args.first
  else
    super
  end
end

Instance Attribute Details

#all_fieldsObject (readonly)

Returns the value of attribute all_fields.



3
4
5
# File 'lib/ecircle/base.rb', line 3

def all_fields
  @all_fields
end

#idObject

Returns the value of attribute id.



3
4
5
# File 'lib/ecircle/base.rb', line 3

def id
  @id
end

#named_attrsObject (readonly)

Returns the value of attribute named_attrs.



3
4
5
# File 'lib/ecircle/base.rb', line 3

def named_attrs
  @named_attrs
end

Instance Method Details

#[](name) ⇒ Object



9
10
11
# File 'lib/ecircle/base.rb', line 9

def [](name)
  @all_fields[name.to_sym]
end

#init_with_xml(element_name, xml_string) ⇒ Object



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

def init_with_xml(element_name, xml_string)
  n = Nokogiri.parse(xml_string)
  @id = n.xpath("#{element_name}/@id" ).to_s
  @all_fields = Hash[ n.xpath("//#{element_name}/*").collect do |a|
                        [a.name.to_sym, a.children.first.to_s]
                      end ]
  @named_attrs = Hash[ n.xpath("#{element_name}/namedattr").collect do |a|
                         [a.attributes["name"].value,
                          a.children.empty? ? "" : a.children.first.to_s]
                       end ]
end