Class: SData::VirtualBase

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, ActiveRecordExtensions::Mixin, ActiveRecordExtensions::SdataUuidableMixin, PayloadMap
Includes:
Sync::SdataSyncingMixin
Defined in:
lib/s_data/virtual_base.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ActiveRecordExtensions::Mixin

acts_as_sdata, find_by_sdata_instance_id

Methods included from ActiveRecordExtensions::SdataUuidableMixin

has_sdata_uuid

Methods included from PayloadMap

define_payload_map, has_sdata_association, has_sdata_attr

Methods included from Sync::SdataSyncingMixin

#sd_digest, #sd_sync_state, #sd_sync_state=

Constructor Details

#initialize(the_baze, the_type = nil) ⇒ VirtualBase

Returns a new instance of VirtualBase.



16
17
18
19
20
21
22
23
# File 'lib/s_data/virtual_base.rb', line 16

def initialize(the_baze, the_type=nil)
  @uuid_class = SData::SdUuid      
  self.baze = the_baze
  if self.respond_to?('sdata_type') && the_type
    self.sdata_type = the_type
  end
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object

temporary



26
27
28
29
30
31
32
# File 'lib/s_data/virtual_base.rb', line 26

def method_missing(meth, *args, &block)
  if @payload
    @payload.send(meth, *args, &block)
  else
    super
  end      
end

Class Attribute Details

.baze_classObject

Returns the value of attribute baze_class.



7
8
9
# File 'lib/s_data/virtual_base.rb', line 7

def baze_class
  @baze_class
end

Instance Attribute Details

#bazeObject

Returns the value of attribute baze.



13
14
15
# File 'lib/s_data/virtual_base.rb', line 13

def baze
  @baze
end

Class Method Details

.all(*params) ⇒ Object



94
95
96
# File 'lib/s_data/virtual_base.rb', line 94

def self.all(*params)
  self.collection(self.baze_class.all(*params))
end

.baze_class_nameObject



78
79
80
# File 'lib/s_data/virtual_base.rb', line 78

def self.baze_class_name
  baze_class.nil? ? nil : baze_class.name.demodulize
end

.baze_classesObject

TODO – should return all bb models that are used to composite the virtual model (or at least the subset whose modifications must be tracked)



74
75
76
# File 'lib/s_data/virtual_base.rb', line 74

def self.baze_classes
  [self.baze_class]
end

.build_for(data, the_type = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/s_data/virtual_base.rb', line 50

def self.build_for(data, the_type=nil)
  if data.is_a? Array
    data.map {|item| virtual_base_for_object(item, the_type) }.compact
  else
   virtual_base_for_object(data, the_type)
  end
end

.collection(arr) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/s_data/virtual_base.rb', line 98

def self.collection(arr)
  arr.map do |obj| 
    case obj
    when VirtualBase, DeletedObjectProxy
      obj
    when ActiveRecord::Base
      self.new(obj)
    else
      obj
    end
  end
end

.find(*params) ⇒ Object



86
87
88
# File 'lib/s_data/virtual_base.rb', line 86

def self.find(*params)
  self.new(self.baze_class.find(*params))
end

.first(*params) ⇒ Object



90
91
92
# File 'lib/s_data/virtual_base.rb', line 90

def self.first(*params)
  self.new(self.baze_class.first(*params))
end

.sdata_nameObject



82
83
84
# File 'lib/s_data/virtual_base.rb', line 82

def self.sdata_name
  name.demodulize
end

.virtual_base_for_object(obj, the_type = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/s_data/virtual_base.rb', line 58

def self.virtual_base_for_object(obj, the_type=nil)
  if obj
    vb = self.new(obj, the_type)
    vb = DeletedObjectProxy.from_virtual_base(vb) if obj.class.paranoid? && obj.deleted?
    vb
  else
    nil
  end
end

Instance Method Details

#baze_class_nameObject



34
35
36
# File 'lib/s_data/virtual_base.rb', line 34

def baze_class_name
  self.class.baze_class_name
end

#ownerObject



68
69
70
# File 'lib/s_data/virtual_base.rb', line 68

def owner
  raise "Security problem: owner not defined in subclass!"
end

#referenceObject



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

def reference
  # GCRM requires reference to be present and unique to each GCRM model. Decided to use baze id, but
  # alone it is insufficient because some GCRM models (e.g. Address) can have the same baze id for different
  # bazes (e.g. customer vs user). So using the form below.
  # Adding GCRM model name is fine but unneeded. GCRM name _only_, without BB model name, is insufficient.
  "#{self.baze.class.name.demodulize}_#{self.baze.id}"
end

#sd_classObject



46
47
48
# File 'lib/s_data/virtual_base.rb', line 46

def sd_class
  self.class.name
end