Class: Schemas::Base
Instance Attribute Summary
Attributes included from Dry::Schema
#attributes, #errors
Instance Method Summary
collapse
#[], #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
26
27
28
|
# File 'app/schemas/base.rb', line 26
def clear_html(value)
Rails::Html::WhiteListSanitizer.new.sanitize(value)
end
|
#forced_array(value) ⇒ Object
54
55
56
|
# File 'app/schemas/base.rb', line 54
def forced_array(value)
[value.present? ? value : nil].flatten.compact
end
|
#forced_hash(value) ⇒ Object
59
60
61
|
# File 'app/schemas/base.rb', line 59
def forced_hash(value)
value.blank? ? {} : value
end
|
#normalize_integer(value) ⇒ Object
8
9
10
11
|
# File 'app/schemas/base.rb', line 8
def normalize_integer(value)
return nil if value.blank?
Integer(value.to_s.gsub(/\s/, '')) rescue value
end
|
#normalize_url(value) ⇒ Object
31
32
33
34
|
# File 'app/schemas/base.rb', line 31
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
64
65
66
|
# File 'app/schemas/base.rb', line 64
def nullify_empty(value)
value.present? ? value : nil
end
|
#strip_string(value, empty_to_nil = true) ⇒ Object
14
15
16
17
18
|
# File 'app/schemas/base.rb', line 14
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
48
49
50
51
|
# File 'app/schemas/base.rb', line 48
def to_big_decimal(value)
return unless value.present?
BigDecimal.new(value.to_s.gsub(/\s/, '')) rescue value
end
|
#to_bool(value) ⇒ Object
37
38
39
|
# File 'app/schemas/base.rb', line 37
def to_bool(value)
value.to_s.to_bool
end
|
#to_date(value) ⇒ Object
21
22
23
|
# File 'app/schemas/base.rb', line 21
def to_date(value)
value.to_date rescue value
end
|
#to_float(value) ⇒ Object
42
43
44
45
|
# File 'app/schemas/base.rb', line 42
def to_float(value)
return unless value.present?
BigDecimal.new(value.to_s.gsub(/\s/, '')) rescue value
end
|