Class: Talius::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/talius.rb

Overview

Base class for Talius::Node::Tag and Talius::Node::Att.

Direct Known Subclasses

Att, Tag

Defined Under Namespace

Classes: Att, Tag

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Node

Initializes a new node. Accepts the string from the selector. Generally, you won’t have to instantiate a Talius::Node object yourself.



533
534
535
536
537
538
539
540
541
542
543
544
545
546
# File 'lib/talius.rb', line 533

def initialize(raw)
	tokens = raw.split('|', 2)
	
	# if one element
	if tokens.length == 1
		@namespace = nil
		@name = tokens[0]
	elsif tokens.length == 2
		@namespace = tokens[0]
		@name = tokens[1]
	else
		raise 'node-syntax-error: ' + raw.to_s
	end
end

Instance Attribute Details

#nameObject (readonly)

The name of the tag or attribute. Does not include the namespace.

raw = 'section, mml|dev'
selector = Talius.new(raw)
selector.rules[0].tags['section'].name # => "section"
selector.rules[1].tags['mml|dev'].name # => "dev"


579
580
581
# File 'lib/talius.rb', line 579

def name
  @name
end

#namespaceObject (readonly)

The node’s namespace. If a namespace is not explicitly given in the selector string then this property is nil.

raw = 'section, mml|dev'
selector = Talius.new(raw)
selector.rules[0].tags['section'].namespace # => nil
selector.rules[1].tags['mml|dev'].namespace # => "mml"


563
564
565
# File 'lib/talius.rb', line 563

def namespace
  @namespace
end

Instance Method Details

#full_nameObject

Returns the namespace (if there is one) and the name of the node. If there is a namespace then the namespace is followed by |.

raw = 'section, mml|dev'
selector = Talius.new(raw)
selector.rules[0].tags['section'].fullname # => "section"
selector.rules[1].tags['mml|dev'].fullname # => "mml|dev"


596
597
598
599
600
601
602
# File 'lib/talius.rb', line 596

def full_name
	if @namespace
		return @namespace + '|' + @name
	else
		return @name
	end
end

#inspectObject

Returns the stringification of the hash returned by to_h.

raw = 'section, mml|dev'
selector = Talius.new(raw)
selector.rules[0].tags['section'].inspect # => {"name"=>"section"}
selector.rules[1].tags['mml|dev'].inspect # => {"name"=>"dev", "namespace"=>"mml"}


656
657
658
# File 'lib/talius.rb', line 656

def inspect
	return to_h.inspect
end

#to_hObject

Returns a hash of the name and namespace (if there is a namespace).

raw = 'section, mml|dev'
selector = Talius.new(raw)
selector.rules[0].tags['section'].to_h # => {"name"=>"section"}
selector.rules[1].tags['mml|dev'].to_h # => {"name"=>"dev", "namespace"=>"mml"}


631
632
633
634
635
636
637
638
639
640
641
# File 'lib/talius.rb', line 631

def to_h
	rv = {}
	rv['name'] = @name
	
	# namespace
	if @namespace
		rv['namespace'] = @namespace
	end
	
	return rv
end

#to_sObject

Returns the results of full_name.



613
614
615
# File 'lib/talius.rb', line 613

def to_s
	return full_name
end