Class: ApiMaker::IndividualCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/api_maker/individual_command.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, args:, collection:, command:, primary_key: nil, response:) ⇒ IndividualCommand

Returns a new instance of IndividualCommand.



4
5
6
7
8
9
10
11
# File 'lib/api_maker/individual_command.rb', line 4

def initialize(id:, args:, collection:, command:, primary_key: nil, response:)
  @id = id
  @args = args
  @collection = collection
  @command = command
  @primary_key = primary_key
  @response = response
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



2
3
4
# File 'lib/api_maker/individual_command.rb', line 2

def args
  @args
end

#commandObject (readonly)

Returns the value of attribute command.



2
3
4
# File 'lib/api_maker/individual_command.rb', line 2

def command
  @command
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/api_maker/individual_command.rb', line 2

def id
  @id
end

Instance Method Details

#error(data = nil) ⇒ Object



13
14
15
# File 'lib/api_maker/individual_command.rb', line 13

def error(data = nil)
  @response.error_for_command(@id, data)
end

#fail(data = nil) ⇒ Object



17
18
19
# File 'lib/api_maker/individual_command.rb', line 17

def fail(data = nil)
  @response.fail_for_command(@id, data)
end

#modelObject



21
22
23
24
25
26
27
28
# File 'lib/api_maker/individual_command.rb', line 21

def model
  raise "Collection wasn't set" unless @collection

  @model ||= @collection.find { |model| model.id.to_s == @primary_key.to_s }
  raise "Couldn't find model by that ID: #{@primary_key}" unless @model

  @model
end

#model_idObject



30
31
32
# File 'lib/api_maker/individual_command.rb', line 30

def model_id
  @primary_key
end

#result(data = nil) ⇒ Object



34
35
36
# File 'lib/api_maker/individual_command.rb', line 34

def result(data = nil)
  @response.result_for_command(@id, ApiMaker::ResultParser.parse(data, controller: @response.controller))
end