Class: Entities

Inherits:
RPCQooxdooService show all
Includes:
StorageHandler
Defined in:
lib/qooxview/entity.rb

Direct Known Subclasses

ConfigBases, MigrationVersions, Sessions, Statics

Constant Summary collapse

@@all =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StorageHandler

#add_new_storage, #add_value_to_storage, #create, #create_key, #data_update, #delete_all, #delete_id, #field_args, #filter_by, #find, #find_by, #find_key_by, #first, #get_entry, #has_field?, #has_storage?, #last, #load, #match_by, #match_key, #matches_by, #migrate, #new_id, #replace_st, #save, #save_data, #search_all, #search_all_, #search_by, #search_by_all, #set_entry, #update_key

Methods inherited from RPCQooxdooService

add_prime_service, entities, #get_services, inherited, migrate_all, #needs_covered, services

Constructor Details

#initializeEntities

Returns a new instance of Entities.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/qooxview/entity.rb', line 31

def initialize
  begin
    @data_class = eval(singular(self.class.name))
  rescue Exception
    # Just in case the data-class is not created yet
    eval("class #{singular(self.class.name)} < Entity\nend", TOPLEVEL_BINDING)
    @data_class = eval(singular(self.class.name))
  end
  @storage = nil
  @last_id = 1
  @save_after_create = false
  @changed = false
  @null_allowed = false
  @loading = false
  @is_loaded = false
  @dont_migrate = false

  if @data_class != 'Entity'
    @@all[@data_class] = self
    dputs(4) { "Initializing #{self.class.name} with data_class = #{@data_class}" }

    # Initialize the basic variables
    @blocks = {}
    @values = []
    @data_instances = {}
    @default_type = :CSV
    @keys = {}

    # Stuff for the StorageHandler
    @storage = {}
    @data = {}
    @name = singular(self.class.name)
    @data_field_id = "#{@name}_id".downcase.to_sym
    if @@all.keys.find { |k| k.to_s == 'Static' } &&
        @name != 'Static'
      @static = Statics.get_hash("Entities.#{@name}")
    end
    dputs(3) { "#{@name}: #{@static.inspect}" }

    # Now call the setup_data to initialize the fields
    value_block :default
    setup_data()

    # Finally adding @data_field_id to all storage-types
    dputs(4) { "Adding #{@data_field_id} to block" }
    value_block @data_field_id
    value_int_ALL @data_field_id

    dputs(4) { 'Configuring all storages' }
    @storage.each { |k, s|
      dputs(4) { "Configuring #{k} #{s.inspect}" }
      s.configure(s.config)
    }
    dputs(4) { "Block is now: #{@blocks.inspect}" }
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(cmd, *args) ⇒ Object

We can

  • match_by_field - where the data is searched for the “field”

  • search_by_field - where all data matching “field” is returned

  • list_field - returns an array of all values of “field”

  • listp_field - returns an array of arrays of all “data_field_id” and values of “field”

  • value_type - adds an entry for a value of “type”



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/qooxview/entity.rb', line 203

def method_missing(cmd, *args)
  cmd_str = cmd.to_s
  dputs(5) { "Method missing: #{cmd}" }
  case cmd_str
    when /^match_(key|by)_(.*)/
      define_singleton_method(cmd_str.to_sym) { |arg| match_key($~[2], arg) }
      self.send(cmd_str.to_sym, *args)
    when /^create_key_(.*)/
      create_key($~[1])
    when /^(find|search|matches)(_by|)_/
      action = "#{$~[1]}#{$~[2]}"
      field = cmd_str.sub(/^(find|search|matches)(_by|)_/, '')
      dputs(4) { "Using #{action} for field #{field}" }
      if args[0].is_a? Entity
        dputs(4) { "Getting id because it's an Entity" }
        self.send(action, field, args[0].id)
      else
        self.send(action, field, args[0])
      end
    when /^list_/
      field = cmd_str.sub(/^list_/, '')
      dputs(5) { "Using list for field #{field}" }
      ret = @data.values.collect { |v| v[field.to_sym] }.sort { |a, b|
        a.downcase <=> b.downcase
      }
      dputs(4) { "Returning #{ret.inspect}" }
      ret
    when /^listp_/
      field = cmd_str.sub(/^listp_/, '')
      reverse = field =~ /^rev_/
      if reverse
        field.sub!(/^rev_/, '')
      end
      dputs(5) { "Using listpairs for field #{field.inspect}, #{@data.inspect}" }
      ret = @data.keys.collect { |k|
        dputs(4) { "k is #{k.inspect} - data is #{@data[k].inspect}" }
        [k, @data[k][field.to_sym]] }.sort { |a, b|
        a[1] <=> b[1]
      }
      reverse and ret.reverse!
      dputs(3) { "Returning #{ret.inspect}" }
      ret
    when /^value_/
      cmds = cmd_str.split('_')[1..-1]
      value_add_(cmds, args)
    else
      dputs(0) { "Error: Method is missing: #{cmd} in Entities" }
      dputs(0) { caller.inspect }
      super cmd, *args
  end
end

Instance Attribute Details

#blocksObject

Returns the value of attribute blocks.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def blocks
  @blocks
end

#changedObject

Returns the value of attribute changed.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def changed
  @changed
end

#dataObject

Returns the value of attribute data.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def data
  @data
end

#data_classObject

Returns the value of attribute data_class.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def data_class
  @data_class
end

#data_field_idObject

Returns the value of attribute data_field_id.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def data_field_id
  @data_field_id
end

#data_instancesObject

Returns the value of attribute data_instances.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def data_instances
  @data_instances
end

#is_loadedObject

Returns the value of attribute is_loaded.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def is_loaded
  @is_loaded
end

#keysObject

Returns the value of attribute keys.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def keys
  @keys
end

#last_idObject

Returns the value of attribute last_id.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def last_id
  @last_id
end

#loadingObject

Returns the value of attribute loading.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def loading
  @loading
end

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def name
  @name
end

#null_allowedObject

Returns the value of attribute null_allowed.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def null_allowed
  @null_allowed
end

#save_after_createObject

Returns the value of attribute save_after_create.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def save_after_create
  @save_after_create
end

#storageObject

Returns the value of attribute storage.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def storage
  @storage
end

#valuesObject

Returns the value of attribute values.



26
27
28
# File 'lib/qooxview/entity.rb', line 26

def values
  @values
end

Class Method Details

.delete_all_data(local_only = false) ⇒ Object



348
349
350
351
352
353
354
# File 'lib/qooxview/entity.rb', line 348

def self.delete_all_data(local_only = false)
  @@all.each_pair { |k, v|
    dputs(2) { "Erasing data of #{k}" }
    v.delete_all(local_only)
  }
  ConfigBases.init_load
end

.has_entity?(a) ⇒ Boolean

Returns:

  • (Boolean)


399
400
401
402
403
404
405
# File 'lib/qooxview/entity.rb', line 399

def self.has_entity?(a)
  dputs(4) { "Searching #{a.inspect} in #{@@all.keys.inspect}" }
  @@all.keys.each { |k|
    return true if k.name.to_s == a.to_s
  }
  return false
end

.is_setup?(e) ⇒ Boolean

Returns:

  • (Boolean)


390
391
392
393
394
395
396
397
# File 'lib/qooxview/entity.rb', line 390

def self.is_setup?(e)
  ret = false
  @@all.keys.each { |k|
    ret |= k.to_s == "#{e}"
  }
  dputs(4) { "ret:#{ret} for #{e} with #{@@all.keys.inspect}" }
  ret
end

.load(has_static = true) ⇒ Object

Overwrites ActiveSupport load, which is not needed



89
90
91
# File 'lib/qooxview/entity.rb', line 89

def self.load(has_static = true)
  Entities.method_missing(self.name).load(has_static)
end

.load_allObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/qooxview/entity.rb', line 367

def self.load_all
  dputs(2) { 'Loading everything' }
  @@all.each { |k, v|
    dputs(3) { "Loading #{v.class.name}" }
    v.loading = true
    v.load
  }
  @@all.each { |k, v|
    dputs(3) { "Looking to migrate #{k}" }
    v.migrate
    v.loading = false
    respond_to?(:migrated) and migrated
  }
end

.method_missing(m, *args) ⇒ Object

For an easy Entities.Classname access to all entities stored Might also be used by subclasses to directly acces the instance stored in @@services_hash



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/qooxview/entity.rb', line 332

def self.method_missing(m, *args)
  dputs(5) { "I think I got a class: #{self.name} - #{m} - #{caller.inspect}" }
  if self.name == 'Entities'
    # This is for the Entities-class
    if ret = Entities.service(m)
      return ret
    else
      dputs(0) { "Method is missing: #{m} in Entries" }
      return super(m, *args)
    end
  else
    # We're in a subclass, so we first have to fetch the instance
    return Entities.service(self.name).send(m, *args)
  end
end

.needs(entities) ⇒ Object



407
408
409
410
411
412
413
# File 'lib/qooxview/entity.rb', line 407

def self.needs(entities)
  dputs(2) { "#{self.name} needs #{entities}" }
  entities = entities.to_s.to_a unless entities.class == Array
  @@needs["Entities.#{self.name.to_s}"] = entities.collect { |e|
    "Entities.#{e}"
  }
end

.reloadObject



382
383
384
385
386
387
388
# File 'lib/qooxview/entity.rb', line 382

def self.reload
  Entities.save_all
  Entities.delete_all_data(true)
  SQLite.dbs_close_all
  SQLite.dbs_open_load_migrate
  Entities.load_all
end

.save_allObject



356
357
358
359
360
361
362
363
364
365
# File 'lib/qooxview/entity.rb', line 356

def self.save_all()
  #dputs_func
  dputs(3) { 'Saving everything' }
  start = Time.now
  @@all.each { |k, v|
    dputs(3) { "Saving #{v.class.name}" }
    v.save()
  }
  dputs(3) { "Time for saving everything: #{Time.now - start}" }
end

.service(s) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
# File 'lib/qooxview/entity.rb', line 317

def self.service(s)
  dputs(4) { "#{s}, #{@@services_hash["Entities.#{s}"].class.inspect}" }
  if (ret = @@services_hash["Entities.#{s}"]).class == Class
    dputs(0) { "#{s} is missing" }
    caller.each { |c|
      dputs(0) { c }
    }
    exit
  end
  ret
end

Instance Method Details

#get_block_fields(block) ⇒ Object

Gets all field names of a block



290
291
292
293
294
295
# File 'lib/qooxview/entity.rb', line 290

def get_block_fields(block)
  return [] unless @blocks.has_key? block.to_sym
  @blocks[block.to_sym].collect { |v|
    v.name
  }
end

#get_data_instance(k) ⇒ Object

Makes for a small proxy, in that only the needed classes are instantiated - useful for stuff like long LDAP-lists…



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/qooxview/entity.rb', line 174

def get_data_instance(k)
  return nil if !k
  if k.class.to_s !~ /(Integer|Fixnum)/
    dputs(0) { 'This is very bad' }
    dputs(0) { "value k is #{k.inspect} - #{k.class}" }
    dputs(0) { "caller-stack is #{caller}" }
    raise 'WrongIndex'
  end
  return nil if not k or not @data[k.to_i]
  if !@data_instances[k.to_i]
    old_loading = @loading
    @loading = true
    @data_instances[k.to_i]= @data_class.new(@data[k.to_i][@data_field_id], self)
    @data_instances[k.to_i].init_instance
    @loading = old_loading
  end
  return @data_instances[k.to_i]
end

#get_field_names(b = @blocks) ⇒ Object

Return an array of all available field-names as symbols



268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/qooxview/entity.rb', line 268

def get_field_names(b = @blocks)
  ret = b.collect { |c|
    if c.class == Array
      get_field_names(c)
    elsif c.class == Value
      c.name
    else
      nil
    end
  }
  ret = ret.select { |s| s }
  ret.flatten.collect { |c| c.to_sym }
end

#get_non_list_field_names(b = @blocks) ⇒ Object

Returns non-list field-names



283
284
285
286
287
# File 'lib/qooxview/entity.rb', line 283

def get_non_list_field_names(b = @blocks)
  get_field_names(b).select { |f|
    !%w(list select entity).index(get_value(f).dtype.to_s)
  }
end

#get_value(n, b = @blocks) ⇒ Object

Returns the Value for an entry



298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/qooxview/entity.rb', line 298

def get_value(n, b = @blocks)
  dputs(5) { "Name is #{n}" }
  n = n.to_sym
  b.each { |c|
    if c.class == Array
      v = get_value(n, c)
      dputs(5) { "Found array #{v.inspect}" }
      v and return v
    elsif c.class == Value
      if c.name.to_sym == n
        dputs(5) { "Found value #{c.inspect}" }
        return c
      end
    end
  }
  dputs(5) { 'Found nothing' }
  return nil
end

#match_by_id(k) ⇒ Object



193
194
195
# File 'lib/qooxview/entity.rb', line 193

def match_by_id(k)
  get_data_instance(k)
end

#respond_to?(cmd, all = false) ⇒ Boolean

Returns:

  • (Boolean)


255
256
257
258
259
260
261
# File 'lib/qooxview/entity.rb', line 255

def respond_to?(cmd, all = false)
  dputs(5) { cmd.inspect }
  if cmd =~ /^(match_by_|search_by_|list_|listp_|value_)/
    return true
  end
  super cmd, all
end

#setup_dataObject

Here comes the definition of the data used in that Entity. If the return-value is true, the data is loaded automatically



95
96
97
# File 'lib/qooxview/entity.rb', line 95

def setup_data
  return false
end

#singular(name) ⇒ Object

Gets the singular of a name



100
101
102
103
104
105
106
107
108
109
# File 'lib/qooxview/entity.rb', line 100

def singular(name)
  case name
    when /ies$/
      return name.sub(/ies$/, 'y')
    when /sses$/
      return name.sub(/es$/, '')
    when /s$/
      return name.chop
  end
end

#value_add(cmds, *args) ⇒ Object



138
139
140
# File 'lib/qooxview/entity.rb', line 138

def value_add(cmds, *args)
  value_add_(cmds.to_s.split('_'), args)
end

#value_add_(cmds, args) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/qooxview/entity.rb', line 125

def value_add_(cmds, args)
  value = Value.new(cmds, args, @default_type)
  @values.push value

  if not get_field_names.index value.name.to_sym
    # Prepare the entry in the blocks
    @blocks[@block_now].push(value)
  end

  # And add the entry in the DataHandler
  add_value_to_storage(value)
end

#value_block(*args) ⇒ Object

Starts a block of values to be displayed together. Takes optionally an argument for the permissions neede, might be the same as another permission. If no permission is given, the name of the block is used as the name of the permission. The final name of the permission is prepended by the name of the class!



120
121
122
123
# File 'lib/qooxview/entity.rb', line 120

def value_block(*args)
  @block_now = args[0]
  @blocks[@block_now] = []
end

#value_date(name) ⇒ Object

Adds a date-entry to the current block



153
154
155
# File 'lib/qooxview/entity.rb', line 153

def value_date(name)
  value_add(:date, name)
end

#value_entity(name, entity = name) ⇒ Object

Adds a reference to an entity, per default takes the name as the entity



168
169
170
# File 'lib/qooxview/entity.rb', line 168

def value_entity(name, entity = name)
  value_add("entity_#{entity}", name)
end

#value_int(name) ⇒ Object

Adds a int-entry to the current block



148
149
150
# File 'lib/qooxview/entity.rb', line 148

def value_int(name)
  value_add(:int, name)
end

#value_list_drop_(name, init) ⇒ Object

Adds a list which will show as a drop-down



163
164
165
# File 'lib/qooxview/entity.rb', line 163

def value_list_drop_(name, init)
  value_add(:list_drop, name, init)
end

#value_str(name) ⇒ Object

Adds a str-entry to the current block



143
144
145
# File 'lib/qooxview/entity.rb', line 143

def value_str(name)
  value_add(:str, name)
end

#value_time(name) ⇒ Object

Adds a date-entry to the current block



158
159
160
# File 'lib/qooxview/entity.rb', line 158

def value_time(name)
  value_add(:time, name)
end

#whoamiObject



263
264
265
# File 'lib/qooxview/entity.rb', line 263

def whoami
  dputs(0) { "I'm !*2@#" }
end