Class: SimpleParams::Params

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Validations, ActiveModel::Validations::Callbacks, HasAttributes, HasTypedParams, RailsHelpers, StrictParams, Validations
Defined in:
lib/simple_params/params.rb

Direct Known Subclasses

NestedParams

Constant Summary

Constants included from HasTypedParams

HasTypedParams::TYPES

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#valid?, #validate!

Constructor Details

#initialize(params = {}) ⇒ Params

Returns a new instance of Params.



99
100
101
102
103
104
105
106
107
108
# File 'lib/simple_params/params.rb', line 99

def initialize(params={})
  set_strictness
  params = InitializationHash.new(params)
  @original_params = params.original_params
  define_attributes(@original_params)

  # Nested Classes
  @nested_classes = nested_classes.keys
  set_accessors(params)
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/simple_params/params.rb', line 17

def options
  @options
end

Instance Attribute Details

#original_paramsObject Also known as: original_hash, raw_params

Returns the value of attribute original_params.



95
96
97
# File 'lib/simple_params/params.rb', line 95

def original_params
  @original_params
end

Class Method Details

.add_validations(name, opts = {}) ⇒ Object



32
33
34
35
# File 'lib/simple_params/params.rb', line 32

def add_validations(name, opts = {})
  validations = ValidationBuilder.new(opts).build
  validates name, validations unless validations.empty?
end

.api_pie_documentationObject



23
24
25
# File 'lib/simple_params/params.rb', line 23

def api_pie_documentation
  SimpleParams::ApiPieDoc.new(self).build
end

.model_nameObject



19
20
21
# File 'lib/simple_params/params.rb', line 19

def model_name
  ActiveModel::Name.new(self)
end

.nested_array(name, opts = {}, &block) ⇒ Object



56
57
58
59
# File 'lib/simple_params/params.rb', line 56

def nested_array(name, opts={}, &block)
  klass = NestedParams.define_new_array_class(self, name, opts, &block)
  add_nested_class(name, klass, opts)
end

.nested_arraysObject



45
46
47
# File 'lib/simple_params/params.rb', line 45

def nested_arrays
  nested_classes.select { |key, klass| klass.array? }
end

.nested_classesObject



37
38
39
# File 'lib/simple_params/params.rb', line 37

def nested_classes
  @nested_classes ||= {}
end

.nested_hash(name, opts = {}, &block) ⇒ Object Also known as: nested_param, nested



49
50
51
52
# File 'lib/simple_params/params.rb', line 49

def nested_hash(name, opts={}, &block)
  klass = NestedParams.define_new_hash_class(self, name, opts, &block)
  add_nested_class(name, klass, opts)
end

.nested_hashesObject



41
42
43
# File 'lib/simple_params/params.rb', line 41

def nested_hashes
  nested_classes.select { |key, klass| klass.hash? }
end

.param(name, opts = {}) ⇒ Object



27
28
29
30
# File 'lib/simple_params/params.rb', line 27

def param(name, opts={})
  define_attribute(name, opts)
  add_validations(name, opts)
end

Instance Method Details

#define_attributes(params) ⇒ Object



110
111
112
113
114
# File 'lib/simple_params/params.rb', line 110

def define_attributes(params)
  self.class.defined_attributes.each_pair do |key, opts|
    send("#{key}_attribute=", Attribute.new(self, key, opts))
  end
end

#errorsObject



120
121
122
123
124
125
126
# File 'lib/simple_params/params.rb', line 120

def errors
  nested_class_hash = {}
  @nested_classes.each do |param|
    nested_class_hash[param.to_sym] = send(param)
  end
  @errors ||= SimpleParams::Errors.new(self, nested_class_hash)
end

#to_hashObject



116
117
118
# File 'lib/simple_params/params.rb', line 116

def to_hash
  HashBuilder.new(self).build
end