Sha256: 92ac1af0cd00297af7eb789770b0c8f749b7c6a4eb4bf4aedd8689272f272c20

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

module ActiveFedora
  module AttributeMethods
    module Read
      module ClassMethods
        protected

          def define_method_attribute(name, owner: nil) # rubocop:disable Lint/UnusedMethodArgument
            name = name.to_s
            safe_name = name.unpack('h*'.freeze).first
            temp_method = "__temp__#{safe_name}"

            ActiveFedora::AttributeMethods::AttrNames.set_name_cache safe_name, name

            generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
              def #{temp_method}
                name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{safe_name}
                _read_attribute(name) { |n| missing_attribute(n, caller) }
              end
            STR

            generated_attribute_methods.module_eval do
              alias_method name, temp_method
              undef_method temp_method
            end
          end
      end

      extend ActiveSupport::Concern

      # Returns the value of the attribute identified by <tt>attr_name</tt> after
      # it has been typecast (for example, "2004-12-12" in a date column is cast
      # to a date object, like Date.new(2004, 12, 12)).
      def read_attribute(attr_name, &block)
        name = attr_name.to_s
        _read_attribute(name, &block)
      end

      def _read_attribute(attr_name) # :nodoc:
        attributes.fetch(attr_name.to_s) { |n| yield n if block_given? }
      end

      alias attribute _read_attribute
      private :attribute
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active-fedora-14.0.1 lib/active_fedora/attribute_methods/read.rb
active-fedora-14.0.0 lib/active_fedora/attribute_methods/read.rb