class ParamsReady::Format

Constants

OMIT_ALL

Attributes

hash[R]
marshal[R]
name[R]
naming_scheme[R]

Public Class Methods

define(name, format) click to toggle source
# File lib/params_ready/format.rb, line 109
def self.define(name, format)
  @names = @names.dup
  @names[name] = format
  @names.freeze
end
instance(name) click to toggle source
# File lib/params_ready/format.rb, line 115
def self.instance(name)
  raise ParamsReadyError, "Unknown format '#{name}'" unless @names.key? name
  @names[name]
end
new(marshal:, naming_scheme:, remap:, omit:, local:, name: nil) click to toggle source
# File lib/params_ready/format.rb, line 25
def initialize(marshal:, naming_scheme:, remap:, omit:, local:, name: nil)
  @marshal = Helpers::Rule(marshal)
  @naming_scheme = naming_scheme
  @remap = remap
  @omit = omit.to_set.freeze
  @local = local
  @name = name.nil? ? name : name.to_sym
  @hash = [@marshal, @naming_scheme, @remap, @omit, @local, @name].hash
  freeze
end
resolve(format_or_name) click to toggle source
# File lib/params_ready/format.rb, line 120
def self.resolve(format_or_name)
  if format_or_name.is_a? Format
    format_or_name
  elsif format_or_name.is_a? Symbol
    instance(format_or_name)
  elsif format_or_name.respond_to? :format
    format_or_name.format
  else
    raise ParamsReadyError, "Not an acceptable format: #{format_or_name}"
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/params_ready/format.rb, line 36
def ==(other)
  return false unless other.is_a? Format
  return true if self.object_id == other.object_id
  return false unless marshal == other.marshal
  return false unless naming_scheme == other.naming_scheme
  return false unless remap? == other.remap?
  return false unless @omit == other.instance_variable_get(:@omit)
  return false unless local? == other.local?
  return false unless name == other.name

  true
end
alternative?() click to toggle source
# File lib/params_ready/format.rb, line 49
def alternative?
  @naming_scheme == :alternative
end
hash_key(parameter) click to toggle source
# File lib/params_ready/format.rb, line 61
def hash_key(parameter)
  case @naming_scheme
  when :standard then parameter.name
  when :alternative then parameter.altn
  else
    raise ParamsReadyError, "Unexpected option: #{@naming_scheme}"
  end
end
local?() click to toggle source
# File lib/params_ready/format.rb, line 78
def local?
  @local
end
marshal?(type) click to toggle source
# File lib/params_ready/format.rb, line 86
def marshal?(type)
  @marshal.include?(type)
end
omit?(parameter) click to toggle source
# File lib/params_ready/format.rb, line 70
def omit?(parameter)
  return true if parameter.no_output?(self)
  return true if parameter.is_undefined? && @omit.member?(:undefined)
  return true if parameter.is_nil? && @omit.member?(:nil)
  return true if parameter.is_default? && @omit.member?(:default)
  false
end
preserve?(parameter) click to toggle source
# File lib/params_ready/format.rb, line 82
def preserve?(parameter)
  !omit?(parameter)
end
remap?() click to toggle source
# File lib/params_ready/format.rb, line 57
def remap?
  @remap
end
standard?() click to toggle source
# File lib/params_ready/format.rb, line 53
def standard?
  @naming_scheme == :standard
end
update(**opts) click to toggle source
# File lib/params_ready/format.rb, line 90
def update(**opts)
  opts = instance_variables.reject { |ivar| ivar == :@hash }.map do |ivar|
    value = instance_variable_get(ivar)
    name = ivar.to_s.gsub(/^@/, '').to_sym
    [name, value]
  end.to_h.merge(opts)

  Format.new(**opts)
end