feidee_utils 0.0.5.3 → 0.0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52939cfeaaed123fbd26ae21d28323de5e4f9315
4
- data.tar.gz: e9fea8a9ebda721a02a0a71a49a4aed8b4382bad
3
+ metadata.gz: 13652aed513e122b73d0f6a0c8444038f62e6ce7
4
+ data.tar.gz: 7e6471828e9b63c29d0509cbe0a4623fdfcb1447
5
5
  SHA512:
6
- metadata.gz: 262d1e6c897f1ad951605df672c58d1f2058eb5d28554754100e8ee5e340606533a0e0dad838d01db0738b960df6b6912be520a60367a67e34a41ed1055c0331
7
- data.tar.gz: 74daf0b8276450c6359668881731ba86f2339a882340098457e91c2dabcd251fc2c0cfd231057de214de03acc626adedb5588e831f163dd511587e5fa13bf85a
6
+ metadata.gz: 9850f54ef6101377e427e5074e7070660283713b9333ee8f289471fcf01f6817adc40989e161ab35e945b9ba494d9be52c74e67fb97485428e09833a5555d581
7
+ data.tar.gz: 463fba9a6616c73a0333338eee7610a22476dfea791be9acc5e4152313f286f7b5fe1da28d973c100966e23fa1cfba7dd774dddadd06292ec9b4d77760a558e1
@@ -62,7 +62,7 @@ module FeideeUtils
62
62
  "clientID", # Always equal to poid.
63
63
  ].freeze
64
64
 
65
- define_accessors(FieldMappings)
65
+ register_indexed_accessors(FieldMappings)
66
66
 
67
67
  # NOTE: balance is not set for credit cards etc. Instead
68
68
  # credit/debit are used.
@@ -111,7 +111,7 @@ module FeideeUtils
111
111
  "SELECT * FROM #{self.class.table_name} WHERE parent = ?", poid
112
112
  ) do |result|
113
113
  result.each do |raw_row|
114
- arr << self.class.new(result.columns, result.types, raw_row)
114
+ arr << self.class.new(raw_row)
115
115
  end
116
116
  end
117
117
  arr
@@ -31,7 +31,7 @@ module FeideeUtils
31
31
  "clientID", # Always equal to poid.
32
32
  ].freeze
33
33
 
34
- define_accessors(FieldMappings)
34
+ register_indexed_accessors(FieldMappings)
35
35
 
36
36
  define_type_enum({
37
37
  0 => :asset,
@@ -59,7 +59,7 @@ module FeideeUtils
59
59
  "clientID", # Always equal to poid.
60
60
  ].freeze
61
61
 
62
- define_accessors(FieldMappings)
62
+ register_indexed_accessors(FieldMappings)
63
63
 
64
64
  define_type_enum({
65
65
  0 => :expenditure,
@@ -40,7 +40,7 @@ module FeideeUtils
40
40
  drop_unused_tables if strip
41
41
 
42
42
  # TODO: make Ledger a first class object.
43
- @ledger = Record.generate_namespaced_record_classes(self)
43
+ @ledger = Record.generate_subclasses(self)
44
44
  end
45
45
 
46
46
  def sqlite_backup(dest_file_path)
@@ -10,6 +10,9 @@ module FeideeUtils
10
10
  end
11
11
 
12
12
  module ClassMethods
13
+
14
+ private
15
+
13
16
  def define_accessors field_mappings
14
17
  field_mappings.each do |name, key|
15
18
  if method_defined? name
@@ -36,6 +39,31 @@ module FeideeUtils
36
39
  self.class.environment.const_get(target_class_name).find_by_id(poid)
37
40
  end
38
41
  end
42
+
43
+ def register_indexed_accessors field_mappings
44
+ # The indexes of those columns are unknown until we see the schema.
45
+ const_set :IndexedAccessorFieldMappings, field_mappings
46
+ end
47
+
48
+ protected
49
+ # NOTE: Here we assume the underlaying database schema does not change.
50
+ # The assumption is safe in the sense that it is generally expected to
51
+ # restart and/or recompile your application after updating the schema.
52
+ def define_indexed_accessors
53
+ return if !const_defined? :IndexedAccessorFieldMappings
54
+
55
+ self::IndexedAccessorFieldMappings.each do |name, column_name|
56
+ if method_defined? name
57
+ raise "Accessor #{name} already exists in #{self.name}."
58
+ end
59
+
60
+ index = self.column_names.index column_name
61
+ if index.nil?
62
+ raise "Cannot find column #{column_name} in #{inspect}."
63
+ end
64
+ define_method name do column_at_index(index) end
65
+ end
66
+ end
39
67
  end
40
68
  end
41
69
  end
@@ -6,6 +6,8 @@ module FeideeUtils
6
6
  module ClassMethods
7
7
  attr_reader :child_classes
8
8
 
9
+ private
10
+
9
11
  # Must be invoked by Record.inherited
10
12
  def collect_subclass(child_class)
11
13
  @child_classes ||= Set.new
@@ -23,7 +25,7 @@ module FeideeUtils
23
25
  define_method("environment") { mod }
24
26
  })
25
27
 
26
- this.child_classes.each do |child_class|
28
+ @contained_classes = this.child_classes.map do |child_class|
27
29
  if child_class.name.start_with? FeideeUtils.name
28
30
  class_name = child_class.name.sub(/#{FeideeUtils.name}::/, '')
29
31
  # Generate a const for the child class
@@ -32,6 +34,10 @@ module FeideeUtils
32
34
  })
33
35
  end
34
36
  end
37
+
38
+ def self.contained_classes
39
+ @contained_classes
40
+ end
35
41
  end
36
42
  end
37
43
  end
@@ -3,25 +3,35 @@ module FeideeUtils
3
3
  class Record
4
4
  module Persistent
5
5
  module ClassMethods
6
+ protected
7
+
6
8
  # Names
7
9
  # Must be invoked by Record.inherited
8
- def genereate_names subclass
10
+ def generate_names
9
11
  entity_name =
10
- if i = subclass.name.rindex("::")
11
- subclass.name[(i+2)..-1]
12
+ if i = self.name.rindex("::")
13
+ self.name[(i+2)..-1]
12
14
  else
13
- subclass.name
15
+ self.name
14
16
  end
15
17
 
16
18
  id_field_name = entity_name.sub(/^[A-Z]/) { $&.downcase } + "POID"
17
19
  entity_name_underscore =
18
20
  entity_name.gsub(/([a-z\d])([A-Z\d])/, '\1_\2').downcase
19
21
  table_name = "t_" + entity_name_underscore
20
- subclass.class_exec do
21
- define_singleton_method :entity_name do entity_name end
22
- define_singleton_method :id_field_name do id_field_name end
23
- define_singleton_method :table_name do table_name end
24
- end
22
+ define_singleton_method :entity_name do entity_name end
23
+ define_singleton_method :id_field_name do id_field_name end
24
+ define_singleton_method :table_name do table_name end
25
+ end
26
+
27
+ public
28
+
29
+ def columns
30
+ database.table_info(self.table_name)
31
+ end
32
+
33
+ def column_names
34
+ @column_names ||= self.columns.map do |entry| entry["name"] end.freeze
25
35
  end
26
36
 
27
37
  # Persistent
@@ -29,7 +39,7 @@ module FeideeUtils
29
39
  arr = []
30
40
  database.query("SELECT * FROM #{self.table_name}") do |result|
31
41
  result.each do |raw_row|
32
- arr << self.new(result.columns, result.types, raw_row)
42
+ arr << self.new(raw_row)
33
43
  end
34
44
  end
35
45
  arr
@@ -49,7 +59,7 @@ module FeideeUtils
49
59
  "in table #{self.table_name}."
50
60
  end
51
61
 
52
- self.new(raw_result.columns, raw_result.types, raw_row)
62
+ self.new(raw_row)
53
63
  end
54
64
 
55
65
  def find(id)
@@ -24,8 +24,7 @@ module FeideeUtils
24
24
  # is copied to the new namespace, with it's database method overloaded.
25
25
  class Record
26
26
  public
27
- def initialize(columns, types, row)
28
- @columns = columns.freeze
27
+ def initialize(row)
29
28
  @row = row.freeze
30
29
 
31
30
  validate_integrity
@@ -39,6 +38,14 @@ module FeideeUtils
39
38
  # Do nothing.
40
39
  end
41
40
 
41
+ def self.generate_subclasses db
42
+ env = generate_namespaced_record_classes db
43
+ env.contained_classes.each do |klass|
44
+ klass.define_indexed_accessors
45
+ end
46
+ env
47
+ end
48
+
42
49
  class << self
43
50
  protected
44
51
  def database
@@ -49,7 +56,7 @@ module FeideeUtils
49
56
  def inherited subclass
50
57
  if subclass.name != nil and subclass.name.start_with? FeideeUtils.name
51
58
  collect_subclass subclass
52
- genereate_names subclass
59
+ subclass.generate_names
53
60
  end
54
61
  end
55
62
  end
@@ -74,10 +81,14 @@ module FeideeUtils
74
81
  # few characters. The overhead is relatively low.
75
82
  # In fact, a downstream benchmark showed that it is faster than building a
76
83
  # hash upfront and lookup the hash here.
77
- index = @columns.index key
84
+ index = self.class.column_names.index key
78
85
  return nil if index.nil?
79
86
  @row[index]
80
87
  end
81
88
 
89
+ def column_at_index index
90
+ @row[index]
91
+ end
92
+
82
93
  end
83
94
  end
@@ -122,7 +122,7 @@ module FeideeUtils
122
122
  "FSourceKey", # WTF
123
123
  ].freeze
124
124
 
125
- define_accessors(FieldMappings)
125
+ register_indexed_accessors(FieldMappings)
126
126
 
127
127
  define_type_enum({
128
128
  0 => :expenditure,
@@ -1,3 +1,3 @@
1
1
  module FeideeUtils
2
- VERSION = '0.0.5.3'
2
+ VERSION = '0.0.6.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: feidee_utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5.3
4
+ version: 0.0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liqing Muyi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-26 00:00:00.000000000 Z
11
+ date: 2018-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake