Class: CSL::TextNode

Inherits:
Node show all
Defined in:
lib/csl/node.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#attributes

Attributes included from Treelike

#children, #nodename, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Node

#attribute?, #attributes?, #attributes_for, constantize, create_attributes, #custom_attributes, #deep_copy, #default_attribute?, default_attributes, #default_attributes, #each, #exact_match?, #format_page_ranges?, #formatting_options, #has_attributes?, #has_default_attributes?, #has_language?, hide_default_attributes!, hide_default_attributes?, match?, #match?, matches?, #merge!, #page_range_format, parse, parse!, #quotes?, #reverse_merge!, #save_to, show_default_attributes!, #strip_periods?, types

Methods included from Extensions::Nesting

#nesting

Methods included from PrettyPrinter

#pretty_print, #to_xml

Methods included from Treelike

#<<, #add_child, #add_children, #ancestors, #closest, #delete_child, #delete_children, #depth, #descendants, #each_ancestor, #each_child, #each_descendant, #each_sibling, #find_child, #find_children, #has_children?, #root, #root?, #siblings, #unlink

Constructor Details

#initialize(argument = '') ⇒ TextNode

TextNodes quack like a string. def_delegators :to_s, *String.instance_methods(false).reject do |m|

m.to_s =~ /^\W|!$|(?:^(?:hash|eql?|to_s|length|size|inspect)$)/

end

String.instance_methods(false).select { |m| m.to_s =~ /!$/ }.each do |m|

define_method(m) do
  content.send(m) if content.respond_to?(m)
end

end



575
576
577
578
579
580
581
582
583
584
585
586
# File 'lib/csl/node.rb', line 575

def initialize(argument = '')
  case
  when argument.respond_to?(:each_pair)
    super
  when argument.respond_to?(:to_s)
    super({})
    @text = argument.to_s
    yield self if block_given?
  else
    raise ArgumentError, "failed to create text node from #{argument.inspect}"
  end
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



558
559
560
# File 'lib/csl/node.rb', line 558

def text
  @text
end

Class Method Details

.create(name, attributes = {}, &block) ⇒ Object



549
550
551
552
553
554
555
# File 'lib/csl/node.rb', line 549

def create(name, attributes = {}, &block)
  klass = constantize(name)

  node = (klass || TextNode).new(attributes, &block)
  node.nodename = name
  node
end

Instance Method Details

#<=>(other) ⇒ Object



607
608
609
610
611
612
# File 'lib/csl/node.rb', line 607

def <=>(other)
  return nil unless other.is_a?(Node)
  return -1 unless other.textnode?

  [nodename, attributes, text] <=> [other.nodename, other.attributes, other.text]
end

#empty?Boolean

Returns:

  • (Boolean)


599
600
601
# File 'lib/csl/node.rb', line 599

def empty?
  text.nil? || text.empty?
end

#initialize_copy(other) ⇒ Object



588
589
590
591
# File 'lib/csl/node.rb', line 588

def initialize_copy(other)
  super
  @text = other.text
end

#inspectObject



614
615
616
# File 'lib/csl/node.rb', line 614

def inspect
  "#<#{[self.class.name, text.inspect, *attribute_assignments].join(' ')}>"
end

#tagsObject



603
604
605
# File 'lib/csl/node.rb', line 603

def tags
  ["<#{attribute_assignments.unshift(nodename).join(' ')}>#{to_s}</#{nodename}>"]
end

#textnode?Boolean

Returns:

  • (Boolean)


593
594
595
# File 'lib/csl/node.rb', line 593

def textnode?
  true
end

#to_sObject



560
561
562
# File 'lib/csl/node.rb', line 560

def to_s
  CSL.encode_xml_text text.to_s.strip
end