Class: ReaPack::Index::NamedNode

Inherits:
Object
  • Object
show all
Defined in:
lib/reapack/index/named_node.rb

Direct Known Subclasses

Category, Package, Version

Constant Summary collapse

NAME_ATTR =
'name'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node) ⇒ NamedNode



48
49
50
# File 'lib/reapack/index/named_node.rb', line 48

def initialize(node)
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



52
53
54
# File 'lib/reapack/index/named_node.rb', line 52

def node
  @node
end

Class Method Details

.create(name, parent) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/reapack/index/named_node.rb', line 36

def self.create(name, parent)
  node = Nokogiri::XML::Node.new tag, parent.document
  node[NAME_ATTR] = name
  node.parent = parent

  instance = new node
  instance.instance_variable_set :@is_new, true
  instance.instance_variable_set :@dirty, true

  instance
end

.fetch(name, parent, create) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/reapack/index/named_node.rb', line 24

def self.fetch(name, parent, create)
  return unless parent

  instance = find_one name, parent

  if create
    instance ||= self.create name, parent
  end

  instance
end

.find_all(parent) ⇒ Object



18
19
20
21
22
# File 'lib/reapack/index/named_node.rb', line 18

def self.find_all(parent)
  parent.element_children
    .select {|node| node.name == tag }
    .map {|node| self.new node }
end

.find_one(name, parent) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/reapack/index/named_node.rb', line 10

def self.find_one(name, parent)
  node = parent.element_children.find {|node|
    node.name == tag && node[NAME_ATTR] == name
  }

  self.new node if node
end

.tagObject



5
6
7
8
# File 'lib/reapack/index/named_node.rb', line 5

def self.tag;
  raise "@tag is unset" unless @tag
  @tag
end

Instance Method Details

#children(tag) ⇒ Object



69
70
71
# File 'lib/reapack/index/named_node.rb', line 69

def children(tag)
  @node.element_children.select {|node| node.name == tag }
end

#empty?Boolean



61
62
63
# File 'lib/reapack/index/named_node.rb', line 61

def empty?
  @node.element_children.empty?
end

#is_new?Boolean



54
# File 'lib/reapack/index/named_node.rb', line 54

def is_new?; !!@is_new; end

#modified?Boolean



55
# File 'lib/reapack/index/named_node.rb', line 55

def modified?; !!@dirty; end

#nameObject



57
58
59
# File 'lib/reapack/index/named_node.rb', line 57

def name
  @node[NAME_ATTR]
end

#removeObject



65
66
67
# File 'lib/reapack/index/named_node.rb', line 65

def remove
  @node.remove
end