Class: CSL::Node
- Extended by:
- Forwardable
- Includes:
- PrettyPrinter, Treelike, Comparable, Enumerable
- Defined in:
- lib/csl/node.rb
Direct Known Subclasses
Info, Info::Author, Info::Category, Info::Contributor, Info::Link, Info::Translator, Locale, Locale::Date, Locale::DatePart, Locale::StyleOptions, Locale::Term, Locale::Terms, Sort, Sort::Key, Style, Style::Bibliography, Style::Choose, Style::Choose::Block, Style::Citation, Style::Date, Style::DatePart, Style::EtAl, Style::Group, Style::Label, Style::Layout, Style::Macro, Style::Name, Style::NamePart, Style::Names, Style::Number, Style::Substitute, Style::Text, TextNode
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
Attributes included from Treelike
Class Method Summary collapse
- .constantize(name) ⇒ Object
-
.create(name, attributes = {}, &block) ⇒ Object
Returns a new node with the passed in name and attributes.
- .create_attributes(attributes) ⇒ Object
- .default_attributes ⇒ Object
- .hide_default_attributes! ⇒ Object
- .hide_default_attributes? ⇒ Boolean
-
.match?(name_pattern) ⇒ Boolean
Whether or not the node’s name matches the passed-in name pattern.
-
.matches? ⇒ Boolean
Whether or not the node’s name matches the passed-in name pattern.
- .parse(data) ⇒ Object
- .parse!(data) ⇒ Object
- .show_default_attributes! ⇒ Object
- .types ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#attribute?(name) ⇒ Boolean
Returns true if the node contains an attribute with the passed-in name; false otherwise.
-
#attributes?(*names) ⇒ Boolean
True if the node contains attributes for all passed-in names; false otherwise.
-
#attributes_for(*filter) ⇒ Hash
The node’s attributes matching the filter.
-
#custom_attributes ⇒ Hash
The attributes currently not set to their default values.
- #deep_copy ⇒ Object
-
#default_attribute?(name) ⇒ Boolean
Whether or not key is set to the default value.
-
#default_attributes ⇒ Hash
The attributes currently set to their default values.
-
#each ⇒ Object
(also: #each_pair)
Iterates through the Node’s attributes.
-
#exact_match?(name = nodename, conditions = {}) ⇒ Boolean
(also: #matches_exactly?)
Tests whether or not the Name matches the passed-in node name and attribute conditions exactly; if a Hash is passed as a single argument, it is taken as the conditions parameter (the name parameter is automatically matches in this case).
-
#formatting_options ⇒ Hash
The node’s formatting options.
-
#has_attributes? ⇒ Boolean
Returns true if the node contains any attributes (ignores nil values); false otherwise.
-
#has_default_attributes? ⇒ Boolean
(also: #has_defaults?)
Whether or not the node has default attributes.
- #has_language? ⇒ Boolean
-
#initialize(attributes = {}) {|_self| ... } ⇒ Node
constructor
A new instance of Node.
- #initialize_copy(other) ⇒ Object
- #inspect ⇒ Object
-
#match?(name = nodename, conditions = {}) ⇒ Boolean
(also: #matches?)
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 automatically matches in this case).
- #save_to(path, options = {}) ⇒ Object
-
#tags ⇒ Object
Returns the node’ XML tags (including attribute assignments) as an array of strings.
- #textnode? ⇒ Boolean (also: #has_text?)
Methods included from PrettyPrinter
Methods included from Treelike
#<<, #add_child, #add_children, #ancestors, #delete_child, #delete_children, #depth, #descendants, #each_ancestor, #each_child, #each_descendant, #each_sibling, #empty?, #find_child, #find_children, #has_children?, #root, #root?, #siblings, #unlink
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ Node
Returns a new instance of Node.
225 226 227 228 229 230 |
# File 'lib/csl/node.rb', line 225 def initialize(attributes = {}) @attributes = self.class.create_attributes(attributes) @children = self.class.create_children yield self if block_given? end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
221 222 223 |
# File 'lib/csl/node.rb', line 221 def attributes @attributes end |
Class Method Details
.constantize(name) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/csl/node.rb', line 43 def constantize(name) pattern = /:#{name.to_s.tr('-', '')}$/i klass = types.detect { |t| t.matches?(pattern) } case when !klass.nil? klass when nesting[-2].respond_to?(:constantize) nesting[-2].constantize(name) else nil end end |
.create(name, attributes = {}, &block) ⇒ Object
Returns a new node with the passed in name and attributes.
65 66 67 68 69 70 71 |
# File 'lib/csl/node.rb', line 65 def create(name, attributes = {}, &block) klass = constantize(name) node = (klass || Node).new(attributes, &block) node.nodename = name node end |
.create_attributes(attributes) ⇒ Object
73 74 75 76 77 78 79 |
# File 'lib/csl/node.rb', line 73 def create_attributes(attributes) if const?(:Attributes) const_get(:Attributes).new(default_attributes.merge(attributes)) else default_attributes.merge(attributes) end end |
.default_attributes ⇒ Object
27 28 29 |
# File 'lib/csl/node.rb', line 27 def default_attributes @default_attributes ||= {} end |
.hide_default_attributes! ⇒ Object
35 36 37 |
# File 'lib/csl/node.rb', line 35 def hide_default_attributes! @show_default_attributes = false end |
.hide_default_attributes? ⇒ Boolean
31 32 33 |
# File 'lib/csl/node.rb', line 31 def hide_default_attributes? !@show_default_attributes end |
.match?(name_pattern) ⇒ Boolean
Returns whether or not the node’s name matches the passed-in name pattern.
59 60 61 |
# File 'lib/csl/node.rb', line 59 def match?(name_pattern) name_pattern === name end |
.matches? ⇒ Boolean
Returns whether or not the node’s name matches the passed-in name pattern.
62 63 64 |
# File 'lib/csl/node.rb', line 62 def match?(name_pattern) name_pattern === name end |
.parse(data) ⇒ Object
81 82 83 84 85 |
# File 'lib/csl/node.rb', line 81 def parse(data) parse!(data) rescue nil end |
.parse!(data) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/csl/node.rb', line 87 def parse!(data) node = CSL.parse!(data, self) raise ParseError, "root node not #{self.name}: #{node.inspect}" unless node.class == self || Node.equal?(self) node end |
.show_default_attributes! ⇒ Object
39 40 41 |
# File 'lib/csl/node.rb', line 39 def show_default_attributes! @show_default_attributes = true end |
.types ⇒ Object
23 24 25 |
# File 'lib/csl/node.rb', line 23 def types @types ||= Set.new end |
Instance Method Details
#<=>(other) ⇒ Object
425 426 427 428 429 |
# File 'lib/csl/node.rb', line 425 def <=>(other) [nodename, attributes, children] <=> [other.nodename, other.attributes, other.children] rescue nil end |
#attribute?(name) ⇒ Boolean
Returns true if the node contains an attribute with the passed-in name; false otherwise.
292 293 294 |
# File 'lib/csl/node.rb', line 292 def attribute?(name) attributes.fetch(name, false) end |
#attributes?(*names) ⇒ Boolean
Returns true if the node contains attributes for all passed-in names; false otherwise.
299 300 301 302 303 |
# File 'lib/csl/node.rb', line 299 def attributes?(*names) names.flatten(1).all? do |name| attribute?(name) end end |
#attributes_for(*filter) ⇒ Hash
Returns the node’s attributes matching the filter.
412 413 414 415 416 417 418 |
# File 'lib/csl/node.rb', line 412 def attributes_for(*filter) filter.flatten! Hash[map { |name, value| !value.nil? && filter.include?(name) ? [name, value.to_s] : nil }.compact] end |
#custom_attributes ⇒ Hash
Returns the attributes currently not set to their default values.
284 285 286 287 288 |
# File 'lib/csl/node.rb', line 284 def custom_attributes attributes.to_hash.reject do |name, _| default_attribute?(name) end end |
#deep_copy ⇒ Object
239 240 241 242 243 244 245 246 247 |
# File 'lib/csl/node.rb', line 239 def deep_copy copy = dup each_child do |child| copy.add_child child.deep_copy end copy end |
#default_attribute?(name) ⇒ Boolean
Returns whether or not key is set to the default value.
268 269 270 271 272 273 274 |
# File 'lib/csl/node.rb', line 268 def default_attribute?(name) defaults = self.class.default_attributes name, value = name.to_sym, attributes.fetch(name) return false unless !value.nil? || defaults.key?(name) defaults[name] == value end |
#default_attributes ⇒ Hash
Returns the attributes currently set to their default values.
277 278 279 280 281 |
# File 'lib/csl/node.rb', line 277 def default_attributes attributes.to_hash.select do |name, _| default_attribute?(name) end end |
#each ⇒ Object Also known as: each_pair
Iterates through the Node’s attributes
256 257 258 259 260 261 262 263 |
# File 'lib/csl/node.rb', line 256 def each if block_given? attributes.each_pair(&Proc.new) self else to_enum end end |
#exact_match?(name = nodename, conditions = {}) ⇒ Boolean Also known as: matches_exactly?
Tests whether or not the Name matches the passed-in node name and attribute conditions exactly; if a Hash is passed as a single argument, it is taken as the conditions parameter (the name parameter is automatically matches in this case).
Whether or not the arguments match the node is determined as follows:
-
The name must match Treelike#nodename
-
All attribute name/value pairs of the node must match the corresponding pairs in the passed-in Hash
Note that all node attributes are used by this method – if you want to match only a subset of attributes #match? should be used instead.
397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/csl/node.rb', line 397 def exact_match?(name = nodename, conditions = {}) name, conditions = match_conditions_for(name, conditions) return false unless name === nodename return true if conditions.empty? conditions.values_at(*attributes.keys).zip( attributes.values_at(*attributes.keys)).all? do |condition, value| condition === value end end |
#formatting_options ⇒ Hash
Returns the node’s formatting options.
421 422 423 |
# File 'lib/csl/node.rb', line 421 def attributes_for Schema.attr(:formatting) end |
#has_attributes? ⇒ Boolean
Returns true if the node contains any attributes (ignores nil values); false otherwise.
307 308 309 |
# File 'lib/csl/node.rb', line 307 def has_attributes? !attributes.empty? end |
#has_default_attributes? ⇒ Boolean Also known as: has_defaults?
Returns whether or not the node has default attributes.
250 251 252 |
# File 'lib/csl/node.rb', line 250 def has_default_attributes? !default_attributes.empty? end |
#has_language? ⇒ Boolean
311 312 313 |
# File 'lib/csl/node.rb', line 311 def has_language? false end |
#initialize_copy(other) ⇒ Object
232 233 234 235 236 237 |
# File 'lib/csl/node.rb', line 232 def initialize_copy(other) super @attributes = self.class.create_attributes(other.attributes) @children = self.class.create_children @parent, @ancestors, @descendants, @siblings, @root, @depth = nil end |
#inspect ⇒ Object
449 450 451 |
# File 'lib/csl/node.rb', line 449 def inspect "#<#{[self.class.name, *attribute_assignments].join(' ')} children=[#{children.count}]>" end |
#match?(name = nodename, conditions = {}) ⇒ Boolean Also known as: matches?
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 automatically matches in this case).
Whether or not the arguments match the node is determined as follows:
-
The name must match Treelike#nodename
-
All attribute name/value pairs passed as conditions must match the corresponding attributes of the node
Note that only attributes present in the passed-in conditions influence the match – if you want to match only nodes that contain no other attributes than specified by the conditions, #exact_match? should be used instead.
357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/csl/node.rb', line 357 def match?(name = nodename, conditions = {}) name, conditions = match_conditions_for(name, conditions) return false unless name === nodename return true if conditions.empty? conditions.values.zip( attributes.values_at(*conditions.keys)).all? do |condition, value| condition === value end end |
#save_to(path, options = {}) ⇒ Object
320 321 322 323 324 325 326 |
# File 'lib/csl/node.rb', line 320 def save_to(path, = {}) File.open(path, 'w:UTF-8') do |f| f << ([:compact] ? to_xml : pretty_print) end self end |
#tags ⇒ Object
Returns the node’ XML tags (including attribute assignments) as an array of strings.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
# File 'lib/csl/node.rb', line 433 def if has_children? = [] << "<#{[nodename, *attribute_assignments].join(' ')}>" << children.map { |node| node.respond_to?(:tags) ? node. : [node.to_s] }.flatten(1) << "</#{nodename}>" else ["<#{[nodename, *attribute_assignments].join(' ')}/>"] end end |
#textnode? ⇒ Boolean Also known as: has_text?
315 316 317 |
# File 'lib/csl/node.rb', line 315 def textnode? false end |