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.



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

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



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

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.merge!(type: args.first) if args.first
  options.merge!(default: block) if block
  new(name, options)
end

.generate_methods(name, target) ⇒ Object



15
16
# 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 = nil) ⇒ Object



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

def build_attribute owner, raw_value = nil
  attribute = self.class.attribute_class.new(name, owner)
  attribute.write_value(raw_value) if raw_value
  attribute
end

#inspect_reflectionObject



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

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

#readonlyObject



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

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

#typeObject



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

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

#typecasterObject



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

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