class ParamsReady::Parameter::AbstractParameter

Attributes

definition[R]

Public Class Methods

intent_for_children(method, &block) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 112
def self.intent_for_children(method, &block)
  case method
  when :restriction
    raise ParamsReadyError, "Block unexpected for '#{method}' method" unless block.nil?
    define_method :intent_for_children do |intent|
      intent.for_children(self)
    end
  when :delegate
    define_method :intent_for_children do |intent|
      delegate_name, *others = self.instance_eval(&block)
      intent.delegate(self, delegate_name, *others)
    end
  when :pass
    raise ParamsReadyError, "Block unexpected for '#{method}' method" unless block.nil?
    define_method :intent_for_children do |intent|
      intent
    end
  else
    raise ParamsReadyError, "Unimplemented permission method: '#{method}'"
  end
end
new(definition, **options) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 136
def initialize(definition, **options)
  raise ParamsReadyError, "Unexpected options: #{options}" unless options.empty?
  @definition = definition
end

Public Instance Methods

==(other) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 176
def ==(other)
  return false unless self.match?(other)

  bare_value == other.bare_value
rescue
  false
end
dup() click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 196
def dup
  clone = definition.create
  populate_other clone
  clone
end
inspect() click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 190
def inspect
  preserve = Format.instance(:inspect).preserve?(self)
  content = preserve ? inspect_content : '[FILTERED]'
  "#{self.class.name.split("::").last} #{self.name}: { #{content} }"
end
match?(other) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 171
def match?(other)
  return false unless other.instance_of?(self.class)
  definition == other.definition
end
populate(context, validator) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 156
def populate(context, validator)
  return if definition.populator.nil?

  definition.populator.call(context, self)
  validator
rescue => error
  populator_error = PopulatorError.new(error)
  if validator.nil?
    raise populator_error
  else
    validator.error! populator_error
  end
  validator
end
to_hash(format = Format.instance(:backend), restriction: nil, data: nil) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 184
def to_hash(format = Format.instance(:backend), restriction: nil, data: nil)
  restriction ||= Restriction.blanket_permission
  intent = Intent.new(format, restriction, data: data)
  to_hash_if_eligible(intent) || {}
end
update_if_applicable(value, path) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 146
def update_if_applicable(value, path)
  if path.empty?
    update_self(value)
  elsif respond_to? :update_child
    update_child(value, path)
  else
    raise ParamsReadyError, "Expected path to be terminated in '#{name}'"
  end
end
update_in(value, path) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 141
def update_in(value, path)
  _, updated = update_if_applicable(value, path)
  updated
end

Protected Instance Methods

update_self(value) click to toggle source
# File lib/params_ready/parameter/parameter.rb, line 204
def update_self(value)
  clone = definition.create
  clone.set_value value
  clone.freeze if frozen?
  [true, clone]
end