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
# File 'lib/active_data/model/attributes/base.rb', line 8

def initialize(name, owner)
  @name = name
  @owner = owner
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

#inspect_attributeObject



59
60
61
62
63
64
65
66
67
68
# File 'lib/active_data/model/attributes/base.rb', line 59

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/active_data/model/attributes/base.rb', line 70

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

  if pollute
    previous_value = read
    result = yield
    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



43
44
45
# File 'lib/active_data/model/attributes/base.rb', line 43

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

#readObject



31
32
33
# File 'lib/active_data/model/attributes/base.rb', line 31

def read
  @value_cache
end

#read_before_type_castObject



35
36
37
# File 'lib/active_data/model/attributes/base.rb', line 35

def read_before_type_cast
  @value_cache
end

#readonly?Boolean

Returns:



55
56
57
# File 'lib/active_data/model/attributes/base.rb', line 55

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

#reflectionObject



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

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

#resetObject



27
28
29
# File 'lib/active_data/model/attributes/base.rb', line 27

def reset
  remove_variable(:value, :value_before_type_cast)
end

#typecast(value) ⇒ Object



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

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

#value_present?Boolean

Returns:



39
40
41
# File 'lib/active_data/model/attributes/base.rb', line 39

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

#write(value) ⇒ Object



22
23
24
25
# File 'lib/active_data/model/attributes/base.rb', line 22

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

#write_value(value) ⇒ Object



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

def write_value(value)
  reset
  @value_cache = value
end