Class: KBTable
- Inherits:
-
Object
- Object
- KBTable
- Includes:
- DRb::DRbUndumped
- Defined in:
- lib/og/store/kirby/kirbybase.rb
Overview
KBTable
Constant Summary collapse
- UNENCODE_RE =
Regular expression used to determine if field needs to be un-encoded.
/&(?:amp|linefeed|carriage_return|substitute|pipe);/
- TYPE_CONV =
{ :Integer => :Integer, :Float => :Float, :String => :unencode_str, :Boolean => :str_to_bool, :Date => :str_to_date, :DateTime => :str_to_datetime }
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.create_called_from_database_instance(db, name, filename) ⇒ Object
———————————————————————– create_called_from_database_instance ———————————————————————– ++ Return a new instance of KBTable.
-
.valid_field_type?(field_type) ⇒ Boolean
———————————————————————– KBTable.valid_field_type ———————————————————————– ++ Return true if valid field type.
Instance Method Summary collapse
-
#[](*index) ⇒ Object
———————————————————————– [] ———————————————————————– ++ Return the record(s) whose recno field is included in index.
-
#[]=(index, updates) ⇒ Object
———————————————————————– []= ———————————————————————– ++ Update record whose recno field equals index.
-
#clear(reset_recno_ctr = true) ⇒ Object
———————————————————————– clear ———————————————————————– ++ Delete all records from table.
-
#delete(&select_cond) ⇒ Object
———————————————————————– delete ———————————————————————– ++ Delete records from table and return # deleted.
-
#encrypted? ⇒ Boolean
———————————————————————– encrypted? ———————————————————————– ++ Returns true if table is encrypted.
-
#field_names ⇒ Object
———————————————————————– field_names ———————————————————————– ++ Return array containing table field names.
-
#field_types ⇒ Object
———————————————————————– field_types ———————————————————————– ++ Return array containing table field types.
-
#import_csv(csv_filename) ⇒ Object
———————————————————————– import_csv ———————————————————————– ++ Import csv file into table.
-
#initialize(db, name, filename) ⇒ KBTable
constructor
This has been declared private so user’s cannot create new instances of KBTable from their application.
-
#insert(*data, &insert_proc) ⇒ Object
———————————————————————– insert ———————————————————————– ++ Insert a new record into a table, return unique record number.
-
#pack ⇒ Object
———————————————————————– pack ———————————————————————– ++ Remove blank records from table, return total removed.
-
#select(*filter, &select_cond) ⇒ Object
———————————————————————– select ———————————————————————– ++ Return array of records (Structs) matching select conditions.
-
#set(recs, data) ⇒ Object
———————————————————————– set ———————————————————————– ++ Set fields of records to updated values.
-
#total_recs ⇒ Object
———————————————————————– total_recs ———————————————————————– ++ Return total number of undeleted (blank) records in table.
-
#update(*updates, &select_cond) ⇒ Object
———————————————————————– update ———————————————————————– ++ Return array of records (Structs) to be updated based on select cond.
-
#update_all(*updates) ⇒ Object
———————————————————————– update_all ———————————————————————– ++ Return array of records (Structs) to be updated, in this case all records.
Constructor Details
#initialize(db, name, filename) ⇒ KBTable
This has been declared private so user’s cannot create new instances of KBTable from their application. A user gets a handle to a KBTable instance by calling KirbyBase#get_table for an existing table or KirbyBase#create_table for a new table.
845 846 847 848 849 850 851 852 853 854 855 |
# File 'lib/og/store/kirby/kirbybase.rb', line 845 def initialize(db, name, filename) @db = db @name = name @filename = filename @encrypted = false # Alias delete_all to clear method. alias delete_all clear update_header_vars end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
811 812 813 |
# File 'lib/og/store/kirby/kirbybase.rb', line 811 def filename @filename end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
811 812 813 |
# File 'lib/og/store/kirby/kirbybase.rb', line 811 def name @name end |
Class Method Details
.create_called_from_database_instance(db, name, filename) ⇒ Object
create_called_from_database_instance
++ Return a new instance of KBTable. Should never be called directly by your application. Should only be called from KirbyBase#get_table.
- db
-
KirbyBase instance.
- name
-
Symbol specifying table name.
- filename
-
String specifying filename of physical file that holds table.
837 838 839 |
# File 'lib/og/store/kirby/kirbybase.rb', line 837 def KBTable.create_called_from_database_instance(db, name, filename) return new(db, name, filename) end |
Instance Method Details
#[](*index) ⇒ Object
++ Return the record(s) whose recno field is included in index.
- index
-
Array of Integer(s) specifying recno(s) you wish to select.
1072 1073 1074 1075 1076 1077 1078 1079 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1072 def [](*index) recs = select { |r| index.include?(r.recno) } if recs.size == 1 return recs[0] else return recs end end |
#[]=(index, updates) ⇒ Object
[]=
++ Update record whose recno field equals index.
- index
-
Integer specifying recno you wish to select.
- updates
-
Hash, Struct, or Array containing updates.
978 979 980 |
# File 'lib/og/store/kirby/kirbybase.rb', line 978 def []=(index, updates) return update(updates) { |r| r.recno == index } end |
#clear(reset_recno_ctr = true) ⇒ Object
clear
++ Delete all records from table. You can also use #delete_all.
- reset_recno_ctr
-
true/false specifying whether recno counter should be reset to 0.
1057 1058 1059 1060 1061 1062 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1057 def clear(reset_recno_ctr=true) delete { true } pack @db.engine.reset_recno_ctr if reset_recno_ctr end |
#delete(&select_cond) ⇒ Object
delete
++ Delete records from table and return # deleted.
- select_cond
-
Proc containing code to select records.
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1030 def delete(&select_cond) raise ArgumentError, "Must specify select condition code " + \ "block. To delete all records, use #clear instead." if \ select_cond.nil? # Update the header variables. update_header_vars # Get all records that match the selection criteria and # return them in an array. result_set = get_matches(:delete, [], select_cond) @db.engine.delete_records(self, result_set) # Return the number of records deleted. return result_set.size end |
#encrypted? ⇒ Boolean
encrypted?
++ Returns true if table is encrypted.
863 864 865 866 867 868 869 |
# File 'lib/og/store/kirby/kirbybase.rb', line 863 def encrypted? if @encrypted return true else return false end end |
#field_names ⇒ Object
field_names
++ Return array containing table field names.
877 878 879 880 |
# File 'lib/og/store/kirby/kirbybase.rb', line 877 def field_names update_header_vars return @field_names end |
#field_types ⇒ Object
field_types
++ Return array containing table field types.
888 889 890 891 |
# File 'lib/og/store/kirby/kirbybase.rb', line 888 def field_types update_header_vars return @field_types end |
#import_csv(csv_filename) ⇒ Object
import_csv
++ Import csv file into table.
- csv_filename
-
filename of csv file to import.
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1136 def import_csv(csv_filename) type_convs = @field_types.collect { |x| TYPE_CONV[x] } CSV.open(csv_filename, 'r') do |row| temp_row = [] (0...@field_names.size-1).each { |x| if row[x].to_s == '' temp_row << nil else temp_row << send(type_convs[x+1], row[x].to_s) end } insert(*temp_row) end end |
#insert(*data, &insert_proc) ⇒ Object
insert
++ Insert a new record into a table, return unique record number.
- data
-
Array, Hash, Struct instance containing field values of
new record.
- insert_proc
-
Proc instance containing insert code. This and the data parameter are mutually exclusive.
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/og/store/kirby/kirbybase.rb', line 904 def insert(*data, &insert_proc) raise 'Cannot specify both a hash/array/struct and a ' + \ 'proc for method #insert!' unless data.empty? or insert_proc.nil? raise 'Must specify either hash/array/struct or insert ' + \ 'proc for method #insert!' if data.empty? and insert_proc.nil? # Update the header variables. update_header_vars # Convert input, which could be an array, a hash, or a Struct # into a common format (i.e. hash). if data.empty? input_rec = convert_input_data(insert_proc) else input_rec = convert_input_data(data) end # Check the field values to make sure they are proper types. validate_input(input_rec) return @db.engine.insert_record(self, @field_names.collect { |f| input_rec.fetch(f, '') }) end |
#pack ⇒ Object
pack
++ Remove blank records from table, return total removed.
1114 1115 1116 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1114 def pack return @db.engine.pack_table(self) end |
#select(*filter, &select_cond) ⇒ Object
select
++ Return array of records (Structs) matching select conditions.
- filter
-
List of field names (Symbols) to include in result set.
- select_cond
-
Proc containing select code.
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1090 def select(*filter, &select_cond) # Declare these variables before the code block so they don't go # after the code block is done. result_set = [] # Update the header variables. update_header_vars # Validate that all names in filter are valid field names. validate_filter(filter) filter = @field_names if filter.empty? # Get all records that match the selection criteria and # return them in an array of Struct instances. return get_matches(:select, filter, select_cond) end |
#set(recs, data) ⇒ Object
set
++ Set fields of records to updated values. Returns number of records updated.
- recs
-
Array of records (Structs) that will be updated.
- data
-
Hash, Struct, Proc containing updates.
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
# File 'lib/og/store/kirby/kirbybase.rb', line 992 def set(recs, data) # Update the header variables. update_header_vars # Convert updates, which could be an array, a hash, or a Struct # into a common format (i.e. hash). update_rec = convert_input_data(data) validate_input(update_rec) updated_recs = [] recs.each do |rec| updated_rec = {} updated_rec[:rec] = @field_names.collect { |f| if update_rec.has_key?(f) update_rec[f] else rec.send(f) end } updated_rec[:fpos] = rec.fpos updated_rec[:line_length] = rec.line_length updated_recs << updated_rec end @db.engine.update_records(self, updated_recs) # Return the number of records updated. return recs.size end |
#total_recs ⇒ Object
total_recs
++ Return total number of undeleted (blank) records in table.
1124 1125 1126 |
# File 'lib/og/store/kirby/kirbybase.rb', line 1124 def total_recs return @db.engine.get_total_recs(self) end |
#update(*updates, &select_cond) ⇒ Object
update
++ Return array of records (Structs) to be updated based on select cond.
- updates
-
Hash or Struct containing updates.
- select_cond
-
Proc containing code to select records to update.
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 |
# File 'lib/og/store/kirby/kirbybase.rb', line 952 def update(*updates, &select_cond) raise ArgumentError, "Must specify select condition code " + \ "block. To update all records, use #update_all instead." if \ select_cond.nil? # Update the header variables. update_header_vars # Get all records that match the selection criteria and # return them in an array. result_set = get_matches(:update, @field_names, select_cond) return result_set if updates.empty? set(result_set, updates) end |
#update_all(*updates) ⇒ Object
update_all
++ Return array of records (Structs) to be updated, in this case all records.
- updates
-
Hash or Struct containing updates.
939 940 941 |
# File 'lib/og/store/kirby/kirbybase.rb', line 939 def update_all(*updates) update(*updates) { true } end |