Class: Schemas::Base

Inherits:
Object
  • Object
show all
Includes:
Dry::Schema
Defined in:
app/getboat/schemas/base.rb

Instance Attribute Summary

Attributes included from Dry::Schema

#attributes, #errors

Instance Method Summary collapse

Methods included from Dry::Schema

#[], #initialize, #schema, #valid?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Dry::Schema

Instance Method Details

#clear_html(value) ⇒ Object



24
25
26
# File 'app/getboat/schemas/base.rb', line 24

def clear_html(value)
  Rails::Html::WhiteListSanitizer.new.sanitize(value)
end

#forced_array(value) ⇒ Object



52
53
54
# File 'app/getboat/schemas/base.rb', line 52

def forced_array(value)
  [value.present? ? value : nil].flatten.compact
end

#forced_hash(value) ⇒ Object



57
58
59
# File 'app/getboat/schemas/base.rb', line 57

def forced_hash(value)
  value.blank? ? {} : value
end

#normalize_integer(value) ⇒ Object



6
7
8
9
# File 'app/getboat/schemas/base.rb', line 6

def normalize_integer(value)
  return nil if value.blank?
  Integer(value.to_s.gsub(/\s/, '')) rescue value
end

#normalize_url(value) ⇒ Object



29
30
31
32
# File 'app/getboat/schemas/base.rb', line 29

def normalize_url(value)
  return if value.blank?
  value.to_s.strip.scan(/^https?:\/\//).present? ? value : sprintf('https://%s', value.to_s.strip)
end

#nullify_empty(value) ⇒ Object



62
63
64
# File 'app/getboat/schemas/base.rb', line 62

def nullify_empty(value)
  value.present? ? value : nil
end

#strip_string(value, empty_to_nil = true) ⇒ Object



12
13
14
15
16
# File 'app/getboat/schemas/base.rb', line 12

def strip_string(value, empty_to_nil = true)
  value = value.to_s.strip
  value = nil if empty_to_nil && value.blank?
  value
end

#to_big_decimal(value) ⇒ Object



46
47
48
49
# File 'app/getboat/schemas/base.rb', line 46

def to_big_decimal(value)
  return unless value.present?
  BigDecimal.new(value.to_s.gsub(/\s/, '')) rescue value
end

#to_bool(value) ⇒ Object



35
36
37
# File 'app/getboat/schemas/base.rb', line 35

def to_bool(value)
  value.to_s.to_bool
end

#to_date(value) ⇒ Object



19
20
21
# File 'app/getboat/schemas/base.rb', line 19

def to_date(value)
  value.to_date rescue value
end

#to_float(value) ⇒ Object



40
41
42
43
# File 'app/getboat/schemas/base.rb', line 40

def to_float(value)
  return unless value.present?
  BigDecimal.new(value.to_s.gsub(/\s/, '')) rescue value
end