Sha256: 1a1ee18c9e420592c9bb3ce9e1694132363c984e6f031f048a98fe5bf6833cf1

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 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_from(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 convert_to_standard_attribute(extended_attribute_key)
          extended_attribute_key.gsub(/_(at|date|on)x$/, "_\\1")
        end

        def extended_attribute_context
          Time.zone
        end

        def extended_attribute?(extended_attribute_key)
          /_(at|date|on)x$/ =~ extended_attribute_key
        end
      end

      private

      def keep_extended_attributes_from(instance_definition)
        instance_definition.select { |extended_attribute_key, _| extended_attribute?(extended_attribute_key) }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tdc-0.4.1 lib/tdc/extended_attributes/default_interpreter.rb