Class: BrainDamage::Relation

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/brain_damage/lib/relation/base.rb

Direct Known Subclasses

BelongsTo, HasAndBelongsToMany, HasMany, HasOne

Constant Summary collapse

VALID_RELATION_TYPES =
[ :belongs_to, :has_and_belongs_to_many, :has_many, :has_one ]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRelation

Returns a new instance of Relation.



5
6
7
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 5

def initialize
  raise "class Relation is an abstract class and can't be instantiated"
end

Class Method Details

.create(type, options) ⇒ Object



41
42
43
44
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 41

def self.create(type, options)
  return nil unless Relation.is_valid_relation? type
  "BrainDamage::#{type.to_s.camelize}".constantize.new options
end

.is_valid_relation?(type) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 37

def self.is_valid_relation? type
  VALID_RELATION_TYPES.include? type
end

Instance Method Details

#add_options_to_line(line, options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 26

def add_options_to_line(line, options)
  return line unless options
  ([line] + options.map { |name, value|
    if value.is_a? Symbol
      "#{name}: :#{value}"
    else
      "#{name}: '#{value}'"
    end
  }).join ', '
end

#class_nameObject



17
18
19
20
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 17

def class_name
  return @options[:class_name] if @options[:class_name]
  @options[:field].name.to_s.singularize.camelize
end

#model_linesObject



22
23
24
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 22

def model_lines
  []
end

#nested_onObject



13
14
15
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 13

def nested_on
  resource_name_according_to_foreign
end

#resource_name_according_to_foreignObject



9
10
11
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 9

def resource_name_according_to_foreign
  @options[:as] or @options[:inverse_of] or singular_table_name
end

#white_listObject



46
47
48
# File 'lib/generators/brain_damage/lib/relation/base.rb', line 46

def white_list
  nil
end