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
-
.clone_schemas(parent) ⇒ Object
-
.define_type_method(type_name) ⇒ 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
-
.schemas ⇒ Object
Instance Method Summary
collapse
included
#array?, #coerce, #default, #normalize, #normalize_proc, #read_default
Constructor Details
#initialize(params = {}) ⇒ Base
Returns a new instance of Base.
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/easy_params/base.rb', line 13
def initialize(params = {})
self.class.schema.each do |attr, type|
coerced_type = type.coerce(params.to_h[attr])
if coerced_type && (type.is_a?(EasyParams::Base) || type.is_a?(EasyParams::Types::StructsCollection))
coerced_type.owner = self
end
coerced_type.each { |v| v.owner = coerced_type } if coerced_type.is_a?(EasyParams::Types::StructsCollection)
public_send("#{attr}=", coerced_type)
end
end
|
Instance Attribute Details
#default=(value) ⇒ Object
Sets the attribute default
11
12
13
|
# File 'lib/easy_params/base.rb', line 11
def default=(value)
@default = value
end
|
Class Method Details
.array(param_name, of:, default: nil, normalize: nil, **validations) ⇒ Object
73
74
75
76
77
78
|
# File 'lib/easy_params/base.rb', line 73
def array(param_name, of:, default: nil, normalize: nil, **validations)
validates param_name, **validations if validations.any?
type = EasyParams::Types::Array.of(EasyParams.types[of])
type = customize_type(type, default, &normalize)
attribute(param_name, type)
end
|
.attribute(param_name, type) ⇒ Object
39
40
41
42
43
|
# File 'lib/easy_params/base.rb', line 39
def attribute(param_name, type)
attr_accessor param_name
schema[param_name] = type
end
|
.clone_schema(parent) ⇒ Object
80
81
82
|
# File 'lib/easy_params/base.rb', line 80
def clone_schema(parent)
@schema = parent.schema.dup
end
|
.clone_schemas(parent) ⇒ Object
84
85
86
|
# File 'lib/easy_params/base.rb', line 84
def clone_schemas(parent)
@schemas = parent.schemas.dup
end
|
.define_type_method(type_name) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/easy_params/base.rb', line 88
def define_type_method(type_name)
define_singleton_method(type_name) do |param_name, default: nil, normalize: nil, **validations|
validates param_name, **validations if validations.any?
type = customize_type(EasyParams.types[type_name], default, &normalize)
attribute(param_name, type)
end
end
|
.each(param_name, definition = nil, default: nil, normalize: nil, **validations, &block) ⇒ Object
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/easy_params/base.rb', line 49
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
handle_schema_definition(param_name, definition, &block)
type = EasyParams::Types::Each.of(schemas[param_name].new)
type = customize_type(type, default, &normalize)
attribute(param_name, type)
end
|
.has(param_name, definition = nil, default: nil, normalize: nil, **validations, &block) ⇒ Object
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/easy_params/base.rb', line 61
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
handle_schema_definition(param_name, definition, &block)
type = schemas[param_name].new
type = customize_type(type, default, &normalize)
attribute(param_name, type)
end
|
.inherited(subclass) ⇒ Object
25
26
27
28
29
|
# File 'lib/easy_params/base.rb', line 25
def inherited(subclass)
super
subclass.clone_schema(self)
subclass.clone_schemas(self)
end
|
.name ⇒ Object
35
36
37
|
# File 'lib/easy_params/base.rb', line 35
def name
'EasyParams::Base'
end
|
.schema ⇒ Object
45
46
47
|
# File 'lib/easy_params/base.rb', line 45
def schema
@schema ||= {}
end
|
.schemas ⇒ Object
31
32
33
|
# File 'lib/easy_params/base.rb', line 31
def schemas
@schemas ||= {}
end
|
Instance Method Details
#attributes ⇒ Object
124
125
126
|
# File 'lib/easy_params/base.rb', line 124
def attributes
self.class.schema.to_h { |k, type| [k, type.array? ? send(k).to_a : send(k)] }
end
|
#to_h ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/easy_params/base.rb', line 132
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
|