Class: Mothership::Inputs

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, context = nil, inputs = {}, given = {}, global = {}) ⇒ Inputs

Returns a new instance of Inputs.



8
9
10
11
12
13
14
15
16
# File 'lib/mothership/inputs.rb', line 8

def initialize(
    command, context = nil,
    inputs = {}, given = {}, global = {})
  @command = command
  @context = context
  @inputs = inputs
  @given = given
  @global = global
end

Instance Attribute Details

#current_inputObject (readonly)

the input being processed; set during #get



6
7
8
# File 'lib/mothership/inputs.rb', line 6

def current_input
  @current_input
end

#givenObject (readonly)

Returns the value of attribute given.



3
4
5
# File 'lib/mothership/inputs.rb', line 3

def given
  @given
end

#globalObject (readonly)

Returns the value of attribute global.



3
4
5
# File 'lib/mothership/inputs.rb', line 3

def global
  @global
end

#inputsObject (readonly)

Returns the value of attribute inputs.



3
4
5
# File 'lib/mothership/inputs.rb', line 3

def inputs
  @inputs
end

Instance Method Details

#[](name, *args) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mothership/inputs.rb', line 57

def [](name, *args)
  return @inputs[name] if @inputs.key?(name)

  if @command && meta = @command.inputs[name]
    # special case so #invoke can be called with singular-named inputs
    singular = meta[:singular]

    if @inputs.key?(singular)
      return @inputs[name] = [@inputs[singular]]
    end
  end

  found, val = get(name, @context, *args)

  @inputs[name] = val unless meta && meta[:forget]

  val
end

#direct(name) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/mothership/inputs.rb', line 22

def direct(name)
  if @inputs.key?(name)
    @inputs[name]
  elsif (given = @given[name]) != :interact
    given
  end
end

#forget(name) ⇒ Object



124
125
126
127
# File 'lib/mothership/inputs.rb', line 124

def forget(name)
  @inputs.delete(name)
  @given.delete(name)
end

#get(name, context, *args) ⇒ Object

search:

  1. cache

  2. cache, singular

  3. given

  4. given, singular

  5. global

  6. global, singular



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/mothership/inputs.rb', line 101

def get(name, context, *args)
  before_input = @current_input
  @current_input = [name, args]

  if @command && meta = @command.inputs[name]
    found, val = find_in(@given, name, meta, context, *args)
  elsif @global.is_a?(self.class)
    return @global.get(name, context, *args)
  elsif @global.key?(name)
    return [true, @global[name]]
  end

  return [false, val] if not found

  if val == :interact
    [true, interact(name, context, *args)]
  else
    [true, convert_given(meta, context, val, *args)]
  end
ensure
  @current_input = before_input
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/mothership/inputs.rb', line 18

def has?(name)
  @inputs.key?(name) || @given.key?(name)
end

#interact(name, context, *args) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/mothership/inputs.rb', line 76

def interact(name, context, *args)
  meta =
    if @command
      @command.inputs[name]
    else
      Mothership.global_option(name)
    end

  interact = meta[:interact] || :"ask_#{name}"

  case interact
  when Symbol, String
    context.send(interact, *args)
  else
    context.instance_exec(*args, &interact)
  end
end

#interactive?(name) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/mothership/inputs.rb', line 129

def interactive?(name)
  @given[name] == :interact
end

#merge(inputs) ⇒ Object



30
31
32
# File 'lib/mothership/inputs.rb', line 30

def merge(inputs)
  self.class.new(@command, @context, @inputs.merge(inputs), @given, @global)
end

#merge_given(given) ⇒ Object



34
35
36
# File 'lib/mothership/inputs.rb', line 34

def merge_given(given)
  self.class.new(@command, @context, @inputs.dup, @given.merge(given), @global)
end

#rebase(inputs) ⇒ Object



38
39
40
# File 'lib/mothership/inputs.rb', line 38

def rebase(inputs)
  self.class.new(@command, @context, inputs.merge(@inputs), @given, @global)
end

#rebase_given(given) ⇒ Object



42
43
44
# File 'lib/mothership/inputs.rb', line 42

def rebase_given(given)
  self.class.new(@command, @context, @inputs.dup, given.merge(@given), @global)
end

#without(*names) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/mothership/inputs.rb', line 46

def without(*names)
  given = @given.dup
  inputs = @inputs.dup
  names.each do |n|
    given.delete(n)
    inputs.delete(n)
  end

  self.class.new(@command, @context, inputs, given, @global)
end