Permalink
Browse files

Fixed the :as option for specifying name aliases.

  • Loading branch information...
1 parent f59e5eb commit e8b3b85e3c9e76f723abe1ba03a5de7b77dd75eb @tylerhunt committed May 7, 2009
Showing with 8 additions and 9 deletions.
  1. +8 −9 lib/relief/element.rb
View
@@ -5,16 +5,16 @@ class Element
def initialize(name, options, &block)
@name = name
@options = options
- @children = {}
+ @children = []
instance_eval(&block) if block_given?
end
def parse(document)
- @children.inject({}) do |values, child|
- name, element = child
+ @children.inject({}) do |values, element|
+ key = element.options[:as] || element.name
- values[name] = begin
+ values[key] = begin
target = (document / element.xpath)
parse_node = lambda { |target|
@@ -34,8 +34,7 @@ def parse(document)
def element(name, options={}, &block)
options[:xpath] ||= name if name =~ %r([/.])
- name = options[:as].to_sym if options.has_key?(:as)
- @children[name] ||= self.class.new(name, options, &block)
+ @children << self.class.new(name, options, &block)
end
def elements(name, options={}, &block)
@@ -49,12 +48,12 @@ def attribute(name, options={}, &block)
def xpath
if options.has_key?(:xpath)
options[:xpath]
- elsif @children.empty?
+ elsif @children.any?
+ name.to_s
+ else
attribute = @options[:attribute]
attribute = name if attribute == true
!attribute ? "#{name}/text()" : "@#{attribute}"
- else
- name.to_s
end
end
end

0 comments on commit e8b3b85

Please sign in to comment.