Class: SimpleParams::Params

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

Constant Summary collapse

TYPES =
[
  :integer,
  :string,
  :decimal,
  :datetime,
  :date,
  :time,
  :float,
  :boolean,
  :array,
  :hash,
  :object
]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validations

#valid?, #validate!

Constructor Details

#initialize(params = {}, parent = nil) ⇒ Params

Returns a new instance of Params.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/simple_params/params.rb', line 130

def initialize(params={}, parent = nil)
  # Set default strict params
  if self.class.strict_enforcement.nil?
    self.class.strict_enforcement = true
  end

  @parent = parent
  # Initializing Params
  @original_params = hash_to_symbolized_hash(params)
  define_attributes(@original_params)

  # Nested Hashes
  @nested_params = nested_hashes.keys

  # Nested Arrays
  @nested_arrays = nested_arrays.keys

  # Nested Classes
  set_accessors(params)
  initialize_nested_classes
  initialize_nested_array_classes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

Overriding this method to allow for non-strict enforcement!



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/simple_params/params.rb', line 204

def method_missing(method_name, *arguments, &block)
  if strict_enforcement?
    raise SimpleParamsError, "parameter #{method_name} is not defined."
  else
    if @original_params.include?(method_name.to_sym)
      value = @original_params[method_name.to_sym]
      if value.is_a?(Hash)
        define_anonymous_class(method_name, value)
      else
        Attribute.new(self, method_name).value = value
      end
    end
  end
end

Class Attribute Details

.optionsObject

Returns the value of attribute options.



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

def options
  @options
end

.strict_enforcementObject

Returns the value of attribute strict_enforcement.



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

def strict_enforcement
  @strict_enforcement
end

Class Method Details

.allow_undefined_paramsObject



47
48
49
# File 'lib/simple_params/params.rb', line 47

def allow_undefined_params
  @strict_enforcement = false
end

.api_pie_documentationObject



39
40
41
# File 'lib/simple_params/params.rb', line 39

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

.defined_attributesObject



80
81
82
# File 'lib/simple_params/params.rb', line 80

def defined_attributes
  @define_attributes ||= {}
end

.model_nameObject



35
36
37
# File 'lib/simple_params/params.rb', line 35

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

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



69
70
71
72
73
74
# File 'lib/simple_params/params.rb', line 69

def nested_array(name, opts={}, &block)
  attr_accessor name
  nested_array_class = define_nested_class(name, opts, &block)
  @nested_arrays ||= {}
  @nested_arrays[name.to_sym] = nested_array_class
end

.nested_arraysObject



76
77
78
# File 'lib/simple_params/params.rb', line 76

def nested_arrays
  @nested_arrays ||= {}
end

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



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

def nested_hash(name, opts={}, &block)
  attr_accessor name
  nested_class = define_nested_class(name, opts, &block)
  @nested_hashes ||= {}
  @nested_hashes[name.to_sym] = nested_class
end

.nested_hashesObject



65
66
67
# File 'lib/simple_params/params.rb', line 65

def nested_hashes
  @nested_hashes ||= {}
end

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



51
52
53
54
# File 'lib/simple_params/params.rb', line 51

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

.strictObject



43
44
45
# File 'lib/simple_params/params.rb', line 43

def strict
  @strict_enforcement = true
end

Instance Method Details

#attributesObject



159
160
161
# File 'lib/simple_params/params.rb', line 159

def attributes
  (defined_attributes.keys + nested_hashes.keys + nested_arrays.keys).flatten
end

#define_attributes(params) ⇒ Object



153
154
155
156
157
# File 'lib/simple_params/params.rb', line 153

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

#errorsObject



189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/simple_params/params.rb', line 189

def errors
  nested_errors_hash = {}
  @nested_params.each do |param|
    nested_errors_hash[param.to_sym] = send(param).errors
  end

  nested_arrays_hash = {}
  @nested_arrays.each do |array|
    nested_arrays_hash[array.to_sym] = send(array).map(&:errors)
  end

  @errors ||= SimpleParams::Errors.new(self, nested_errors_hash, nested_arrays_hash)
end

#original_paramsObject Also known as: original_hash, raw_params



163
164
165
# File 'lib/simple_params/params.rb', line 163

def original_params
  @original_params ||= {}
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


219
220
221
222
223
224
225
# File 'lib/simple_params/params.rb', line 219

def respond_to?(method_name, include_private = false)
  if strict_enforcement?
    super
  else
    @original_params.include?(method_name.to_sym) || super
  end
end

#to_hashObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/simple_params/params.rb', line 169

def to_hash
  hash = {}
  attributes.each do |attribute|
    raw_attribute = send(attribute)
    if raw_attribute.is_a?(SimpleParams::Params)
      hash[attribute] = send(attribute).to_hash
    elsif raw_attribute.is_a?(Array)
      attribute_array = []
      raw_attribute.each do |r_attr|
        attribute_array << r_attr.to_hash
      end
      hash[attribute] = attribute_array
    else
      hash[attribute] = send(attribute)
    end
  end

  hash
end