Module: SmilyCli::Registry
- Defined in:
- lib/smily_cli/registry.rb
Overview
The catalog of BookingSync API v3 resources the CLI exposes as first-class
commands. Sourced from the public API reference
(https://developers.bookingsync.com/reference). Resources absent here can
still be reached with smily api <method> <path>.
Class Method Summary collapse
-
.all ⇒ Array<Resource>
Every registered resource, ordered as declared.
-
.commands ⇒ Array<String>
Every command word, sorted.
-
.fetch(command) ⇒ Resource
Like Registry.find but raises a helpful UsageError when unknown.
-
.find(command) ⇒ Resource?
Look up a resource by its command word.
-
.grouped ⇒ Hash{String => Array<Resource>}
Registered resources grouped by their SmilyCli::Resource#group, preserving the declaration order of both groups and members.
- .index ⇒ Object private
- .singularize(command) ⇒ Object private
Class Method Details
.all ⇒ Array<Resource>
Returns every registered resource, ordered as declared.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/smily_cli/registry.rb', line 88 def self.all @all ||= ROWS.map do |command, group, description, opts| opts ||= {} Resource.new( command: command, path: opts[:path] || command, singular: opts[:singular] || singularize(command), group: group, description: description, readonly: opts.fetch(:readonly, false) ) end.freeze end |
.commands ⇒ Array<String>
Returns every command word, sorted.
103 104 105 |
# File 'lib/smily_cli/registry.rb', line 103 def self.commands @commands ||= all.map(&:command).sort.freeze end |
.fetch(command) ⇒ Resource
Like find but raises a helpful UsageError when unknown.
119 120 121 |
# File 'lib/smily_cli/registry.rb', line 119 def self.fetch(command) find(command) || raise(UsageError, (command)) end |
.find(command) ⇒ Resource?
Look up a resource by its command word.
111 112 113 |
# File 'lib/smily_cli/registry.rb', line 111 def self.find(command) index[command.to_s] end |
.grouped ⇒ Hash{String => Array<Resource>}
Registered resources grouped by their SmilyCli::Resource#group, preserving the declaration order of both groups and members.
127 128 129 130 131 |
# File 'lib/smily_cli/registry.rb', line 127 def self.grouped all.each_with_object({}) do |resource, groups| (groups[resource.group] ||= []) << resource end end |
.index ⇒ 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.
134 135 136 |
# File 'lib/smily_cli/registry.rb', line 134 def self.index @index ||= all.to_h { |r| [r.command, r] }.freeze end |
.singularize(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.
139 140 141 142 143 144 145 146 147 |
# File 'lib/smily_cli/registry.rb', line 139 def self.singularize(command) # Good-enough English singularization for the labels we actually use. case command when /ies\z/ then command.sub(/ies\z/, "y") when /ses\z/ then command.sub(/es\z/, "") when /s\z/ then command.sub(/s\z/, "") else command end end |