Class: ActiveData::Model::Attributes::Reflections::ReferenceOne

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

Direct Known Subclasses

ReferenceMany

Constant Summary collapse

TYPES =
{
  integer: Integer,
  float: Float,
  decimal: BigDecimal,
  datetime: Time,
  timestamp: Time,
  time: Time,
  date: Date,
  text: String,
  string: String,
  binary: String,
  boolean: Boolean
}

Instance Attribute Summary

Attributes inherited from Base

#name, #options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

attribute_class, #build_attribute, #initialize, #inspect_reflection, #readonly, #type, #typecaster

Constructor Details

This class inherits a constructor from ActiveData::Model::Attributes::Reflections::Base

Class Method Details

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



20
21
22
23
24
25
26
27
28
29
# File 'lib/active_data/model/attributes/reflections/reference_one.rb', line 20

def self.build target, generated_methods, name, *args, &block
  options = args.extract_options!
  generate_methods name, generated_methods
  type_proc = -> {
    reflection = target.reflect_on_association(options[:association])
    column = reflection.klass.columns_hash[reflection.primary_key.to_s]
    TYPES[column.type]
  }
  new(name, options.reverse_merge(type: type_proc))
end

.generate_methods(name, target) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_data/model/attributes/reflections/reference_one.rb', line 31

def self.generate_methods name, target
  target.class_eval <<-RUBY, __FILE__, __LINE__ + 1
    def #{name}
      attribute('#{name}').read
    end

    def #{name}= value
      attribute('#{name}').write(value)
    end

    def #{name}?
      attribute('#{name}').query
    end

    def #{name}_before_type_cast
      attribute('#{name}').read_before_type_cast
    end
  RUBY
end

Instance Method Details

#associationObject



51
52
53
# File 'lib/active_data/model/attributes/reflections/reference_one.rb', line 51

def association
  @association ||= options[:association].to_s
end