Class: ActiveData::Model::Attributes::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_data/model/attributes/base.rb

Direct Known Subclasses

Attribute, ReferenceOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, owner) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
# File 'lib/active_data/model/attributes/base.rb', line 8

def initialize(name, owner)
  @name = name
  @owner = owner
  @origin = :default
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/active_data/model/attributes/base.rb', line 5

def name
  @name
end

#ownerObject (readonly)

Returns the value of attribute owner.



5
6
7
# File 'lib/active_data/model/attributes/base.rb', line 5

def owner
  @owner
end

Instance Method Details

#came_from_default?Boolean

Returns:



45
46
47
# File 'lib/active_data/model/attributes/base.rb', line 45

def came_from_default?
  @origin == :default
end

#came_from_user?Boolean

Returns:



41
42
43
# File 'lib/active_data/model/attributes/base.rb', line 41

def came_from_user?
  @origin == :user
end

#inspect_attributeObject



69
70
71
72
73
74
75
76
77
78
# File 'lib/active_data/model/attributes/base.rb', line 69

def inspect_attribute
  value = case read
  when Date, Time, DateTime
    %("#{read.to_s(:db)}")
  else
    inspection = read.inspect
    inspection.size > 100 ? inspection.truncate(50) : inspection
  end
  "#{name}: #{value}"
end

#polluteObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/active_data/model/attributes/base.rb', line 80

def pollute
  pollute = owner.class.dirty? && !owner.send(:attribute_changed?, name)

  if pollute
    previous_value = owner.__send__(name)
    owner.send("#{name}_will_change!")

    result = yield

    owner.__send__(:clear_attribute_changes, [name]) if owner.__send__(name) == previous_value

    if previous_value != read || (
      read.respond_to?(:changed?) &&
      read.changed?
    )
      owner.send(:set_attribute_was, name, previous_value)
    end
    result
  else
    yield
  end
end

#queryObject



53
54
55
# File 'lib/active_data/model/attributes/base.rb', line 53

def query
  !(read.respond_to?(:zero?) ? read.zero? : read.blank?)
end

#readObject



33
34
35
# File 'lib/active_data/model/attributes/base.rb', line 33

def read
  @value_cache
end

#read_before_type_castObject



37
38
39
# File 'lib/active_data/model/attributes/base.rb', line 37

def read_before_type_cast
  @value_cache
end

#readonly?Boolean

Returns:



65
66
67
# File 'lib/active_data/model/attributes/base.rb', line 65

def readonly?
  !!(readonly.is_a?(Proc) ? evaluate(&readonly) : readonly)
end

#reflectionObject



14
15
16
# File 'lib/active_data/model/attributes/base.rb', line 14

def reflection
  @owner.class._attributes[name]
end

#resetObject



29
30
31
# File 'lib/active_data/model/attributes/base.rb', line 29

def reset
  remove_variable(:value, :value_before_type_cast)
end

#typecast(value) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/active_data/model/attributes/base.rb', line 57

def typecast(value)
  if value.instance_of?(type)
    value
  else
    typecaster.call(value, self) unless value.nil?
  end
end

#value_present?Boolean

Returns:



49
50
51
# File 'lib/active_data/model/attributes/base.rb', line 49

def value_present?
  !read.nil? && !(read.respond_to?(:empty?) && read.empty?)
end

#write(value) ⇒ Object



24
25
26
27
# File 'lib/active_data/model/attributes/base.rb', line 24

def write(value)
  return if readonly?
  write_value value
end

#write_value(value, origin: :user) ⇒ Object



18
19
20
21
22
# File 'lib/active_data/model/attributes/base.rb', line 18

def write_value(value, origin: :user)
  reset
  @origin = origin
  @value_cache = value
end