Class: SmilyCli::ResourceCommand
- Inherits:
-
BaseCommand
- Object
- Thor
- BaseCommand
- SmilyCli::ResourceCommand
- Defined in:
- lib/smily_cli/resource_command.rb
Overview
The set of subcommands every registered resource gets: list, get, and
(unless the resource is read-only) create, update, delete. One bound
subclass is generated per Resource via ResourceCommand.bind; the bound class carries
the resource so the shared command bodies know which path/key to use.
Defined Under Namespace
Modules: Bound
Class Attribute Summary collapse
-
.resource ⇒ Resource?
readonly
The resource this class is bound to.
Class Method Summary collapse
-
.bind(resource) ⇒ Class<ResourceCommand>
Build a Thor subcommand class bound to
resource. - .camelize(command) ⇒ Object private
Instance Method Summary collapse
Methods inherited from BaseCommand
Class Attribute Details
.resource ⇒ Resource? (readonly)
Returns the resource this class is bound to.
18 19 20 |
# File 'lib/smily_cli/resource_command.rb', line 18 def resource @resource end |
Class Method Details
.bind(resource) ⇒ Class<ResourceCommand>
Build a Thor subcommand class bound to resource.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/smily_cli/resource_command.rb', line 24 def bind(resource) klass = Class.new(self) klass.instance_variable_set(:@resource, resource) # Make Thor render help/usage as `smily <command> ...` rather than # deriving a namespace from the generated constant name. klass.namespace(resource.command) klass.remove_command("create", "update", "delete") if resource.readonly klass.remove_command("tree") Bound.const_set("#{camelize(resource.command)}Command", klass) klass end |
.camelize(command) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
37 38 39 |
# File 'lib/smily_cli/resource_command.rb', line 37 def camelize(command) command.split(%r{[_/]}).map(&:capitalize).join end |
Instance Method Details
#create ⇒ Object
102 103 104 105 |
# File 'lib/smily_cli/resource_command.rb', line 102 def create path = ["path"] || resource.collection_path render_result(client.create(path, resource.resource_key, require_data)) end |
#delete(id) ⇒ Object
118 119 120 121 122 123 |
# File 'lib/smily_cli/resource_command.rb', line 118 def delete(id) confirm!("Delete #{resource.singular} #{id}?") body = ["data"] ? wrap_delete_body(parse_data(["data"])) : nil client.delete(resource.collection_path, id, body: body) success("Deleted #{resource.singular} #{id}.") end |
#get(id) ⇒ Object
80 81 82 83 |
# File 'lib/smily_cli/resource_command.rb', line 80 def get(id) query = QueryOptions.build(fields: context.fields, include: ["include"]) render_result(client.get(resource.collection_path, id, query: query)) end |
#list ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/smily_cli/resource_command.rb', line 60 def list query = QueryOptions.build( fields: context.fields, include: ["include"], filter: ["filter"] || [], query: ["query"] || [] ) result = client.list( resource.collection_path, query: query, limit: ["limit"], page: ["page"], per_page: ["per_page"], all: ["all"] ) render_result(result) end |
#update(id) ⇒ Object
110 111 112 |
# File 'lib/smily_cli/resource_command.rb', line 110 def update(id) render_result(client.update(resource.collection_path, resource.resource_key, id, require_data)) end |