Module: Kobana::Resources::Operations

Included in:
Base
Defined in:
lib/kobana/resources/operations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
# File 'lib/kobana/resources/operations.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#deleteObject

rubocop:disable Naming/PredicateMethod



108
109
110
111
112
113
114
115
116
117
# File 'lib/kobana/resources/operations.rb', line 108

def delete
  response = request(:delete, uri)
  case response[:status]
  when 204
    true
  else
    handle_error_response(response)
    false
  end
end

#find_command(command_id) ⇒ Object

Raises:

  • (ArgumentError)


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/kobana/resources/operations.rb', line 132

def find_command(command_id)
  raise ArgumentError, "Command ID cannot be nil" if command_id.nil?

  response = request(:get, "#{uri}/commands/#{command_id}")
  case response[:status]
  when 200
    # response[:data].map { |command| Kobana::Resources::Command.new(command) }
    response[:data].map { |command| command }
  else
    handle_error_response(response)
    nil
  end
end

#handle_error_response(response) ⇒ Object



146
147
148
149
# File 'lib/kobana/resources/operations.rb', line 146

def handle_error_response(response)
  self.class.handle_error_response(response)
  @errors = self.class.errors
end

#list_commands(params = {}) ⇒ Object

rubocop:enable Naming/PredicateMethod



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/kobana/resources/operations.rb', line 120

def list_commands(params = {})
  response = request(:get, "#{uri}/commands", params)
  case response[:status]
  when 200
    # response[:data].map { |command| Kobana::Resources::Command.new(command) }
    response[:data].map { |command| command }
  else
    handle_error_response(response)
    []
  end
end

#saveObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kobana/resources/operations.rb', line 75

def save
  if new_record?
    response = self.class.create(attributes)
    if response.created?
      @attributes = response.attributes
      @errors = []
      true
    else
      @errors = response.errors
      false
    end
  else
    update({})
  end
end

#update(new_attributes = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kobana/resources/operations.rb', line 91

def update(new_attributes = {})
  return if new_attributes.empty?

  data = attributes.merge(new_attributes.deep_symbolize_keys)
  response = request(:put, uri, data.to_json)
  case response[:status]
  when 200..204
    self.class.new(response[:data].merge(updated: true))
  else
    handle_error_response(response)
    resource = self.class.new(attributes.merge(updated: false))
    resource.errors = @errors
    resource
  end
end