class JustimmoClient::OptionParser
@api private
Constants
- OptionParserError
Raised when option parsing fails
Attributes
mappings[RW]
range_suffix[RW]
Public Class Methods
new(options = {}) { |self| ... }
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 34 def initialize(options = {}) @options = options @mappings = {} @range_suffix = %i[_min _max] @context = nil yield self if block_given? parse unless options.empty? end
Public Instance Methods
add(key, **options, &block)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 45 def add(key, **options, &block) add_option(key, options, &block) end
group(groupname) { |self| ... }
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 49 def group(groupname) @context = groupname.to_sym yield self if block_given? @context = nil end
parse(options = {})
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 55 def parse(options = {}) out = {} options.each do |key, value| raise InvalidOption, key unless @options.key?(key.to_sym) group = group_of(key) if group out[group] ||= {} out[group].update(parse_option(key.to_sym, value).reject { |_k, v| v.nil? }) else out.update(parse_option(key.to_sym, value).reject { |_k, v| v.nil? }) end end out end
Private Instance Methods
add_option(key, **options, &block)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 75 def add_option(key, **options, &block) group = group_of(key) raise DuplicateKeyGroup.new(key, group) unless group.nil? @options[key.to_sym] = { group: @context, type: options[:type], as: options[:as], values: options[:values], mod: block_given? ? block : nil } end
apply_mod(key, value)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 96 def apply_mod(key, value) new_key, new_value = @options.dig(key, :mod).call(key, value) [(new_key || key), (new_value || value)] end
bool_to_i(value)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 142 def bool_to_i(value) case value when "true" then 1 when "false" then 0 else value ? 1 : 0 end end
group_of(key)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 88 def group_of(key) @options.dig(key, :group) end
has_mod?(key)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 92 def has_mod?(key) @options.dig(key, :mod).is_a?(Proc) end
mapping(key)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 101 def mapping(key) @mappings.fetch(key, key) end
parse_option(key, value)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 121 def parse_option(key, value) values = @options.dig(key, :values) raise InvalidValue, value unless values.nil? || values.include?(value) key, value = apply_mod(key.to_sym, value) if has_mod?(key) coerced = case @options.dig(key, :type) when :bool then bool_to_i(value) else mapping(value) end { translate(key) => coerced } end
parse_range(key, range)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 136 def parse_range(key, range) min, max = @options[key][:range_suffix] api_param = @options[key][:mapped] { "#{api_param}#{min}": range.first, "#{api_param}#{max}": range.last } end
translate(key)
click to toggle source
# File lib/justimmo_client/option_parser.rb, line 105 def translate(key) return @options[key][:as] if @options[key][:as] suffix = case key when /(.*)_min/ then @range_suffix.first when /(.*)_max/ then @range_suffix.last when /(.*)_id/ then "_id" else nil end key = ($1 || key).to_sym "#{mapping(key)}#{suffix}".to_sym end