Class: HaveAPI::Fs::Components::ActionExecEdit

Inherits:
File show all
Defined in:
lib/haveapi/fs/components/action_exec_edit.rb

Direct Known Subclasses

InstanceCreate, InstanceEdit

Instance Attribute Summary

Attributes inherited from HaveAPI::Fs::Component

#atime, #context, #ctime, #mtime

Instance Method Summary collapse

Methods inherited from File

#file?, #raw_close, #raw_open, #raw_read, #raw_sync, #raw_truncate, #raw_write, #size

Methods inherited from HaveAPI::Fs::Component

#abspath, #bound=, #bound?, children_reader, component, #contents, #directory?, #executable?, #file?, #find, inherited, #invalid?, #invalidate, #parent, #path, #readable?, #reset, #setup, #times, #title, #unsaved?, #use

Constructor Details

#initialize(action_dir) ⇒ ActionExecEdit

Returns a new instance of ActionExecEdit.



5
6
7
8
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 5

def initialize(action_dir)
  super()
  @action_dir = action_dir
end

Instance Method Details

#headerObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 67

def header
  <<END
# This file is in YAML format. Lines beginning with a hash (#) are comments and
# are ignored. The action will be executed once this file is saved and closed.
# The success of this operation can be later checked in
# actions/#{@action_dir.action.name}/status.
# 
# Only required parameters that need to be set are uncommented by default.
# Parameters that are not specified when this file is closed will not be sent
# to the API.
#
# To cancel the operation, either do not save the file or save it empty.
END
end

#readObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 14

def read
  ret = header + "\n"

  @action_dir.action.input_params.each do |name, p|
    param_file = @action_dir.find(:input).find(name)

    if param_file.set?
      v = param_file.new_value
    
    elsif p[:default].nil?
      v = nil

    else
      v = p[:default]
    end

    ret += "# #{p[:label]}; #{p[:type]}\n"
    ret += "# #{p[:description]}\n"
    ret += "# Defaults to '#{p[:default]}'\n" unless p[:default].nil?

    if p[:required] || param_file.set?
      ret += "#{name}: #{v}"

    else
      ret += "##{name}: #{v}"
    end

    ret += "\n\n"
  end

  ret
end

#saveObject



86
87
88
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 86

def save
  @action_dir.exec
end

#save?(data) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 82

def save?(data)
  true
end

#writable?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 10

def writable?
  true
end

#write(str) ⇒ Object

Raises:

  • (Errno::EIO)


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/haveapi/fs/components/action_exec_edit.rb', line 47

def write(str)
  return if str.strip.empty?

  data = YAML.load(str)
  raise Errno::EIO, 'invalid yaml document' unless data.is_a?(::Hash)
  return unless save?(data)

  params = @action_dir.action.input_params

  data.each do |k, v|
    p = @action_dir.find(:input).find(k.to_sym)
    next if p.nil?

    # Type coercion is done later by the client during action call
    p.write_safe(v)
  end

  save
end