Class: MDWA::DSL::EntityAssociation

Inherits:
Object
  • Object
show all
Defined in:
lib/mdwa/dsl/entity_association.rb

Constant Summary collapse

ACCEPTED_TYPES =
[:one_to_many, :many_to_one, :one_to_one, :one_to_one_not_navigable, :many_to_many]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ EntityAssociation

Returns a new instance of EntityAssociation.



12
13
14
15
16
17
# File 'lib/mdwa/dsl/entity_association.rb', line 12

def initialize(source)
  self.source = source
  self.composition = false
  self.skip_views = false
  self.options = {}
end

Instance Attribute Details

#compositionObject

Returns the value of attribute composition.



8
9
10
# File 'lib/mdwa/dsl/entity_association.rb', line 8

def composition
  @composition
end

#descriptionObject

Returns the value of attribute description.



8
9
10
# File 'lib/mdwa/dsl/entity_association.rb', line 8

def description
  @description
end

#destinationObject

Returns the value of attribute destination.



7
8
9
# File 'lib/mdwa/dsl/entity_association.rb', line 7

def destination
  @destination
end

#destination_viewObject

Returns the value of attribute destination_view.



7
8
9
# File 'lib/mdwa/dsl/entity_association.rb', line 7

def destination_view
  @destination_view
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/mdwa/dsl/entity_association.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/mdwa/dsl/entity_association.rb', line 8

def options
  @options
end

#skip_viewsObject

Returns the value of attribute skip_views.



8
9
10
# File 'lib/mdwa/dsl/entity_association.rb', line 8

def skip_views
  @skip_views
end

#sourceObject

Returns the value of attribute source.



7
8
9
# File 'lib/mdwa/dsl/entity_association.rb', line 7

def source
  @source
end

#typeObject

Returns the value of attribute type.



8
9
10
# File 'lib/mdwa/dsl/entity_association.rb', line 8

def type
  @type
end

Instance Method Details

#generateObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mdwa/dsl/entity_association.rb', line 61

def generate
  destination = DSL.entity(self.destination.camelize)
  
  gen = []
  gen << self.name
  gen << destination.scaffold_name + (destination.model_name != destination.scaffold_name ? ",#{destination.model_name}" : '')
  gen << (self.destination_view || destination.default_attribute.name)
  gen << generator_type
  
  gen.join(':')
end

#generator_typeObject

Return the mapped type for the code generation.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mdwa/dsl/entity_association.rb', line 46

def generator_type
  case self.type.to_sym
    when :one_to_many
      return (self.composition ? 'nested_many' : 'has_many' )
    when :one_to_one
      return (self.composition ? 'nested_one' : 'belongs_to' )
    when :one_to_one_not_navigable
      'has_one'
    when :many_to_one
      'belongs_to'
    when :many_to_many
      'has_and_belongs_to_many'
    end
end

#many_to_many?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/mdwa/dsl/entity_association.rb', line 85

def many_to_many?
  self.type.to_sym == :many_to_many
end

#many_to_one?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/mdwa/dsl/entity_association.rb', line 77

def many_to_one?
  self.type.to_sym == :many_to_one
end

#one_to_many?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/mdwa/dsl/entity_association.rb', line 73

def one_to_many?
  self.type.to_sym == :one_to_many
end

#one_to_one?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/mdwa/dsl/entity_association.rb', line 81

def one_to_one?
  self.type.to_sym == :one_to_one
end

#one_to_one_not_navigable?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/mdwa/dsl/entity_association.rb', line 89

def one_to_one_not_navigable?
  self.type.to_sym == :one_to_one_not_navigable
end

#skip_views!Object



32
33
34
# File 'lib/mdwa/dsl/entity_association.rb', line 32

def skip_views!
  self.skip_views = true
end

#skip_views?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/mdwa/dsl/entity_association.rb', line 28

def skip_views?
  skip_views
end

#to_model_associationObject

Return the model association generator.



39
40
41
# File 'lib/mdwa/dsl/entity_association.rb', line 39

def to_model_association
  self.source.generator_model.associations.select{ |m_assoc| m_assoc.model2.name.underscore == self.name.underscore }.first
end