Class: HaveAPI::Fs::Components::ActionDir

Inherits:
Directory show all
Defined in:
lib/haveapi/fs/components/action_dir.rb

Direct Known Subclasses

CreateActionDir, DeleteActionDir, UpdateActionDir

Constant Summary

Constants included from Help

Help::SEARCH_PATH

Instance Attribute Summary collapse

Attributes inherited from HaveAPI::Fs::Component

#atime, #context, #ctime, #mtime

Instance Method Summary collapse

Methods inherited from Directory

#directory?

Methods included from Help

find!, included

Methods inherited from HaveAPI::Fs::Component

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

Constructor Details

#initialize(resource, action) ⇒ ActionDir

Returns a new instance of ActionDir.



7
8
9
10
11
12
# File 'lib/haveapi/fs/components/action_dir.rb', line 7

def initialize(resource, action)
  super()
  
  @resource = resource
  @action = action
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/haveapi/fs/components/action_dir.rb', line 4

def action
  @action
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/haveapi/fs/components/action_dir.rb', line 4

def resource
  @resource
end

Instance Method Details

#contentsObject



27
28
29
30
31
# File 'lib/haveapi/fs/components/action_dir.rb', line 27

def contents
  ret = super + %w(input output status message meta errors exec reset)
  ret << 'exec.yml' if @action.input_params.any?
  ret
end

#exec(meta: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/haveapi/fs/components/action_dir.rb', line 33

def exec(meta: {})
  @action.provide_args(*@resource.prepared_args)

  params = children[:input].values
  params[:meta] = meta
  params[:meta].update(children[:meta].values)

  ret = HaveAPI::Client::Response.new(
      @action,
      @action.execute(params)
  )

  children[:status].set(ret.ok?)

  if ret.ok?
    case @action.output_layout
    when :object
      res = HaveAPI::Client::ResourceInstance.new(
          @resource.instance_variable_get('@client'),
          @resource.instance_variable_get('@api'),
          @resource,
          action: @action,
          response: ret,
      )

    when :object_list
      res = HaveAPI::Client::ResourceInstanceList.new(
          @resource.instance_variable_get('@client'),
          @resource.instance_variable_get('@api'),
          @resource,
          @action,
          ret,
      )

    when :hash
      res = HaveAPI::Fs::HashWrapper.new(
          @resource.instance_variable_get('@client'),
          @resource.instance_variable_get('@api'),
          @resource,
          @action,
          ret.response,
      )

    when :hash_list
      res = HaveAPI::Fs::HashListWrapper.new(
          @resource.instance_variable_get('@client'),
          @resource.instance_variable_get('@api'),
          @resource,
          @action,
          ret.response,
      )

    else
      res = ret.response
    end

    children[:output].data = res
    children[:meta].output = ret.meta

    ret.wait_for_completion if @context.opts[:block] && @action.blocking?

  else
    children[:message].set(ret.message)
    children[:errors].set(ret.errors)
  end
  
  ret

rescue HaveAPI::Client::ValidationError => e
  children[:status].set(false)
  children[:message].set(e.message)
  children[:errors].set(e.errors)
  e

rescue HaveAPI::Client::ActionFailed => e
  children[:status].set(false)
  children[:message].set(e.response.message)
  children[:errors].set(e.response.errors)
  e.response
end

#setupObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/haveapi/fs/components/action_dir.rb', line 14

def setup
  super

  children[:status] = [ActionStatus, self, bound: true]
  children[:message] = [ActionMessage, self, bound: true]
  children[:errors] = [ActionErrors, self, bound: true]
  children[:input] = [ActionInput, self, bound: true]
  children[:output] = [ActionOutput, self, bound: true]
  children[:meta] = [ActionMeta, self, bound: true]
  children[:exec] = [ActionExec, self, bound: true]
  children[:reset] = [DirectoryReset, bound: true]
end

#titleObject



114
115
116
# File 'lib/haveapi/fs/components/action_dir.rb', line 114

def title
  @action.name.capitalize
end