Class: StorageType

Inherits:
Object show all
Defined in:
lib/qooxview/storage_type.rb

Overview

Interface-class for entities. Must provide:

* init( name ) - initialises the data using "name" as the name of the class
* addField( name, type ) - adds a field to the data
* setField( name, data ) - sets the data of a field
* getField( name ) - returns the data of a field
* save - saves the whole entity
* load - loads the whole entity
* search - searches in the whole entity

Direct Known Subclasses

CSV, LDAP, SQLite

Constant Summary collapse

@@types =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, config = {}) ⇒ StorageType

Returns a new instance of StorageType.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/qooxview/storage_type.rb', line 17

def initialize( entity, config = {} )
  @name = entity.name
  @entity = entity
  @data_field_id = entity.data_field_id
  @fields = {}
  @data_cache = true
  
  class_name = self.class.name.to_sym
  if get_config( false, :StorageType, class_name )
    config = $config[:StorageType][class_name].merge( config )
  end
  
  @config = config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *arg) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/qooxview/storage_type.rb', line 83

def method_missing( name, *arg )
  case name
    when /^(init|addField|setField|getField|save|load|search)$/
    dputs( 0 ){ "Must provide #{name}!" }
    exit 0
  else
    super( name, arg )
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/qooxview/storage_type.rb', line 15

def config
  @config
end

#data_cacheObject (readonly)

Returns the value of attribute data_cache.



15
16
17
# File 'lib/qooxview/storage_type.rb', line 15

def data_cache
  @data_cache
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/qooxview/storage_type.rb', line 15

def name
  @name
end

Class Method Details

.data_loadObject



129
130
131
# File 'lib/qooxview/storage_type.rb', line 129

def self.data_load
  
end

.data_save(index = nil) ⇒ Object



125
126
127
# File 'lib/qooxview/storage_type.rb', line 125

def self.data_save( index = nil )
  
end

.has?(t) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.has?( t )
  t and @@types.has_key? t.to_sym
end

.inherited(subclass) ⇒ Object



115
116
117
118
119
# File 'lib/qooxview/storage_type.rb', line 115

def self.inherited( subclass )
  dputs( 2 ){ "Added #{subclass} to StorageTypes" }
  @@types[subclass.name.to_sym] = subclass
  super( subclass )
end

.new_st(st, entity, config = {}) ⇒ Object



109
110
111
112
113
# File 'lib/qooxview/storage_type.rb', line 109

def self.new_st( st, entity, config = {} )
  if StorageType.has? st
    return @@types[st.to_sym].new( entity, config )
  end
end

Instance Method Details

#add_field(name, args) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/qooxview/storage_type.rb', line 40

def add_field( name, args )
  dputs( 3 ){ "Adding field #{name}" }
  if not has_field name
    @fields[name.to_sym] = args
  else
    @fields[name.to_sym].merge!( args )
  end
end

#configure(config) ⇒ Object

By default use configuration-options to overwrite class-variables



33
34
35
36
37
38
# File 'lib/qooxview/storage_type.rb', line 33

def configure( config )
  config.each{|k,v|
    dputs( 3 ){ "Putting configuration #{v.inspect} for #{k.inspect}" }
    eval "@#{k} = v"
  }
end

#data_create(data) ⇒ Object

Before a new data-set is created, ever StorageType has the possibility to adjust the data



99
100
# File 'lib/qooxview/storage_type.rb', line 99

def data_create( data )  
end

#data_double(data) ⇒ Object

If a double-entry has been detected after creation



103
104
# File 'lib/qooxview/storage_type.rb', line 103

def data_double( data )  
end

#data_each(data) ⇒ Object

Calls the block for each data that has more than one key in it



69
70
71
72
73
74
75
76
77
# File 'lib/qooxview/storage_type.rb', line 69

def data_each( data )
  dputs( 5 ){ data.inspect }
  dputs( 5 ){ data.values.inspect }
  data.values.sort{|s, t|
    s[@data_field_id].to_i <=> t[@data_field_id].to_i
  }.each{|d|
    yield d
  }
end

#delete(id) ⇒ Object



106
107
# File 'lib/qooxview/storage_type.rb', line 106

def delete(id)
end

#delete_all(local_only = false) ⇒ Object

Should delete all stored values - only really used for tests



94
95
# File 'lib/qooxview/storage_type.rb', line 94

def delete_all( local_only = false )
end

#extract_data_old(d) ⇒ Object

Returns only the relevant part of the data



60
61
62
63
64
65
66
# File 'lib/qooxview/storage_type.rb', line 60

def extract_data_old( d )
  ret = {}
   ( @fields.keys + [ @data_field_id ] ).uniq.each{|k| 
    ret.merge! k => d[k]
  }
  ret
end

#field_args(name) ⇒ Object



53
54
55
56
57
# File 'lib/qooxview/storage_type.rb', line 53

def field_args( name )
  if has_field name
    return @fields[name.to_sym].dup
  end
end

#has_field(name) ⇒ Object



49
50
51
# File 'lib/qooxview/storage_type.rb', line 49

def has_field( name )
  @fields.has_key? name.to_sym
end

#set_entry(data, field, value) ⇒ Object



79
80
81
# File 'lib/qooxview/storage_type.rb', line 79

def set_entry( data, field, value )
  return value
end