Class: DcTemp

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/dc_temp.rb

Overview

Schema information

Collection name: dc_temp : Collection used for temporary saving of any models data

dc_temp collection has only two fields. Key and Data hash. Data must be populated prior to for display best in dc_new_record callback.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parms = {}) ⇒ DcTemp

Initilize object



45
46
47
48
49
50
51
# File 'app/models/dc_temp.rb', line 45

def initialize(parms = {})
  super()
  parms.stringify_keys!
  self.key = parms.delete('key')  if parms['key']
  self.key = parms.delete('active') if parms['active']
  parms.each { |k, value| self.data[k] = value }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Method missing will return value if value defined by m parameter is saved to



106
107
108
109
110
111
112
113
114
# File 'app/models/dc_temp.rb', line 106

def method_missing(m, *args, &block) #:nodoc:
  m = m.to_s
  if m.match('=')
    m.chomp!('=')
    self.data[m] = args.first
  else
    self.data[m]
  end   
end

Class Method Details

.clear(key) ⇒ Object

Remove all documents with specified key from dc_temp collection



119
120
121
# File 'app/models/dc_temp.rb', line 119

def self.clear(key)
  self.where(key: key).delete
end

.prepare(key:, clear: nil) ⇒ Object

Prepare dc_temp for data. It first checks if data associated with the key is to be deleted and then yields block code.

Returns: Query for the data associated with the key



129
130
131
132
133
134
135
# File 'app/models/dc_temp.rb', line 129

def self.prepare(key:, clear: nil)
  unless %w(no false 0).include?(clear.to_s.strip.downcase)
    self.clear(key)
    yield
  end
  self.where(key: key) 
end

Instance Method Details

#[](field) ⇒ Object

Redefine [] method to act similar as send method



84
85
86
# File 'app/models/dc_temp.rb', line 84

def [](field)
  self.data[field.to_s]
end

#[]=(field, value) ⇒ Object

Redefine [] method to act similar as send method



91
92
93
# File 'app/models/dc_temp.rb', line 91

def []=(field, value)
  self.data[field.to_s] = value
end

#__idObject



56
57
58
# File 'app/models/dc_temp.rb', line 56

def __id()
  self.data['id']
end

#respond_to?(a = nil, b = nil) ⇒ Boolean

Respond_to should always return true.

Returns:

  • (Boolean)


63
64
65
# File 'app/models/dc_temp.rb', line 63

def respond_to?(a=nil,b=nil)
  true
end

#send(field, value = nil) ⇒ Object

Redefine send method. Send is used to assign or access value by cmsedit controller.



70
71
72
73
74
75
76
77
78
79
# File 'app/models/dc_temp.rb', line 70

def send(field,value=nil)
  return super(field) if field.is_a? Symbol
  field = field.to_s
  if field.match('=')
    field.chomp!('=')
    self.data[field] = value
  else
    self.data[field]
  end
end

#to_sObject

For debugging purposes



98
99
100
# File 'app/models/dc_temp.rb', line 98

def to_s
  "DcTemp: @key=#{self.key} @data=#{self.data.inspect}"
end