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 +4 -4
- data/lib/feidee_utils/account.rb +2 -2
- data/lib/feidee_utils/account_group.rb +1 -1
- data/lib/feidee_utils/category.rb +1 -1
- data/lib/feidee_utils/database.rb +1 -1
- data/lib/feidee_utils/record/accessors.rb +28 -0
- data/lib/feidee_utils/record/namespaced.rb +7 -1
- data/lib/feidee_utils/record/persistent.rb +21 -11
- data/lib/feidee_utils/record.rb +15 -4
- data/lib/feidee_utils/transaction.rb +1 -1
- data/lib/feidee_utils/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13652aed513e122b73d0f6a0c8444038f62e6ce7
|
4
|
+
data.tar.gz: 7e6471828e9b63c29d0509cbe0a4623fdfcb1447
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9850f54ef6101377e427e5074e7070660283713b9333ee8f289471fcf01f6817adc40989e161ab35e945b9ba494d9be52c74e67fb97485428e09833a5555d581
|
7
|
+
data.tar.gz: 463fba9a6616c73a0333338eee7610a22476dfea791be9acc5e4152313f286f7b5fe1da28d973c100966e23fa1cfba7dd774dddadd06292ec9b4d77760a558e1
|
data/lib/feidee_utils/account.rb
CHANGED
@@ -62,7 +62,7 @@ module FeideeUtils
|
|
62
62
|
"clientID", # Always equal to poid.
|
63
63
|
].freeze
|
64
64
|
|
65
|
-
|
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(
|
114
|
+
arr << self.class.new(raw_row)
|
115
115
|
end
|
116
116
|
end
|
117
117
|
arr
|
@@ -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.
|
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
|
10
|
+
def generate_names
|
9
11
|
entity_name =
|
10
|
-
if i =
|
11
|
-
|
12
|
+
if i = self.name.rindex("::")
|
13
|
+
self.name[(i+2)..-1]
|
12
14
|
else
|
13
|
-
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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(
|
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(
|
62
|
+
self.new(raw_row)
|
53
63
|
end
|
54
64
|
|
55
65
|
def find(id)
|
data/lib/feidee_utils/record.rb
CHANGED
@@ -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(
|
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
|
-
|
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 =
|
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
|
data/lib/feidee_utils/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2018-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|