Class: ActiveData::Model::Attributes::Reflections::Represents

Inherits:
Attribute
  • Object
show all
Defined in:
lib/active_data/model/attributes/reflections/represents.rb

Instance Attribute Summary

Attributes inherited from Base

#name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Attribute

#defaultizer, #enumerizer, generate_methods, #normalizers

Methods inherited from Base

attribute_class, #build_attribute, generate_methods, #readonly, #typecaster

Constructor Details

#initialize(name, options) ⇒ Represents

Returns a new instance of Represents.

Raises:

  • (ArgumentError)


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

def initialize(name, options)
  super
  raise ArgumentError, "Undefined reference for `#{name}`" if reference.blank?
end

Class Method Details

.build(target, generated_methods, name, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/active_data/model/attributes/reflections/represents.rb', line 6

def self.build(target, generated_methods, name, *args, &block)
  options = args.extract_options!

  reference = target.reflect_on_association(options[:of]) if target.respond_to?(:reflect_on_association)
  reference ||= target.reflect_on_attribute(options[:of]) if target.respond_to?(:reflect_on_attribute)
  options[:of] = reference.name if reference
  validates_nested = target.respond_to?(:validates_nested) && !target.validates_nested?(options[:of])
  target.validates_nested(options[:of]) if validates_nested

  super(target, generated_methods, name, *args, options, &block)
end

Instance Method Details

#columnObject



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

def column
  @column ||= options[:column].presence.try(:to_s) || name
end

#inspect_reflectionObject



47
48
49
# File 'lib/active_data/model/attributes/reflections/represents.rb', line 47

def inspect_reflection
  "#{name}: (represents)"
end

#readerObject



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

def reader
  @reader ||= options[:reader].presence.try(:to_s) || column
end

#reader_before_type_castObject



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

def reader_before_type_cast
  @reader_before_type_cast ||= "#{reader}_before_type_cast"
end

#referenceObject



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

def reference
  @reference ||= options[:of].to_s
end

#typeObject



23
24
25
# File 'lib/active_data/model/attributes/reflections/represents.rb', line 23

def type
  Object
end

#writerObject



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

def writer
  @writer ||= "#{options[:writer].presence || column}="
end