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

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

Direct Known Subclasses

Attribute, ReferenceOne

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Base

Returns a new instance of Base.



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

def initialize(name, options = {})
  @name = name.to_s
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.attribute_classObject



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

def attribute_class
  @attribute_class ||= "ActiveData::Model::Attributes::#{name.demodulize}".constantize
end

.build(_target, _generated_methods, name, *args, &block) ⇒ Object



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

def build(_target, _generated_methods, name, *args, &block)
  options = args.extract_options!
  options[:type] = args.first if args.first
  options[:default] = block if block
  new(name, options)
end

.generate_methods(name, target) ⇒ Object



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

def generate_methods(name, target) end

Instance Method Details

#build_attribute(owner, raw_value = ActiveData::UNDEFINED) ⇒ Object



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

def build_attribute(owner, raw_value = ActiveData::UNDEFINED)
  attribute = self.class.attribute_class.new(name, owner)
  attribute.write_value(raw_value, origin: :persistence) unless raw_value == ActiveData::UNDEFINED
  attribute
end

#inspect_reflectionObject



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

def inspect_reflection
  "#{name}: #{type}"
end

#readonlyObject



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

def readonly
  @readonly ||= options[:readonly]
end

#typeObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/active_data/model/attributes/reflections/base.rb', line 33

def type
  @type ||= case options[:type]
  when Class, Module
    options[:type]
  when nil
    raise "Type is not specified for `#{name}`"
  else
    options[:type].to_s.camelize.constantize
  end
end

#typecasterObject



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

def typecaster
  @typecaster ||= ActiveData.typecaster(type.ancestors.grep(Class))
end