Class: MassiveRecord::ORM::Relations::Metadata

Inherits:
Object
  • Object
show all
Defined in:
lib/massive_record/orm/relations/metadata.rb

Overview

The master of metadata related to a relation. For instance; references_one :employee, :foreign_key => “person_id”, :class_name => “Person”

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Metadata

Returns a new instance of Metadata.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/massive_record/orm/relations/metadata.rb', line 16

def initialize(name, options = {})
  options.to_options!
  self.name = name
  self.foreign_key = options[:foreign_key]
  if options.has_key? :store_in
    self.store_in = options[:store_in]
  elsif options.has_key? :store_foreign_key_in
    self.store_foreign_key_in = options[:store_foreign_key_in]
  end
  self.class_name = options[:class_name]
  self.find_with = options[:find_with]
  self.records_starts_from = options[:records_starts_from] if options[:records_starts_from]
  self.polymorphic = options[:polymorphic]
end

Instance Attribute Details

#class_nameObject



61
62
63
# File 'lib/massive_record/orm/relations/metadata.rb', line 61

def class_name
  (@class_name || calculate_class_name).to_s
end

#find_withObject

Returns the value of attribute find_with.



13
14
15
# File 'lib/massive_record/orm/relations/metadata.rb', line 13

def find_with
  @find_with
end

#foreign_keyObject



44
45
46
# File 'lib/massive_record/orm/relations/metadata.rb', line 44

def foreign_key
  (@foreign_key || calculate_foreign_key).to_s
end

#nameObject



32
33
34
# File 'lib/massive_record/orm/relations/metadata.rb', line 32

def name
  @name.to_s if @name
end

#polymorphicObject



88
89
90
# File 'lib/massive_record/orm/relations/metadata.rb', line 88

def polymorphic
  !!@polymorphic
end

#records_starts_fromObject

Returns the value of attribute records_starts_from.



14
15
16
# File 'lib/massive_record/orm/relations/metadata.rb', line 14

def records_starts_from
  @records_starts_from
end

#relation_typeObject



36
37
38
39
40
41
42
# File 'lib/massive_record/orm/relations/metadata.rb', line 36

def relation_type
  if @relation_type
    relation_type = @relation_type.to_s
    relation_type += "_polymorphic" if polymorphic?
    relation_type
  end
end

#store_inObject



69
70
71
# File 'lib/massive_record/orm/relations/metadata.rb', line 69

def store_in
  @store_in.to_s if @store_in
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



102
103
104
# File 'lib/massive_record/orm/relations/metadata.rb', line 102

def ==(other)
  other.instance_of?(self.class) && other.hash == hash
end

#foreign_key_setterObject



48
49
50
# File 'lib/massive_record/orm/relations/metadata.rb', line 48

def foreign_key_setter
  foreign_key+'='
end

#hashObject



107
108
109
# File 'lib/massive_record/orm/relations/metadata.rb', line 107

def hash
  name.hash
end

#new_relation_proxy(proxy_owner) ⇒ Object



97
98
99
# File 'lib/massive_record/orm/relations/metadata.rb', line 97

def new_relation_proxy(proxy_owner)
  proxy_class_name.constantize.new(:proxy_owner => proxy_owner, :metadata => self)
end

#persisting_foreign_key?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/massive_record/orm/relations/metadata.rb', line 83

def persisting_foreign_key?
  !!store_in && !records_starts_from
end

#polymorphic?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/massive_record/orm/relations/metadata.rb', line 92

def polymorphic?
  polymorphic
end

#polymorphic_type_columnObject



52
53
54
55
# File 'lib/massive_record/orm/relations/metadata.rb', line 52

def polymorphic_type_column
  type_column = foreign_key.gsub(/_id$/, '')
  type_column + "_type"
end

#polymorphic_type_column_setterObject



57
58
59
# File 'lib/massive_record/orm/relations/metadata.rb', line 57

def polymorphic_type_column_setter
  polymorphic_type_column+'='
end

#proxy_target_classObject



65
66
67
# File 'lib/massive_record/orm/relations/metadata.rb', line 65

def proxy_target_class
  class_name.constantize
end

#represents_a_collection?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/massive_record/orm/relations/metadata.rb', line 113

def represents_a_collection?
  relation_type == 'references_many'
end

#store_foreign_key_inObject



73
74
75
76
# File 'lib/massive_record/orm/relations/metadata.rb', line 73

def store_foreign_key_in
  ActiveSupport::Deprecation.warn("store_foreign_key_in is deprecated. Use store_in instead!")
  store_in
end

#store_foreign_key_in=(column_family) ⇒ Object



78
79
80
81
# File 'lib/massive_record/orm/relations/metadata.rb', line 78

def store_foreign_key_in=(column_family)
  ActiveSupport::Deprecation.warn("store_foreign_key_in is deprecated. Use store_in instead!")
  self.store_in = column_family
end