Sha256: 10a18fb35a68b791236aaf9f8fad0039ad292f7b32c5ea051e8869a6e38a3417

Contents?: true

Size: 1.48 KB

Versions: 13

Compression:

Stored size: 1.48 KB

Contents

module Tdc
  module ExtendedAttributes
    #
    # Know how on interpret extended attributes.
    #
    # Note: extended attribute keys are expected to be strings rather than symbols.
    #
    class DefaultInterpreter < Tdc::ExtendedAttributes::InterpreterBase
      def interpret(instance_definition)
        extended_attribute_definitions = keep_extended_attributes(instance_definition)

        extended_attribute_definitions.each do |extended_attribute_key, extended_attribute_value|
          # Remove the extended attribute.
          instance_definition.delete(extended_attribute_key)

          # Add the extended attribute back as a standard attribute.
          instance_definition[convert_to_standard_attribute(extended_attribute_key)] =
            extended_attribute_context.instance_eval(extended_attribute_value)
        end
      end

      concerning :HookMethods do
        def extended_attribute_context
          Time.zone
        end
      end

      private

      EXTENDED_ATTRIBUTE_SUFFIX = "_xa"

      def convert_to_standard_attribute(extended_attribute_key)
        extended_attribute_key.delete_suffix(EXTENDED_ATTRIBUTE_SUFFIX)
      end

      def extended_attribute?(extended_attribute_key)
        extended_attribute_key.end_with?(EXTENDED_ATTRIBUTE_SUFFIX)
      end

      def keep_extended_attributes(instance_definition)
        instance_definition.select do |extended_attribute_key, _|
          extended_attribute?(extended_attribute_key)
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
tdc-1.0 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.6.2 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.6.1 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.6.0 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.5.0 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.7 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.6.1 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.6 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.4.1 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.4 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.3.1 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.3 lib/tdc/extended_attributes/default_interpreter.rb
tdc-0.4.2 lib/tdc/extended_attributes/default_interpreter.rb