Class: Economic::Base
- Inherits:
-
Object
show all
- Defined in:
- lib/economic/base.rb
Direct Known Subclasses
Account, AccountingYear, AccountsSummed, Attention, Company, ContraAccount, CostAccount, Currency, Customer, CustomerContact, CustomerGroup, Delivery, Department, DepartmentalDistribution, Distribution, Inventory, Invoice, Journal, Layout, Line, Notes, Order, PaymentTerms, PaymentType, Pdf, Pricing, Product, ProductGroup, Project, Recipient, References, RemittanceAdvice, SalesPerson, Self, Supplier, SupplierContact, SupplierGroup, TotalFromAccount, Unit, User, VatAccount, VatType, VatZone, VendorReference, Voucher
Class Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash = {}) ⇒ Base
Returns a new instance of Base.
3
4
5
|
# File 'lib/economic/base.rb', line 3
def initialize(hash = {})
values_based_on_hash(hash)
end
|
Class Attribute Details
.attributes ⇒ Object
Returns the value of attribute attributes.
8
9
10
|
# File 'lib/economic/base.rb', line 8
def attributes
@attributes
end
|
.relations ⇒ Object
Returns the value of attribute relations.
8
9
10
|
# File 'lib/economic/base.rb', line 8
def relations
@relations
end
|
Class Method Details
.add_attribute(name) ⇒ Object
11
12
13
|
# File 'lib/economic/base.rb', line 11
def self.add_attribute(name)
(@attributes ||= []).push(name)
end
|
.add_relation(name, fields, multiple) ⇒ Object
15
16
17
|
# File 'lib/economic/base.rb', line 15
def self.add_relation(name, fields, multiple)
(@relations ||= []).push(name: name, fields: fields, multiple: multiple)
end
|
.field(name, id: false) ⇒ Object
19
20
21
22
23
24
25
26
27
|
# File 'lib/economic/base.rb', line 19
def self.field(name, id: false)
economic_cased_attibute_name = name.to_s
attr_accessor economic_cased_attibute_name
alias_method snake_case(economic_cased_attibute_name), economic_cased_attibute_name
alias_method "#{snake_case(economic_cased_attibute_name)}=", "#{economic_cased_attibute_name}="
alias_method "id_key", economic_cased_attibute_name if id
alias_method "id_key=", "#{economic_cased_attibute_name}=" if id
add_attribute economic_cased_attibute_name
end
|
.low_camel_case(snake_cased) ⇒ Object
101
102
103
104
|
# File 'lib/economic/base.rb', line 101
def self.low_camel_case(snake_cased)
camel = snake_cased.split("_").collect(&:capitalize).join
camel[0, 1].downcase + camel[1..-1]
end
|
.relation(name, fields:, multiple: false) ⇒ Object
29
30
31
32
33
34
35
|
# File 'lib/economic/base.rb', line 29
def self.relation(name, fields:, multiple: false)
economic_cased_attibute_name = name.to_s
add_relation economic_cased_attibute_name, fields, multiple
attr_accessor economic_cased_attibute_name
alias_method "#{snake_case(economic_cased_attibute_name)}=", "#{economic_cased_attibute_name}="
alias_method snake_case(economic_cased_attibute_name), economic_cased_attibute_name
end
|
.snake_case(camel_cased) ⇒ Object
93
94
95
96
97
98
99
|
# File 'lib/economic/base.rb', line 93
def self.snake_case(camel_cased)
camel_cased.to_s.gsub(/::/, "/")
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr("-", "_")
.downcase
end
|
Instance Method Details
#model_class(name) ⇒ Object
110
111
112
|
# File 'lib/economic/base.rb', line 110
def model_class(name)
Object.const_get("Economic::" + name.slice(0, 1).capitalize + name.slice(1..-1))
end
|
#repo ⇒ Object
106
107
108
|
# File 'lib/economic/base.rb', line 106
def repo
Object.const_get("#{self.class}Repo")
end
|
#to_h(only_fields: []) ⇒ Object
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/economic/base.rb', line 63
def to_h(only_fields: [])
return_hash = {}
self.class.attributes.each do |field_name|
next if only_fields.any? && !only_fields.include?(field_name.to_sym)
return_hash[field_name] = public_send(field_name) if public_send(field_name)
end
self.class.relations&.each do |relation_hash|
if relation_hash[:multiple]
relation_name = relation_hash[:name]
relation_fields = relation_hash[:fields]
begin
arr = public_send(relation_name).map { |relation| relation.to_h(only_fields: relation_fields) }
return_hash[relation_name] = arr unless arr.empty?
rescue NoMethodError
end
else
relation_name = relation_hash[:name]
relation_fields = relation_hash[:fields]
relation_data = public_send(relation_name).to_h(only_fields: relation_fields)
return_hash[relation_name] = relation_data unless relation_data.empty?
end
end
return_hash
end
|
#values_based_on_hash(hash) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/economic/base.rb', line 37
def values_based_on_hash(hash)
@internal_hash = hash || {}
self.class.attributes.each do |field_name|
public_send("#{field_name}=", @internal_hash[field_name] || @internal_hash[field_name.to_sym])
end
self.class.relations&.each do |relation_hash|
relation_name = relation_hash[:name]
if relation_hash[:multiple]
model_name = relation_hash[:name].singularize
related_model_array = Array(@internal_hash[relation_name])&.map { |data|
model_class(model_name).new(data)
}
instance_variable_set("@#{relation_name}", related_model_array)
else
related_model = model_class(relation_name).new(@internal_hash[relation_name])
instance_variable_set("@#{relation_name}", related_model)
end
if related_model
unless @internal_hash[relation_name.to_sym].blank?
related_model.values_based_on_hash(@internal_hash[relation_name.to_sym].to_h)
end
end
end
end
|