class RSchema::Coercers::FixedHash::RemoveExtraneousAttributes
Removes elements from `Hash` values that are not defined in the given `FixedHash` schema.
Attributes
hash_attributes[R]
Public Class Methods
build(schema)
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 13 def self.build(schema) new(schema) end
new(fixed_hash_schema)
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 17 def initialize(fixed_hash_schema) # TODO: make fixed hash attributes frozen, and eliminate dup @hash_attributes = fixed_hash_schema.attributes.map(&:dup) end
Public Instance Methods
call(value)
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 22 def call(value) Result.success(remove_extraneous_elements(value)) end
will_affect?(value)
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 26 def will_affect?(value) keys_to_remove(value).any? end
Private Instance Methods
keys_to_remove(value)
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 44 def keys_to_remove(value) if value.is_a?(Hash) value.keys - valid_keys else [] end end
remove_extraneous_elements(hash)
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 32 def remove_extraneous_elements(hash) extra_keys = keys_to_remove(hash) if extra_keys.any? hash.dup.tap do |stripped_hash| extra_keys.each { |k| stripped_hash.delete(k) } end else hash end end
valid_keys()
click to toggle source
# File lib/rschema/coercers/fixed_hash/remove_extraneous_attributes.rb, line 52 def valid_keys @valid_keys ||= hash_attributes.map(&:key) end