Class: EasyParams::Base

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, Types::Struct, Validation
Defined in:
lib/easy_params/base.rb

Overview

Implements validations logic and nesting structures

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::Struct

#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 (writeonly)

Sets the attribute default

Parameters:

  • value

    the value to set the attribute default to.



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



47
48
49
50
51
52
# File 'lib/easy_params/base.rb', line 47

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



23
24
25
26
27
# File 'lib/easy_params/base.rb', line 23

def attribute(param_name, type)
  attr_accessor param_name

  schema[param_name] = type
end

.each(param_name, default: nil, normalize: nil, **validations, &block) ⇒ Object



33
34
35
36
37
38
# File 'lib/easy_params/base.rb', line 33

def each(param_name, default: nil, normalize: nil, **validations, &block)
  validates param_name, **validations if validations.any?
  type = EasyParams::Types::Each.with_type(&block)
  type = customize_type(type, default, &normalize)
  attribute(param_name, type)
end

.has(param_name, default: nil, normalize: nil, **validations, &block) ⇒ Object



40
41
42
43
44
45
# File 'lib/easy_params/base.rb', line 40

def has(param_name, default: nil, normalize: nil, **validations, &block)
  validates param_name, **validations if validations.any?
  type = Class.new(EasyParams::Types::Struct.class).tap { |c| c.class_eval(&block) }.new
  type = customize_type(type, default, &normalize)
  attribute(param_name, type)
end

.nameObject



19
20
21
# File 'lib/easy_params/base.rb', line 19

def name
  'EasyParams::Base'
end

.schemaObject



29
30
31
# File 'lib/easy_params/base.rb', line 29

def schema
  @schema ||= {}
end

Instance Method Details

#attributesObject



73
74
75
# File 'lib/easy_params/base.rb', line 73

def attributes
  self.class.schema.to_h { |k, type| [k, type.array? ? send(k).to_a : send(k)] }
end

#to_hObject



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/easy_params/base.rb', line 81

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::Types::Struct.class
                    value.to_h
                  else
                    value
                  end
  end
end