Class: EasyParams::Base
Overview
Implements validations logic and nesting structures
Instance Attribute Summary collapse
Class Method Summary
collapse
-
.array(param_name, of:, default: nil, normalize: nil, **validations) ⇒ Object
-
.attribute(param_name, type) ⇒ Object
-
.clone_schema(parent) ⇒ Object
-
.each(param_name, definition = nil, default: nil, normalize: nil, **validations, &block) ⇒ Object
-
.has(param_name, definition = nil, default: nil, normalize: nil, **validations, &block) ⇒ Object
-
.inherited(subclass) ⇒ Object
-
.name ⇒ Object
-
.schema ⇒ Object
Instance Method Summary
collapse
#array?, #coerce, #default
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
12
13
14
15
16
|
# File 'lib/easy_params/base.rb', line 12
def initialize(params = {})
self.class.schema.each do |attr, type|
public_send("#{attr}=", type.coerce(params.to_h[attr]))
end
end
|
Instance Attribute Details
#default=(value) ⇒ Object
Sets the attribute default
10
11
12
|
# File 'lib/easy_params/base.rb', line 10
def default=(value)
@default = value
end
|
Class Method Details
.array(param_name, of:, default: nil, normalize: nil, **validations) ⇒ Object
60
61
62
63
64
65
|
# File 'lib/easy_params/base.rb', line 60
def array(param_name, of:, default: nil, normalize: nil, **validations)
validates param_name, **validations if validations.any?
type = EasyParams::Types::Array.of(EasyParams::Types.const_get(of.to_s.camelcase))
type = customize_type(type, default, &normalize)
attribute(param_name, type)
end
|
.attribute(param_name, type) ⇒ Object
28
29
30
31
32
|
# File 'lib/easy_params/base.rb', line 28
def attribute(param_name, type)
attr_accessor param_name
schema[param_name] = type
end
|
.clone_schema(parent) ⇒ Object
67
68
69
|
# File 'lib/easy_params/base.rb', line 67
def clone_schema(parent)
@schema = parent.schema.dup
end
|
.each(param_name, definition = nil, default: nil, normalize: nil, **validations, &block) ⇒ Object
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/easy_params/base.rb', line 38
def each(param_name, definition = nil, default: nil, normalize: nil, **validations, &block)
validates param_name, **validations if validations.any?
if definition && !(definition < EasyParams::Base)
raise ArgumentError, "definition for attribute #{param_name.inspect} must be a subclass of EasyParams::Base"
end
type = EasyParams::Types::Each.with_type(definition, &block)
type = customize_type(type, default, &normalize)
attribute(param_name, type)
end
|
.has(param_name, definition = nil, default: nil, normalize: nil, **validations, &block) ⇒ Object
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/easy_params/base.rb', line 49
def has(param_name, definition = nil, default: nil, normalize: nil, **validations, &block)
validates param_name, **validations if validations.any?
if definition && !(definition < EasyParams::Base)
raise ArgumentError, "definition for attribute #{param_name.inspect} must be a subclass of EasyParams::Base"
end
type = (definition || Class.new(EasyParams::Base).tap { |c| c.class_eval(&block) }).new
type = customize_type(type, default, &normalize)
attribute(param_name, type)
end
|
.inherited(subclass) ⇒ Object
19
20
21
22
|
# File 'lib/easy_params/base.rb', line 19
def inherited(subclass)
super
subclass.clone_schema(self)
end
|
.name ⇒ Object
24
25
26
|
# File 'lib/easy_params/base.rb', line 24
def name
'EasyParams::Base'
end
|
.schema ⇒ Object
34
35
36
|
# File 'lib/easy_params/base.rb', line 34
def schema
@schema ||= {}
end
|
Instance Method Details
#attributes ⇒ Object
90
91
92
|
# File 'lib/easy_params/base.rb', line 90
def attributes
self.class.schema.to_h { |k, type| [k, type.array? ? send(k).to_a : send(k)] }
end
|
#to_h ⇒ Object
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/easy_params/base.rb', line 98
def to_h
attributes.each.with_object({}) do |(key, value), result|
result[key] = case value
when EasyParams::Types::StructsCollection
value.map(&:to_h)
when EasyParams::Base
value.to_h
else
value
end
end
end
|