lib/csl/node.rb in csl-1.0.0.pre5 vs lib/csl/node.rb in csl-1.0.0.pre6
- old
+ new
@@ -66,10 +66,32 @@
end
end
private
+ def has_language
+ attr_accessor :language
+
+ define_method :has_language? do
+ !language.nil?
+ end
+
+ public :language, :language=, :has_language?
+
+ alias_method :original_attribute_assignments, :attribute_assignments
+
+ define_method :attribute_assignments do
+ if has_language?
+ original_attribute_assignments.unshift('xml:lang="%s"' % language)
+ else
+ original_attribute_assignments
+ end
+ end
+
+ private :original_attribute_assignments, :attribute_assignments
+ end
+
def attr_defaults(attributes)
@default_attributes = attributes
end
# Creates a new Struct for the passed-in attributes. Node instances
@@ -191,10 +213,14 @@
# false otherwise.
def has_attributes?
!attributes.empty?
end
+ def has_language?
+ false
+ end
+
def textnode?
false
end
alias has_text? textnode?
@@ -206,11 +232,11 @@
self
end
# Tests whether or not the Name matches the passed-in node name and
# attribute conditions; if a Hash is passed as a single argument,
- # it is taken as the conditions parameter (the name parameter is
+ # it is taken as the conditions parameter (the name parameter
# automatically matches in this case).
#
# Whether or not the arguments match the node is determined as
# follows:
#
@@ -223,10 +249,11 @@
# no other attributes than specified by the conditions, {#exact_match?}
# should be used instead.
#
# @see #exact_match?
#
+ # If the optional
# @param name [String,Regexp] must match the nodename
# @param conditions [Hash] the conditions
#
# @return [Boolean] whether or not the query matches the node
def match?(name = nodename, conditions = {})
@@ -274,10 +301,25 @@
condition === value
end
end
alias matches_exactly? exact_match?
+ # @option filter [Array] a list of attribute names
+ # @return [Hash] the node's attributes matching the filter
+ def attributes_for(*filter)
+ filter.flatten!
+
+ Hash[map { |name, value|
+ !value.nil? && filter.include?(name) ? [name, value.to_s] : nil
+ }.compact]
+ end
+
+ # @return [Hash] the node's formatting options
+ def formatting_options
+ attributes_for Schema.attr(:formatting)
+ end
+
def <=>(other)
[nodename, attributes, children] <=> [other.nodename, other.attributes, other.children]
rescue
nil
end
@@ -309,10 +351,10 @@
private
def attribute_assignments
each_pair.map { |name, value|
- value.nil? ? nil: [name, value.to_s.inspect].join('=')
+ value.nil? ? nil : [name, value.to_s.inspect].join('=')
}.compact
end
def match_conditions_for(name, conditions)
case name
\ No newline at end of file