Class: Muxml::Base
- Inherits:
-
Object
- Object
- Muxml::Base
- Defined in:
- lib/muxml/base.rb,
lib/muxml/relation.rb
Defined Under Namespace
Modules: Relation
Instance Attribute Summary collapse
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Class Method Summary collapse
- .has_many(relation, options) ⇒ Object
- .has_one(relation, options) ⇒ Object
- .xml_document(file, root) ⇒ Object
Instance Method Summary collapse
-
#initialize(document = nil, element = nil) ⇒ Base
constructor
A new instance of Base.
-
#method_missing(sym, *args, &block) ⇒ Object
for call attributes of xml just prefix with attribute_.
Constructor Details
#initialize(document = nil, element = nil) ⇒ Base
Returns a new instance of Base.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/muxml/base.rb', line 9 def initialize(document = nil, element = nil) if @@xml_files.has_key?(self.class) document = @@xml_files[self.class][:file].to_s element = @@xml_files[self.class][:root] end case document when String xml = REXML::Document.new File.new(document) when REXML::Element xml = document else raise ArgumentError, 'invalid element' end unless element.nil? @xml = xml.elements[element] else @xml = xml end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Object
for call attributes of xml just prefix with attribute_
32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/muxml/base.rb', line 32 def method_missing(sym, *args, &block) if sym.to_s.start_with?('attribute_') attribute = sym.to_s.sub(/^attribute_/, '') return @xml.attributes[attribute] elsif @@relations.has_key?(self.class) if @@relations[self.class][:many].has_key?(sym) return Relation::Many.new(self, sym, @@relations[self.class][:many][sym].dup) elsif @@relations[self.class][:one].has_key?(sym) return Relation::One.new(self, sym, @@relations[self.class][:one][sym].dup) end end raise NoMethodError, "undefined method `#{sym}' for #{self}" end |
Instance Attribute Details
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
5 6 7 |
# File 'lib/muxml/base.rb', line 5 def xml @xml end |
Class Method Details
.has_many(relation, options) ⇒ Object
51 52 53 54 |
# File 'lib/muxml/base.rb', line 51 def has_many(relation, ) @@relations[self] ||= {many: {}, one: {} } @@relations[self][:many][relation] = end |
.has_one(relation, options) ⇒ Object
56 57 58 59 |
# File 'lib/muxml/base.rb', line 56 def has_one(relation, ) @@relations[self] ||= {many: {}, one: {} } @@relations[self][:one][relation] = end |