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

Class Method Details

.allArray<Resource>

Returns every registered resource, ordered as declared.

Returns:

  • (Array<Resource>)

    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

.commandsArray<String>

Returns every command word, sorted.

Returns:

  • (Array<String>)

    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.

Parameters:

  • command (String)

Returns:



119
120
121
# File 'lib/smily_cli/registry.rb', line 119

def self.fetch(command)
  find(command) || raise(UsageError, unknown_message(command))
end

.find(command) ⇒ Resource?

Look up a resource by its command word.

Parameters:

  • command (String)

Returns:



111
112
113
# File 'lib/smily_cli/registry.rb', line 111

def self.find(command)
  index[command.to_s]
end

.groupedHash{String => Array<Resource>}

Registered resources grouped by their SmilyCli::Resource#group, preserving the declaration order of both groups and members.

Returns:



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

.indexObject

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