Class: ActiveNode::Associations::Builder::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/active_node/associations/builder/association.rb

Overview

:nodoc:

Direct Known Subclasses

CollectionAssociation, SingularAssociation

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ Association

Returns a new instance of Association.

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
# File 'lib/active_node/associations/builder/association.rb', line 15

def initialize(model, name, options)
  raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)

  @model   = model
  @name    = name
  @options = options
end

Class Attribute Details

.valid_optionsObject

Returns the value of attribute valid_options.



4
5
6
# File 'lib/active_node/associations/builder/association.rb', line 4

def valid_options
  @valid_options
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



9
10
11
# File 'lib/active_node/associations/builder/association.rb', line 9

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/active_node/associations/builder/association.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/active_node/associations/builder/association.rb', line 9

def options
  @options
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



9
10
11
# File 'lib/active_node/associations/builder/association.rb', line 9

def reflection
  @reflection
end

Class Method Details

.build(*args) ⇒ Object



11
12
13
# File 'lib/active_node/associations/builder/association.rb', line 11

def self.build(*args)
  new(*args).build
end

Instance Method Details

#buildObject



29
30
31
32
33
34
35
# File 'lib/active_node/associations/builder/association.rb', line 29

def build
  validate_options
  define_accessors
  @reflection = model.create_reflection(macro, name, options, model)
  super # provides an extension point
  @reflection
end

#define_accessorsObject



49
50
51
52
# File 'lib/active_node/associations/builder/association.rb', line 49

def define_accessors
  define_readers
  define_writers
end

#define_readersObject



54
55
56
57
58
59
60
# File 'lib/active_node/associations/builder/association.rb', line 54

def define_readers
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}(*args)
      association(:#{name}).reader(*args)
    end
  CODE
end

#define_writersObject



62
63
64
65
66
67
68
# File 'lib/active_node/associations/builder/association.rb', line 62

def define_writers
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
    def #{name}=(value)
      association(:#{name}).writer(value)
    end
  CODE
end

#macroObject

Raises:

  • (NotImplementedError)


37
38
39
# File 'lib/active_node/associations/builder/association.rb', line 37

def macro
  raise NotImplementedError
end

#mixinObject



23
24
25
# File 'lib/active_node/associations/builder/association.rb', line 23

def mixin
  @model
end

#valid_optionsObject



41
42
43
# File 'lib/active_node/associations/builder/association.rb', line 41

def valid_options
  Association.valid_options
end

#validate_optionsObject



45
46
47
# File 'lib/active_node/associations/builder/association.rb', line 45

def validate_options
  options.assert_valid_keys(valid_options)
end