Class: NestedRecord::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/nested_record/setup.rb

Direct Known Subclasses

HasMany, HasOne

Defined Under Namespace

Classes: HasMany, HasOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, **options, &block) ⇒ Setup

Returns a new instance of Setup.



6
7
8
9
10
11
12
13
14
15
# File 'lib/nested_record/setup.rb', line 6

def initialize(owner, name, **options, &block)
  @options = options
  @owner = owner
  @name = name

  setup_association_attribute
  setup_record_class(&block)
  setup_attributes_writer_opts
  setup_methods_extension
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/nested_record/setup.rb', line 4

def name
  @name
end

#primary_keyObject (readonly)

Returns the value of attribute primary_key.



4
5
6
# File 'lib/nested_record/setup.rb', line 4

def primary_key
  @primary_key
end

#reject_if_procObject (readonly)

Returns the value of attribute reject_if_proc.



4
5
6
# File 'lib/nested_record/setup.rb', line 4

def reject_if_proc
  @reject_if_proc
end

Instance Method Details

#attributes_writer_strategyObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/nested_record/setup.rb', line 36

def attributes_writer_strategy
  return unless @options.fetch(:attributes_writer) { true }

  case (strategy = @attributes_writer_opts.fetch(:strategy) { :upsert })
  when :rewrite, :upsert
    return strategy
  else
    raise NestedRecord::ConfigurationError, "Unknown strategy #{strategy.inspect}"
  end
end

#primary_check(type) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nested_record/setup.rb', line 47

def primary_check(type)
  if (pkey_attributes = primary_key)
    klass = record_class
  else
    klass = record_class.find_subtype(type)
    while !(pkey_attributes = klass.primary_key) && (klass < NestedRecord::Base)
      klass = klass.superclass
    end
  end
  # TODO: cache this
  NestedRecord::PrimaryKeyCheck.new(klass, pkey_attributes) if pkey_attributes
end

#record_classObject



17
18
19
20
21
22
# File 'lib/nested_record/setup.rb', line 17

def record_class
  if @record_class.is_a? String
    @record_class = NestedRecord.lookup_const(@owner, @record_class)
  end
  @record_class
end