class ParamsFor::Base

Public Class Methods

attr_accessor(*vars) click to toggle source

Memoizes the accessor atrributes in the @attributes variable to be able to list them and access them

Calls superclass method
# File lib/params_for/base.rb, line 10
def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end
attributes() click to toggle source

Accessor method to the memoized attrubutes setted by the attr_accessor method

@return [Array(Symbols)]

# File lib/params_for/base.rb, line 19
def self.attributes
  @attributes ||= []

  super_attributes = superclass.attributes if superclass && superclass.respond_to?(:attributes)
  (super_attributes || []) + @attributes
end
new(params = {}) click to toggle source

Initializes the param validator object with the given controller params HashwithIndifferentAccess object and feeds any defined attribute with the given param key if they exists

# File lib/params_for/base.rb, line 30
def initialize(params = {})
  params.each do |key, value|
    attribute?(key) && send("#{key}=", value)
  end
end

Public Instance Methods

attributes() click to toggle source

Alias for the attributes class method

@return [Array(Symbol)]

# File lib/params_for/base.rb, line 39
def attributes
  self.class.attributes
end
to_params() click to toggle source

Returns the given attibutes validated and parsed if needed to be used in the controller

@return [HashWithIndifferentAccess]

# File lib/params_for/base.rb, line 47
def to_params
  ::HashWithIndifferentAccess.new(attributes_hash)
end

Private Instance Methods

attribute?(key) click to toggle source

Whenever the given they is a valid attribute or not

@return [Boolean]

# File lib/params_for/base.rb, line 56
def attribute?(key)
  self.class.attributes.include? key.to_sym
end
attributes_hash() click to toggle source

Return a hash with the attributes and values that will be sent to the controller

@return [Hash] {attribute1: value, attribute2: value}

# File lib/params_for/base.rb, line 76
def attributes_hash
  settled_attributes.inject({}) do |out, attribute|
    value = public_send(attribute)
    out[attribute] = value
    out
  end
end
settled_attributes() click to toggle source

Return the attributes of the validator filtered by attributes has been set in the initializer with params

@return [Array<Symbol>]

# File lib/params_for/base.rb, line 64
def settled_attributes
  instance_vars = self.instance_variables.map do |attr|
    attr.to_s[1..-1].to_sym
  end

  instance_vars & attributes
end