Class: ApiMaker::IndividualCommand
- Inherits:
-
Object
- Object
- ApiMaker::IndividualCommand
- Defined in:
- lib/api_maker/individual_command.rb
Defined Under Namespace
Classes: NotFoundOrNoAccessError
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#primary_key ⇒ Object
readonly
Returns the value of attribute primary_key.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #error(data = nil) ⇒ Object
- #fail(data = nil) ⇒ Object
-
#initialize(id:, args:, collection:, command:, primary_key: nil, response:) ⇒ IndividualCommand
constructor
A new instance of IndividualCommand.
- #model ⇒ Object
- #model_id ⇒ Object
- #raise_model_not_found_or_no_access ⇒ Object
- #result(data = nil) ⇒ Object
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
4 5 6 |
# File 'lib/api_maker/individual_command.rb', line 4 def args @args end |
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
4 5 6 |
# File 'lib/api_maker/individual_command.rb', line 4 def collection @collection end |
#command ⇒ Object (readonly)
Returns the value of attribute command.
4 5 6 |
# File 'lib/api_maker/individual_command.rb', line 4 def command @command end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/api_maker/individual_command.rb', line 4 def id @id end |
#primary_key ⇒ Object (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 |
#response ⇒ Object (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 |
#model ⇒ Object
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_id ⇒ Object
34 35 36 |
# File 'lib/api_maker/individual_command.rb', line 34 def model_id primary_key end |
#raise_model_not_found_or_no_access ⇒ Object
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 |