Class: Rix::Commands::AddElement

Inherits:
Rix::Command show all
Defined in:
lib/rix/commands/add_element.rb

Instance Attribute Summary collapse

Attributes inherited from Rix::Command

#files, #out, #xpath

Instance Method Summary collapse

Methods inherited from Rix::Command

#after_all, #before, #execute, #help, #on_attribute, #on_document, #on_node, #on_source, #on_text

Instance Attribute Details

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/rix/commands/add_element.rb', line 4

def name
  @name
end

#valueObject

Returns the value of attribute value.



5
6
7
# File 'lib/rix/commands/add_element.rb', line 5

def value
  @value
end

Instance Method Details

#after(path, document) ⇒ Object



26
27
28
29
30
# File 'lib/rix/commands/add_element.rb', line 26

def after(path, document)
  File.open(path, 'w') do |file|
    document.write file
  end
end

#before_allObject



17
18
19
# File 'lib/rix/commands/add_element.rb', line 17

def before_all
  raise "Missing option: -n, --name" if @name.nil? or @name.empty?
end

#on_element(parent) ⇒ Object



21
22
23
24
# File 'lib/rix/commands/add_element.rb', line 21

def on_element(parent)
  element = parent.add_element @name
  element.text = @value
end

#optsObject



7
8
9
10
11
12
13
14
15
# File 'lib/rix/commands/add_element.rb', line 7

def opts
  OptionParser.new do |opts|
    opts.banner = "Usage: rix add-element [options] <xpath> <files>"
    opts.separator ""
    opts.separator "Options:"
    opts.on("-n", "--name NAME", "The name of the new element") { |name| @name = name }
    opts.on("-v", "--value VALUE", "The value of the new element") { |value| @value = value }
  end
end