Class: AuroraDataApi::Model
- Inherits:
-
Object
- Object
- AuroraDataApi::Model
show all
- Defined in:
- lib/aurora-data-api/model.rb
Constant Summary
collapse
- SCHEMA =
{}
- STRUCTS =
{}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(**params) ⇒ Model
Returns a new instance of Model.
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/aurora-data-api/model.rb', line 55
def initialize(**params)
@struct = STRUCTS[self.class.model_name].new
@timestamp = %i[created_at updated_at].all? { |m| members.include?(m) }
params.each do |col, val|
if col == literal_id
@id = val if val.is_a?(Integer)
else
@struct[col] = val
end
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/aurora-data-api/model.rb', line 86
def method_missing(method_name, *args, &block)
string_name = method_name.to_s
if string_name[-1] == "="
@struct[string_name.chop] = args[0]
elsif string_name[-3, 3] == "_#{literal_id}"
@struct[string_name.sub(/_#{literal_id}\z/, "")].then do |rel|
rel.is_a?(Model) ? rel.id : @struct[method_name]
end
elsif method_name == literal_id
@id
elsif members.include?(method_name)
if @struct[method_name].nil?
col_id = "#{method_name}_#{literal_id}".to_sym
if members.include?(col_id) && @struct[col_id]
col_model = SCHEMA[self.class.to_s.to_sym][:cols].dig(method_name, :type)
return nil unless col_model
@struct[method_name] = AuroraDataApi.const_get("#{col_model}Depot".to_sym).select(
%(where "#{literal_id}" = :id), id: @struct[col_id]
)[0]
end
else
@struct[method_name]
end
else
super
end
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
68
69
70
|
# File 'lib/aurora-data-api/model.rb', line 68
def id
@id
end
|
#timestamp ⇒ Object
Returns the value of attribute timestamp.
68
69
70
|
# File 'lib/aurora-data-api/model.rb', line 68
def timestamp
@timestamp
end
|
Class Method Details
.col(name, type, opt = {}) ⇒ Object
32
33
34
35
36
37
38
39
|
# File 'lib/aurora-data-api/model.rb', line 32
def self.col(name, type, opt = {})
SCHEMA[model_name] ||= {}
SCHEMA[model_name][:cols] ||= {}
if type.is_a? Symbol
SCHEMA[model_name][:cols]["#{name}_#{SCHEMA[model_name][:literal_id] || :id}".to_sym] = {type: Integer}
end
SCHEMA[model_name][:cols][name] = {type: type, opt: opt}
end
|
.literal_id(lit) ⇒ Object
12
13
14
15
|
# File 'lib/aurora-data-api/model.rb', line 12
def self.literal_id(lit)
SCHEMA[model_name] ||= {}
SCHEMA[model_name][:literal_id] = lit
end
|
.model_name ⇒ Object
8
9
10
|
# File 'lib/aurora-data-api/model.rb', line 8
def self.model_name
to_s.to_sym
end
|
.relationship_by(table_sym) ⇒ Object
46
47
48
49
50
51
52
53
|
# File 'lib/aurora-data-api/model.rb', line 46
def self.relationship_by(table_sym)
result = Model::SCHEMA[to_s.to_sym].select { |_k, v|
v.is_a? Hash
}.select { |_k, v|
v.dig(:opt, :table) == table_sym
}
result.count > 0 ? [result.keys[0], result.values[0][:type]] : nil
end
|
.schema(&block) ⇒ Object
26
27
28
29
30
|
# File 'lib/aurora-data-api/model.rb', line 26
def self.schema(&block)
block.call
STRUCTS[model_name] = Struct.new(model_name.to_s, *SCHEMA[model_name][:cols].keys)
table("#{name.to_s.downcase}s".to_sym) if table_name.nil?
end
|
.table(name) ⇒ Object
21
22
23
24
|
# File 'lib/aurora-data-api/model.rb', line 21
def self.table(name)
SCHEMA[model_name] ||= {}
SCHEMA[model_name][:table_name] = name
end
|
.table_name ⇒ Object
17
18
19
|
# File 'lib/aurora-data-api/model.rb', line 17
def self.table_name
SCHEMA.dig(model_name, :table_name)
end
|
.timestamp ⇒ Object
41
42
43
44
|
# File 'lib/aurora-data-api/model.rb', line 41
def self.timestamp
col :created_at, Time, null: false
col :updated_at, Time, null: false
end
|
Instance Method Details
#_destroy ⇒ Object
147
148
149
150
151
152
153
|
# File 'lib/aurora-data-api/model.rb', line 147
def _destroy
@id = nil
@struct.members.each do |member|
@struct[member] = nil unless member == literal_id
end
true
end
|
#_set_id(id) ⇒ Object
143
144
145
|
# File 'lib/aurora-data-api/model.rb', line 143
def _set_id(id)
@id = id
end
|
#attributes ⇒ Object
132
133
134
135
136
137
138
139
140
141
|
# File 'lib/aurora-data-api/model.rb', line 132
def attributes
{}.tap do |hash|
members.each do |member|
next if member.to_s.match?(/_#{literal_id}\z/)
send(member).then do |v|
hash[member] = v.is_a?(Model) ? v.attributes : v
end
end
end
end
|
#build_params(include_id: false) ⇒ Object
122
123
124
125
126
127
128
129
130
|
# File 'lib/aurora-data-api/model.rb', line 122
def build_params(include_id: false)
{}.tap do |hash|
members.each do |member|
next if member == literal_id && !include_id
next if SCHEMA[self.class.to_s.to_sym][:cols][member][:type].is_a? Symbol
send(member).then { |v| hash[member] = v }
end
end
end
|
#literal_id ⇒ Object
118
119
120
|
# File 'lib/aurora-data-api/model.rb', line 118
def literal_id
SCHEMA[self.class.model_name][:literal_id] || :id
end
|
#members ⇒ Object
77
78
79
|
# File 'lib/aurora-data-api/model.rb', line 77
def members
[literal_id] + @struct.members
end
|
#respond_to_missing?(symbol, include_private) ⇒ Boolean
81
82
83
84
|
# File 'lib/aurora-data-api/model.rb', line 81
def respond_to_missing?(symbol, include_private)
!(%i[encoding].include? symbol)
end
|
#set_timestamp(at:) ⇒ Object
70
71
72
73
74
75
|
# File 'lib/aurora-data-api/model.rb', line 70
def set_timestamp(at:)
return false unless @timestamp
self.updated_at = Time.now
self.created_at = Time.now if at == :create
true
end
|
#table_name ⇒ Object
114
115
116
|
# File 'lib/aurora-data-api/model.rb', line 114
def table_name
SCHEMA[self.class.to_s.to_sym][:table_name]
end
|