Class: Bigamy::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/bigamy/proxy.rb

Direct Known Subclasses

BelongsTo, HasMany, HasOne

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, options) ⇒ Proxy

Returns a new instance of Proxy.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/bigamy/proxy.rb', line 6

def initialize parent, name, options
  self.name = name
  self.me = parent
  self.primary_key = options.delete(:primary_key) || :id
  self.methods_added = Set.new
  self.options = options

  serialize_foreign_key
  create_accessors
  serialize_foreign_key
end

Instance Attribute Details

#meObject

Returns the value of attribute me.



4
5
6
# File 'lib/bigamy/proxy.rb', line 4

def me
  @me
end

#methods_addedObject

Returns the value of attribute methods_added.



4
5
6
# File 'lib/bigamy/proxy.rb', line 4

def methods_added
  @methods_added
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/bigamy/proxy.rb', line 4

def name
  @name
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/bigamy/proxy.rb', line 4

def options
  @options
end

#primary_keyObject

Returns the value of attribute primary_key.



4
5
6
# File 'lib/bigamy/proxy.rb', line 4

def primary_key
  @primary_key
end

Instance Method Details

#add_getterObject



30
31
32
# File 'lib/bigamy/proxy.rb', line 30

def add_getter
  raise
end

#add_setterObject



34
35
36
# File 'lib/bigamy/proxy.rb', line 34

def add_setter
  raise
end

#create_accessorsObject



22
23
24
25
26
27
28
# File 'lib/bigamy/proxy.rb', line 22

def create_accessors
  methods_added << name
  methods_added << "#{name}="

  add_getter
  add_setter
end

#divorce_everyoneObject



46
47
48
49
# File 'lib/bigamy/proxy.rb', line 46

def divorce_everyone
  methods_added.each {|m| me.send(:undef_method, m) if me.respond_to?(m)}
  self.methods_added = Set.new
end

#foreign_keyObject



18
19
20
# File 'lib/bigamy/proxy.rb', line 18

def foreign_key
  raise
end

#root_klassObject



60
61
62
# File 'lib/bigamy/proxy.rb', line 60

def root_klass
  me
end

#root_klass_nameObject



64
65
66
# File 'lib/bigamy/proxy.rb', line 64

def root_klass_name
  me.to_s.underscore.singularize.gsub('/', '_')
end

#serialize_foreign_keyObject



38
39
40
41
42
43
44
# File 'lib/bigamy/proxy.rb', line 38

def serialize_foreign_key
  target_klass.class_eval "    def \#{foreign_key}\n      import_id_val read_val(:\#{foreign_key})\n    end\n    EOF\nend\n"

#target_klassObject Also known as: klass



51
52
53
# File 'lib/bigamy/proxy.rb', line 51

def target_klass
  options[:class] || name.to_s.camelcase.singularize.constantize
end

#target_klass_nameObject



56
57
58
# File 'lib/bigamy/proxy.rb', line 56

def target_klass_name
  name.to_s.underscore.singularize.gsub('/', '_')
end