class HaveAPI::Parameters::Typed

Constants

ATTRIBUTES

Attributes

default[R]
desc[R]
label[R]
name[R]
type[R]

Public Class Methods

new(name, args = {}) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 9
def initialize(name, args = {})
  # The hash values are deleted and it shouldn't affect the received hash
  myargs = args.clone

  @name = name
  @label = myargs.delete(:label) || name.to_s.capitalize
  @layout = :custom

  (ATTRIBUTES - %i[label]).each do |attr|
    instance_variable_set("@#{attr}", myargs.delete(attr))
  end

  @type ||= String

  @validators = HaveAPI::ValidatorChain.new(myargs) unless myargs.empty?
  raise "unused arguments #{myargs}" unless myargs.empty?
end

Public Instance Methods

add_validator(k, v) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 59
def add_validator(k, v)
  @validators ||= HaveAPI::ValidatorChain.new({})
  @validators.add_or_replace(k, v)
end
clean(raw) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 75
def clean(raw)
  return instance_exec(raw, &@clean) if @clean

  if raw.nil?
    @default

  elsif @type.nil?
    nil

  elsif @type == Integer
    raw.to_i

  elsif @type == Float
    raw.to_f

  elsif @type == Boolean
    Boolean.to_b(raw)

  elsif @type == ::Datetime
    begin
      DateTime.iso8601(raw).to_time
    rescue ArgumentError
      raise HaveAPI::ValidationError, "not in ISO 8601 format '#{raw}'"
    end

  else
    raw
  end
end
db_name() click to toggle source
# File lib/haveapi/parameters/typed.rb, line 27
def db_name
  @db_name || @name
end
describe(context) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 47
def describe(context)
  {
    required: required?,
    label: @label,
    description: @desc,
    type: @type ? @type.to_s : String.to_s,
    validators: @validators ? @validators.describe : {},
    default: @default,
    protected: @protected || false
  }
end
fill?() click to toggle source
# File lib/haveapi/parameters/typed.rb, line 39
def fill?
  @fill
end
format_output(v) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 109
def format_output(v)
  if v.nil?
    nil

  elsif @type == ::Datetime && v.is_a?(Time)
    v.iso8601

  elsif @type == Boolean
    v ? true : false

  elsif @type == Integer
    v.to_i

  elsif @type == Float
    v.to_f

  elsif @type == String
    v.to_s

  else
    v
  end
end
load_validators?() click to toggle source
# File lib/haveapi/parameters/typed.rb, line 43
def load_validators?
  @load_validators.nil? || @load_validators
end
optional?() click to toggle source
# File lib/haveapi/parameters/typed.rb, line 35
def optional?
  !@required
end
patch(attrs) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 64
def patch(attrs)
  attrs.each do |k, v|
    if ATTRIBUTES.include?(k)
      instance_variable_set("@#{k}", v)

    else
      add_validator(k, v)
    end
  end
end
required?() click to toggle source
# File lib/haveapi/parameters/typed.rb, line 31
def required?
  @validators ? @validators.required? : false
end
validate(v, params) click to toggle source
# File lib/haveapi/parameters/typed.rb, line 105
def validate(v, params)
  @validators ? @validators.validate(v, params) : true
end