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



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

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

#exec(meta: {}) ⇒ Object



32
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
# File 'lib/haveapi/fs/components/action_dir.rb', line 32

def exec(meta: {})
  @action.provide_args(*@resource.prepared_args)
  ret = HaveAPI::Client::Response.new(
      @action,
      @action.execute(
          children[:input].values.update({meta: meta})
      )
  )

  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,
      )

    else
      res = ret
    end

    children[:output].data = res

  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
# 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[:exec] = [ActionExec, self, bound: true]
  children[:reset] = [DirectoryReset, bound: true]
end

#titleObject



89
90
91
# File 'lib/haveapi/fs/components/action_dir.rb', line 89

def title
  @action.name.capitalize
end