Class: ApiMaker::IndividualCommand

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

Defined Under Namespace

Classes: NotFoundOrNoAccessError

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.



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

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.



4
5
6
# File 'lib/api_maker/individual_command.rb', line 4

def args
  @args
end

#collectionObject (readonly)

Returns the value of attribute collection.



4
5
6
# File 'lib/api_maker/individual_command.rb', line 4

def collection
  @collection
end

#commandObject (readonly)

Returns the value of attribute command.



4
5
6
# File 'lib/api_maker/individual_command.rb', line 4

def command
  @command
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/api_maker/individual_command.rb', line 4

def id
  @id
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



4
5
6
# File 'lib/api_maker/individual_command.rb', line 4

def primary_key
  @primary_key
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/api_maker/individual_command.rb', line 4

def response
  @response
end

Instance Method Details

#error(data = nil) ⇒ Object



15
16
17
# File 'lib/api_maker/individual_command.rb', line 15

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

#fail(data = nil) ⇒ Object



19
20
21
# File 'lib/api_maker/individual_command.rb', line 19

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

#modelObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/api_maker/individual_command.rb', line 23

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

    model ||= collection.find { |model_in_collection| model_in_collection.id.to_s == primary_key.to_s }
    raise_model_not_found_or_no_access unless model

    model
  end
end

#model_idObject



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

def model_id
  primary_key
end

#raise_model_not_found_or_no_accessObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/api_maker/individual_command.rb', line 38

def raise_model_not_found_or_no_access
  command_name = command
    .name
    .delete_prefix("Commands::")
    .gsub(/Command\Z/, "")

  model_name = collection.klass.name

  raise NotFoundOrNoAccessError, "Couldn't find or no access to #{model_name} #{primary_key} on the #{command_name} command"
end

#result(data = nil) ⇒ Object



49
50
51
# File 'lib/api_maker/individual_command.rb', line 49

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