Class: MetaContent::Sanitizer
- Inherits:
-
Object
- Object
- MetaContent::Sanitizer
- Defined in:
- lib/meta_content/sanitizer.rb
Defined Under Namespace
Classes: Change
Instance Method Summary collapse
- #from_database(raw_results) ⇒ Object
-
#initialize(record) ⇒ Sanitizer
constructor
A new instance of Sanitizer.
- #sanitize(value, type) ⇒ Object
- #to_database(changes) ⇒ Object
Constructor Details
#initialize(record) ⇒ Sanitizer
Returns a new instance of Sanitizer.
4 5 6 |
# File 'lib/meta_content/sanitizer.rb', line 4 def initialize(record) @record = record end |
Instance Method Details
#from_database(raw_results) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/meta_content/sanitizer.rb', line 8 def from_database(raw_results) sanitized_results = HashWithIndifferentAccess.new raw_results.each do |namespace, results| results.each do |k,v| = schema[namespace] || {} = [k] next unless sanitized_results[namespace] ||= {} sanitized_results[namespace][k] = sanitize_from_database(v, [:type] || :string) end end sanitized_results end |
#sanitize(value, type) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/meta_content/sanitizer.rb', line 37 def sanitize(value, type) return nil if value.nil? # simulate writing and reading from database change = sanitize_to_database(value, type) sanitize_from_database(change.value, type) end |
#to_database(changes) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/meta_content/sanitizer.rb', line 22 def to_database(changes) sanitized_results = HashWithIndifferentAccess.new changes.each do |namespace, namespaced_changes| namespaced_changes.map do |k,v| = schema[namespace] || {} = [k] next unless sanitized_results[namespace] ||= {} sanitized_results[namespace][k] = sanitize_to_database(v, [:type] || :string) end end sanitized_results end |