Class: Fatty::Command

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target:, action:, payload: {}) ⇒ Command

Returns a new instance of Command.



7
8
9
10
11
# File 'lib/fatty/command.rb', line 7

def initialize(target:, action:, payload: {})
  @target = target.to_sym
  @action = action.to_sym
  @payload = payload || {}
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/fatty/command.rb', line 5

def action
  @action
end

#payloadObject (readonly)

Returns the value of attribute payload.



5
6
7
# File 'lib/fatty/command.rb', line 5

def payload
  @payload
end

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'lib/fatty/command.rb', line 5

def target
  @target
end

Class Method Details

.coerce(value) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/fatty/command.rb', line 25

def self.coerce(value)
  case value
  when Command
    value
  when Array
    coerce_array(value)
  else
    raise ArgumentError, "expected command, got #{value.inspect}"
  end
end

.normalize_list(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/fatty/command.rb', line 36

def self.normalize_list(value)
  result =
    case value
    when nil
      []
    when Command
      [value]
    when Array
      value.grep(Command)
    else
      []
    end

  result
end

.session(id, action, **payload) ⇒ Object



21
22
23
# File 'lib/fatty/command.rb', line 21

def self.session(id, action, **payload)
  new(target: id, action: action, payload: payload)
end

.terminal(action, **payload) ⇒ Object



17
18
19
# File 'lib/fatty/command.rb', line 17

def self.terminal(action, **payload)
  new(target: :terminal, action: action, payload: payload)
end

Instance Method Details

#idObject



60
61
62
# File 'lib/fatty/command.rb', line 60

def id
  target unless terminal?
end

#inspectObject



13
14
15
# File 'lib/fatty/command.rb', line 13

def inspect
  "Command: @target=#{@target}, @action=#{@action} @payload keys=#{@payload.keys}"
end

#session?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/fatty/command.rb', line 56

def session?
  !terminal?
end

#terminal?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/fatty/command.rb', line 52

def terminal?
  target == :terminal
end