Class: Ecircle::Base
- Inherits:
-
Object
- Object
- Ecircle::Base
- Defined in:
- lib/ecircle/base.rb
Instance Attribute Summary collapse
-
#all_fields ⇒ Object
readonly
Returns the value of attribute all_fields.
-
#id ⇒ Object
Returns the value of attribute id.
-
#named_attrs ⇒ Object
readonly
Returns the value of attribute named_attrs.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #init_with_xml(element_name, xml_string) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
-
#method_missing(method, *args, &block) ⇒ Object
Handle all assignments, everything else is propagated to super.
Constructor Details
#initialize ⇒ Base
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_fields ⇒ Object (readonly)
Returns the value of attribute all_fields.
3 4 5 |
# File 'lib/ecircle/base.rb', line 3 def all_fields @all_fields end |
#id ⇒ Object
Returns the value of attribute id.
3 4 5 |
# File 'lib/ecircle/base.rb', line 3 def id @id end |
#named_attrs ⇒ Object (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 |