Class: ParamsFor::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/params_for/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

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



30
31
32
33
34
# File 'lib/params_for/base.rb', line 30

def initialize(params = {})
  params.each do |key, value|
    attribute?(key) && send("#{key}=", value)
  end
end

Class Method Details

.attr_accessor(*vars) ⇒ Object

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



10
11
12
13
14
# File 'lib/params_for/base.rb', line 10

def self.attr_accessor(*vars)
  @attributes ||= []
  @attributes.concat vars
  super(*vars)
end

.attributesArray(Symbols)

Accessor method to the memoized attrubutes setted by the attr_accessor method

Returns:

  • (Array(Symbols))


19
20
21
22
23
24
# 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

Instance Method Details

#attributesArray(Symbol)

Alias for the attributes class method

Returns:

  • (Array(Symbol))


39
40
41
# File 'lib/params_for/base.rb', line 39

def attributes
  self.class.attributes
end

#to_paramsHashWithIndifferentAccess

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

Returns:

  • (HashWithIndifferentAccess)


47
48
49
# File 'lib/params_for/base.rb', line 47

def to_params
  ::HashWithIndifferentAccess.new(attributes_hash)
end