Class: Associationist::Mixin

Inherits:
Module
  • Object
show all
Defined in:
lib/associationist/mixin.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_config) ⇒ Mixin

Returns a new instance of Mixin.



3
4
5
# File 'lib/associationist/mixin.rb', line 3

def initialize raw_config
  @raw_config = raw_config
end

Instance Method Details

#included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/associationist/mixin.rb', line 7

def included base
  config = Config.new @raw_config
  config.check

  reflection_options = {associationist: config}

  case config.type
  when :singular
    reflection = Builder::SingularAssociation.build(base, config.name, nil, reflection_options)
  when :collection
    reflection = Builder::CollectionAssociation.build(base, config.name, nil, reflection_options)
  else
    raise "unknown type #{config.type.inspect}"
  end
  ::ActiveRecord::Reflection.add_reflection base, config.name, reflection
end

#inspectObject



24
25
26
# File 'lib/associationist/mixin.rb', line 24

def inspect
  "#<Associationist::Mixin @name=#{@raw_config[:name].inspect}>"
end