Class: Mixml::Application::SelectCommand

Inherits:
Commander::Command
  • Object
show all
Defined in:
lib/mixml/application.rb

Overview

Command that selects nodes

Direct Known Subclasses

ModifyCommand

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, args = ARGV) ⇒ SelectCommand

Initialize a new command

Parameters:

  • method (Symbol)

    Command method

  • args (Array<String>) (defaults to: ARGV)

    Command arguments



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mixml/application.rb', line 19

def initialize(method, args = ARGV)
    super(method)

    @method = method
    @selectors = []

    option '-x', '--xpath STRING', String, 'XPath expression to select nodes' do |value|
        @selectors << ->(tool) {
            xpath value
        }
    end

    option '-c', '--css STRING', String, 'CSS rule to select nodes' do |value|
        @selectors << ->(tool) {
            css value
        }
    end

    when_called self, :execute
end

Instance Attribute Details

#suppress_outputBoolean

Returns Suppress automatic output of result.

Returns:

  • (Boolean)

    Suppress automatic output of result



13
14
15
# File 'lib/mixml/application.rb', line 13

def suppress_output
  @suppress_output
end

Instance Method Details

#before(args, options) ⇒ Object

Invoked before the command is executed

Parameters:

  • args (Array<String>)

    Arguments from the command line

  • options (Commander::Command::Options)

    Options from the command line



71
72
# File 'lib/mixml/application.rb', line 71

def before(args, options)
end

#execute(args, options) ⇒ Object

Run the command

Parameters:

  • args (Array<String>)

    Arguments from the command line

  • options (Commander::Command::Options)

    Options from the command line



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/mixml/application.rb', line 44

def execute(args, options)
    before args, options

    if @suppress_output then
        $tool.print = false
        $tool.save = false
    end

    $tool.work(args) do
        @selectors.each do |selector|
            selection = instance_eval(&selector)
            selection.send name, *parameters
        end
    end
end

#parametersArray

Parameters for command execution

Returns:

  • (Array)

    Parameters



63
64
65
# File 'lib/mixml/application.rb', line 63

def parameters
    []
end