Class: SimpleParams::Params
- Inherits:
-
Object
- Object
- SimpleParams::Params
show all
- 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
#valid?, #validate!
Constructor Details
#initialize(params = {}, parent = nil) ⇒ Params
Returns a new instance of Params.
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/simple_params/params.rb', line 105
def initialize(params={}, parent = nil)
if self.class.strict_enforcement.nil?
self.class.strict_enforcement = true
end
@parent = parent
@original_params = hash_to_symbolized_hash(params)
define_attributes(@original_params)
@nested_params = nested_hashes.keys
@errors = SimpleParams::Errors.new(self, @nested_params)
set_accessors(params)
initialize_nested_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!
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/simple_params/params.rb', line 142
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(value)
else
Attribute.new(self, method_name).value = value
end
end
end
end
|
Class Attribute Details
.options ⇒ Object
Returns the value of attribute options.
32
33
34
|
# File 'lib/simple_params/params.rb', line 32
def options
@options
end
|
.strict_enforcement ⇒ Object
Returns the value of attribute strict_enforcement.
32
33
34
|
# File 'lib/simple_params/params.rb', line 32
def strict_enforcement
@strict_enforcement
end
|
Class Method Details
.allow_undefined_params ⇒ Object
42
43
44
|
# File 'lib/simple_params/params.rb', line 42
def allow_undefined_params
@strict_enforcement = false
end
|
.api_pie_documentation ⇒ Object
34
35
36
|
# File 'lib/simple_params/params.rb', line 34
def api_pie_documentation
SimpleParams::ApiPieDoc.new(self).build
end
|
.defined_attributes ⇒ Object
64
65
66
|
# File 'lib/simple_params/params.rb', line 64
def defined_attributes
@define_attributes ||= {}
end
|
.nested_hash(name, opts = {}, &block) ⇒ Object
Also known as:
nested_param, nested
51
52
53
54
55
56
|
# File 'lib/simple_params/params.rb', line 51
def nested_hash(name, opts={}, &block)
attr_accessor name
nested_class = define_nested_class(opts, &block)
@nested_hashes ||= {}
@nested_hashes[name.to_sym] = nested_class
end
|
.nested_hashes ⇒ Object
60
61
62
|
# File 'lib/simple_params/params.rb', line 60
def nested_hashes
@nested_hashes ||= {}
end
|
.param(name, opts = {}) ⇒ Object
46
47
48
49
|
# File 'lib/simple_params/params.rb', line 46
def param(name, opts={})
define_attribute(name, opts)
add_validations(name, opts)
end
|
.strict ⇒ Object
38
39
40
|
# File 'lib/simple_params/params.rb', line 38
def strict
@strict_enforcement = true
end
|
Instance Method Details
#attributes ⇒ Object
131
132
133
|
# File 'lib/simple_params/params.rb', line 131
def attributes
(defined_attributes.keys + nested_hashes.keys).flatten
end
|
#define_attributes(params) ⇒ Object
125
126
127
128
129
|
# File 'lib/simple_params/params.rb', line 125
def define_attributes(params)
self.class.defined_attributes.each_pair do |key, opts|
send("#{key}_attribute=", Attribute.new(self, key, opts))
end
end
|
#original_params ⇒ Object
Also known as:
original_hash, raw_params
135
136
137
|
# File 'lib/simple_params/params.rb', line 135
def original_params
@original_params ||= {}
end
|
#respond_to?(method_name, include_private = false) ⇒ Boolean
157
158
159
160
161
162
163
|
# File 'lib/simple_params/params.rb', line 157
def respond_to?(method_name, include_private = false)
if strict_enforcement?
super
else
@original_params.include?(method_name.to_sym) || super
end
end
|