Class: Brainstem::DSL::Association

Inherits:
Object
  • Object
show all
Includes:
Concerns::Lookup
Defined in:
lib/brainstem/dsl/association.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Lookup

#run_on_with_lookup

Constructor Details

#initialize(name, target_class, options) ⇒ Association

Returns a new instance of Association.



10
11
12
13
14
# File 'lib/brainstem/dsl/association.rb', line 10

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/brainstem/dsl/association.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/brainstem/dsl/association.rb', line 8

def options
  @options
end

#target_classObject (readonly)

Returns the value of attribute target_class.



8
9
10
# File 'lib/brainstem/dsl/association.rb', line 8

def target_class
  @target_class
end

Instance Method Details

#always_return_ref_with_sti_base?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/brainstem/dsl/association.rb', line 59

def always_return_ref_with_sti_base?
  options[:always_return_ref_with_sti_base]
end

#descriptionObject



16
17
18
# File 'lib/brainstem/dsl/association.rb', line 16

def description
  options[:info].presence
end

#method_nameObject



32
33
34
35
36
37
38
# File 'lib/brainstem/dsl/association.rb', line 32

def method_name
  if options[:dynamic] || options[:lookup]
    nil
  else
    (options[:via].presence || name).to_s
  end
end

#polymorphic?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/brainstem/dsl/association.rb', line 55

def polymorphic?
  target_class == :polymorphic
end

#polymorphic_classesObject



24
25
26
# File 'lib/brainstem/dsl/association.rb', line 24

def polymorphic_classes
  options[:polymorphic_classes]
end

#response_keyObject



20
21
22
# File 'lib/brainstem/dsl/association.rb', line 20

def response_key
  options[:response_key]
end

#run_on(model, context, helper_instance = Object.new) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brainstem/dsl/association.rb', line 40

def run_on(model, context, helper_instance = Object.new)
  if options[:lookup]
    run_on_with_lookup(model, context, helper_instance)
  elsif options[:dynamic]
    proc = options[:dynamic]
    if proc.arity == 1
      helper_instance.instance_exec(model, &proc)
    else
      helper_instance.instance_exec(&proc)
    end
  else
    model.send(method_name)
  end
end

#typeObject



28
29
30
# File 'lib/brainstem/dsl/association.rb', line 28

def type
  options[:type]
end