class DearInventory::Parameters

Public Class Methods

convert(resource_class, endpoint, params) click to toggle source
# File lib/dear_inventory/parameters.rb, line 40
def convert(resource_class, endpoint, params)
  params_class = endpoint_class(resource_class, endpoint)

  convert_with_params_class(
    endpoint: endpoint, params_class: params_class,
    resource_class: resource_class, params: params
  )
end
fields(fields) click to toggle source
# File lib/dear_inventory/parameters.rb, line 25
def fields(fields)
  const_set(:FIELDS, fields.freeze)

  fields.each do |name, specifications|
    define_method_for_validator(name, specifications)
  end
end
new(params) click to toggle source
# File lib/dear_inventory/parameters.rb, line 10
def initialize(params)
  @values = T.let({}, T::Hash[Symbol, T.untyped])
  assign_params(params)
end

Private Class Methods

convert_with_params_class( params_class:, resource_class:, endpoint:, params: ) click to toggle source
# File lib/dear_inventory/parameters.rb, line 168
def convert_with_params_class(
  params_class:, resource_class:, endpoint:, params:
)
  return params_class.new(params) unless params_class.nil?

  message = "No parameters class can be found for resource, " \
    "#{resource_class}, with endpoint #{endpoint}"
  raise ArgumentError, message
end
define_enum_method(name, specifications) click to toggle source
# File lib/dear_inventory/parameters.rb, line 101
def define_enum_method(name, specifications)
  validator = DearInventory::Validators::Enum
  options = {
    values: specifications[:values],
  }
  define_method_with_options(name, validator, options)
end
define_method_for_validator(name, specifications) click to toggle source
# File lib/dear_inventory/parameters.rb, line 75
def define_method_for_validator(name, specifications)
  case specifications[:type]
  when :Enum
    define_enum_method(name, specifications)
  when :String
    define_string_method(
      name,
      T.cast(specifications, T::Hash[Symbol, T.any(Symbol, T::Boolean)])
    )
  else
    define_method_without_options(
      name,
      T.cast(specifications, T::Hash[Symbol, T.any(Symbol, T::Boolean)])
    )
  end
end
define_method_with_options(name, validator, options) click to toggle source
# File lib/dear_inventory/parameters.rb, line 132
def define_method_with_options(name, validator, options)
  define_method("#{name}=") do |value|
    validator.(
      name,
      value,
      options: options
    )

    T.unsafe(self).instance_variable_get(:@values)[name] = value
  end
end
define_method_without_options(name, specifications) click to toggle source
# File lib/dear_inventory/parameters.rb, line 150
def define_method_without_options(name, specifications)
  validator = Object.const_get(
    "DearInventory::Validators::#{specifications[:type]}"
  )
  define_method("#{name}=") do |value|
    validator.(name, value)
    T.unsafe(self).instance_variable_get(:@values)[name] = value
  end
end
define_string_method(name, specifications) click to toggle source
# File lib/dear_inventory/parameters.rb, line 115
def define_string_method(name, specifications)
  validator = DearInventory::Validators::String
  options = {
    max_length: specifications[:max_length],
  }
  define_method_with_options(name, validator, options)
end
endpoint_class(resource_class, endpoint) click to toggle source
# File lib/dear_inventory/parameters.rb, line 57
def endpoint_class(resource_class, endpoint)
  params_class = DearInventory::EndpointClass.(
    class_type: "Parameters",
    resource_class: resource_class,
    endpoint: endpoint
  )
  T.cast(params_class, T.class_of(DearInventory::Parameters))
end

Public Instance Methods

to_h() click to toggle source
# File lib/dear_inventory/parameters.rb, line 180
def to_h
  self.class.
    const_get(:FIELDS).
    each_with_object({}) do |(field_name, specifications), hash|
      value = parameter_value(field_name, specifications)
      next if value.nil?

      hash[specifications[:property]] = value
    end
end

Private Instance Methods

assign_params(params) click to toggle source
# File lib/dear_inventory/parameters.rb, line 211
def assign_params(params)
  params.each do |name, value|
    public_send(:"#{name}=", value)
  end
end
parameter_value(field_name, specifications) click to toggle source
# File lib/dear_inventory/parameters.rb, line 199
def parameter_value(field_name, specifications)
  value = @values[field_name]
  Validators::Required.(field_name, value) if specifications[:required]

  if value.respond_to?(:strftime)
    ::DearInventory::DateTime.new(value).to_s
  else
    value
  end
end