Module: CliMiami

Defined in:
lib/cli_miami.rb,
lib/cli_miami/metadata.rb

Overview

Create Cli Miami namespace and load library

Defined Under Namespace

Classes: A, Error, S, Validation

Constant Summary collapse

BOOLEAN_TRUE_VALUES =
%w(true t yes y).freeze
BOOLEAN_FALSE_VALUES =
%w(false f no n).freeze
TYPE_MAP =

keys: type values allowed through the API values: type used to validate internally in the Validation class

{
  array:            :array,
  boolean:          :boolean,
  dir:              :file,
  directory:        :file,
  file:             :file,
  fixnum:           :fixnum,
  float:            :float,
  folder:           :file,
  multiple_choice:  :multiple_choice,
  hash:             :hash,
  integer:          :fixnum,
  list:             :array,
  number:           :fixnum,
  object:           :hash,
  path:             :file,
  range:            :range,
  string:           :string,
  symbol:           :symbol
}.freeze
VERSION =
'1.0.10.pre'.freeze
SUMMARY =
'A feature rich alternative for gets and puts for your command line tool'.freeze
@@presets =

default presets rubocop:disable Style/ClassVars

{
  cli_miami_fail: {
    indent: 2,
    color: :red,
    style: [:bold, :underline]
  },
  cli_miami_instruction: {
    color: :green,
    style: :bold
  },
  cli_miami_instruction_sub: {
    indent: 2,
    color: :green
  },
  cli_miami_success: {
    color: :green,
    style: [:bold, :underline]
  },
  cli_miami_update: {
    indent: 2,
    color: :cyan
  }
}

Class Method Summary collapse

Class Method Details

.get_options(user_options = {}) ⇒ Object

build an options hash from preset options and/or additional options

build shared validation hash object

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cli_miami.rb', line 143

def self.get_options user_options = {}
  # lookup preset if passed in as a symbol, or as a :preset key in an options hash
  if user_options.is_a? Hash
    user_options.deep_symbolize_keys!
    options = @@presets[user_options.delete(:preset)] || {}
    options.deep_merge! user_options
  else
    options = @@presets[user_options.to_sym] || {}
  end

  # make sure all keys are symbols
  options.deep_symbolize_keys!

  # set defaults
  options.reverse_merge!(
    description: nil,
    max: Float::INFINITY,
    min: options.delete(:required) ? 1 : 0,
    newline: true,
    justify: :left,
    padding: 0,
    preset: nil,
    regexp: //,
    style: [],
    type: :string
  )

  # prepare style array
  options[:style] = [options[:style]].flatten

  # lookup type in type map
  options[:type] = TYPE_MAP[options[:type].to_sym] || :string

  # convert range to min/max values
  range = options.delete(:range)
  if range
    options[:min] = range.min
    options[:max] = range.max
  end

  # convert length to min/max values
  length = options.delete(:length)
  if length
    options[:min] = options[:max] = length
  end

  # if value options are set, apply the same treatment to them
  value_options = options[:value_options]
  if value_options
    options[:value_options] = get_options value_options
    options[:value_options][:description] = options[:description]
  end

  options
end

.presetsObject

getter for the presets


134
135
136
# File 'lib/cli_miami.rb', line 134

def self.presets
  @@presets
end

.set_preset(type, options) ⇒ Object

Create a new custom preset

Raises:

  • (ArgumentError)

118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cli_miami.rb', line 118

def self.set_preset type, options
  raise ArgumentError, 'Preset must be a hash of options' unless options.is_a? Hash

  # extend preset if it exists
  extend_preset = options.delete(:preset)
  if extend_preset && @@presets[extend_preset.to_sym]
    extend_preset_options = @@presets[extend_preset.to_sym]
    options = extend_preset_options.merge! options
  end

  # set options to global var
  @@presets[type] = options
end

.versionObject

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity


200
201
202
# File 'lib/cli_miami.rb', line 200

def self.version
  CliMiami::VERSION
end