read.rb 5.1 KB
Newer Older
J
Joshua Peek 已提交
1 2 3 4 5
module ActiveRecord
  module AttributeMethods
    module Read
      extend ActiveSupport::Concern

6 7
      ATTRIBUTE_TYPES_CACHED_BY_DEFAULT = [:datetime, :timestamp, :time, :date]

J
Joshua Peek 已提交
8
      included do
9 10
        cattr_accessor :attribute_types_cached_by_default, :instance_writer => false
        self.attribute_types_cached_by_default = ATTRIBUTE_TYPES_CACHED_BY_DEFAULT
J
Joshua Peek 已提交
11
      end
J
Joshua Peek 已提交
12 13

      module ClassMethods
14 15 16 17
        # +cache_attributes+ allows you to declare which converted attribute values should
        # be cached. Usually caching only pays off for attributes with expensive conversion
        # methods, like time related columns (e.g. +created_at+, +updated_at+).
        def cache_attributes(*attribute_names)
18
          cached_attributes.merge attribute_names.map { |attr| attr.to_s }
19 20 21 22 23
        end

        # Returns the attributes which are cached. By default time related columns
        # with datatype <tt>:datetime, :timestamp, :time, :date</tt> are cached.
        def cached_attributes
J
Jeremy Kemper 已提交
24
          @cached_attributes ||= columns.select { |c| cacheable_column?(c) }.map { |col| col.name }.to_set
25 26 27 28 29 30 31
        end

        # Returns +true+ if the provided attribute is being cached.
        def cache_attribute?(attr_name)
          cached_attributes.include?(attr_name)
        end

32 33 34 35
        def undefine_attribute_methods
          if base_class == self
            generated_attribute_methods.module_eval do
              public_methods(false).each do |m|
36
                singleton_class.send(:undef_method, m) if m.to_s =~ /^attribute_/
37 38 39 40 41 42 43
              end
            end
          end

          super
        end

J
Joshua Peek 已提交
44
        protected
45 46 47 48 49 50
          # Where possible, generate the method by evalling a string, as this will result in
          # faster accesses because it avoids the block eval and then string eval incurred
          # by the second branch.
          #
          # The second, slower, branch is necessary to support instances where the database
          # returns columns with extra stuff in (like 'my_column(omg)').
51
          def define_method_attribute(attr_name)
52 53
            internal = internal_attribute_access_code(attr_name)
            external = external_attribute_access_code(attr_name)
54 55 56 57

            if attr_name =~ ActiveModel::AttributeMethods::NAME_COMPILABLE_REGEXP
              generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__
                def #{attr_name}
58
                  #{internal}
59 60
                end

61
                def self.attribute_#{attr_name}(v, attributes, attributes_cache, attr_name)
62
                  #{external}
63 64 65 66 67
                end
              STR
            else
              generated_attribute_methods.module_eval do
                define_method(attr_name) do
68
                  eval(internal)
69 70
                end

71
                singleton_class.send(:define_method, "attribute_#{attr_name}") do |v, attributes, attributes_cache, attr_name|
72
                  eval(external)
73 74 75
                end
              end
            end
J
Joshua Peek 已提交
76 77 78
          end

        private
J
Jeremy Kemper 已提交
79
          def cacheable_column?(column)
80
            attribute_types_cached_by_default.include?(column.type)
81
          end
J
Joshua Peek 已提交
82

83
          def internal_attribute_access_code(attr_name)
84
            access_code = "(v=@attributes['#{attr_name}']) && #{attribute_cast_code(attr_name)}"
85

86
            unless attr_name == self.primary_key
87
              access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
J
Joshua Peek 已提交
88 89 90 91 92
            end

            if cache_attribute?(attr_name)
              access_code = "@attributes_cache['#{attr_name}'] ||= (#{access_code})"
            end
93

94 95
            access_code
          end
96

97 98 99 100 101 102 103 104 105 106
          def external_attribute_access_code(attr_name)
            access_code = "v && #{attribute_cast_code(attr_name)}"

            if cache_attribute?(attr_name)
              access_code = "attributes_cache[attr_name] ||= (#{access_code})"
            end

            access_code
          end

107 108
          def attribute_cast_code(attr_name)
            columns_hash[attr_name].type_cast_code('v')
J
Joshua Peek 已提交
109 110 111 112 113 114
          end
      end

      # Returns the value of the attribute identified by <tt>attr_name</tt> after it has been typecast (for example,
      # "2004-12-12" in a data column is cast to a date object, like Date.new(2004, 12, 12)).
      def read_attribute(attr_name)
115
        attr_name = attr_name.to_s
116
        accessor  = "attribute_#{attr_name}"
117 118
        methods   = self.class.generated_attribute_methods

119
        if methods.respond_to?(accessor)
120 121
          if @attributes.has_key?(attr_name) || attr_name == 'id'
            methods.send(accessor, @attributes[attr_name], @attributes, @attributes_cache, attr_name)
122
          end
123 124 125 126 127
        elsif !self.class.attribute_methods_generated?
          # If we haven't generated the caster methods yet, do that and
          # then try again
          self.class.define_attribute_methods
          read_attribute(attr_name)
128
        else
129 130 131
          # If we get here, the attribute has no associated DB column, so
          # just return it verbatim.
          @attributes[attr_name]
J
Jon Leighton 已提交
132 133 134 135
        end
      end

      private
J
Joshua Peek 已提交
136 137 138
        def attribute(attribute_name)
          read_attribute(attribute_name)
        end
J
Joshua Peek 已提交
139 140 141
    end
  end
end