attribute.rb 3.5 KB
Newer Older
1 2 3
module ActiveRecord
  class Attribute # :nodoc:
    class << self
4 5
      def from_database(name, value, type)
        FromDatabase.new(name, value, type)
6 7
      end

8 9
      def from_user(name, value, type)
        FromUser.new(name, value, type)
10
      end
11

12 13 14 15
      def with_cast_value(name, value, type)
        WithCastValue.new(name, value, type)
      end

16 17 18 19 20 21
      def null(name)
        Null.new(name)
      end

      def uninitialized(name, type)
        Uninitialized.new(name, type)
22
      end
23 24
    end

25
    attr_reader :name, :value_before_type_cast, :type
26 27 28

    # This method should not be called directly.
    # Use #from_database or #from_user
29 30
    def initialize(name, value_before_type_cast, type)
      @name = name
31 32 33 34 35 36
      @value_before_type_cast = value_before_type_cast
      @type = type
    end

    def value
      # `defined?` is cheaper than `||=` when we get back falsy values
S
Sean Griffin 已提交
37
      @value = type_cast(value_before_type_cast) unless defined?(@value)
38 39 40 41
      @value
    end

    def value_for_database
42
      type.serialize(value)
43 44
    end

45 46 47 48 49
    def changed_from?(old_value)
      type.changed?(old_value, value, value_before_type_cast)
    end

    def changed_in_place_from?(old_value)
50
      has_been_read? && type.changed_in_place?(old_value, value)
51 52
    end

53
    def with_value_from_user(value)
54
      type.assert_valid_value(value)
55
      self.class.from_user(name, value, type)
56 57 58
    end

    def with_value_from_database(value)
59
      self.class.from_database(name, value, type)
60 61
    end

62 63 64 65
    def with_cast_value(value)
      self.class.with_cast_value(name, value, type)
    end

66 67 68 69
    def with_type(type)
      self.class.new(name, value_before_type_cast, type)
    end

70
    def type_cast(*)
71 72 73
      raise NotImplementedError
    end

74 75 76 77
    def initialized?
      true
    end

78 79 80 81
    def came_from_user?
      false
    end

82 83 84 85
    def has_been_read?
      defined?(@value)
    end

86 87 88 89 90 91
    def ==(other)
      self.class == other.class &&
        name == other.name &&
        value_before_type_cast == other.value_before_type_cast &&
        type == other.type
    end
S
Sean Griffin 已提交
92 93 94 95 96
    alias eql? ==

    def hash
      [self.class, name, value_before_type_cast, type].hash
    end
97

98 99 100 101 102 103 104 105
    protected

    def initialize_dup(other)
      if defined?(@value) && @value.duplicable?
        @value = @value.dup
      end
    end

106
    class FromDatabase < Attribute # :nodoc:
107
      def type_cast(value)
108
        type.deserialize(value)
109 110 111
      end
    end

112
    class FromUser < Attribute # :nodoc:
113
      def type_cast(value)
S
Sean Griffin 已提交
114
        type.cast(value)
115
      end
116 117 118 119

      def came_from_user?
        true
      end
120
    end
121

122 123 124 125 126 127 128 129 130 131
    class WithCastValue < Attribute # :nodoc:
      def type_cast(value)
        value
      end

      def changed_in_place_from?(old_value)
        false
      end
    end

132 133 134
    class Null < Attribute # :nodoc:
      def initialize(name)
        super(name, nil, Type::Value.new)
135
      end
136

137 138
      def value
        nil
139
      end
140

141 142 143 144
      def with_type(type)
        self.class.with_cast_value(name, nil, type)
      end

145 146 147 148
      def with_value_from_database(value)
        raise ActiveModel::MissingAttributeError, "can't write unknown attribute `#{name}`"
      end
      alias_method :with_value_from_user, :with_value_from_database
149 150 151
    end

    class Uninitialized < Attribute # :nodoc:
152 153
      def initialize(name, type)
        super(name, nil, type)
154 155 156
      end

      def value
157 158 159 160 161 162
        if block_given?
          yield name
        end
      end

      def value_for_database
163 164 165 166
      end

      def initialized?
        false
167 168
      end
    end
169
    private_constant :FromDatabase, :FromUser, :Null, :Uninitialized, :WithCastValue
170 171
  end
end