Class: Economic::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/economic/base.rb

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

.attributesObject (readonly)

Returns the value of attribute attributes.



8
9
10
# File 'lib/economic/base.rb', line 8

def attributes
  @attributes
end

.relationsObject (readonly)

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



112
113
114
115
# File 'lib/economic/base.rb', line 112

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
36
37
38
39
# 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
  if multiple
    attr_accessor economic_cased_attibute_name
    alias_method "#{snake_case(economic_cased_attibute_name)}=", "#{economic_cased_attibute_name}="
  else
    attr_reader economic_cased_attibute_name
  end
  alias_method snake_case(economic_cased_attibute_name), economic_cased_attibute_name
end

.snake_case(camel_cased) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/economic/base.rb', line 104

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

#dirty?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
# File 'lib/economic/base.rb', line 97

def dirty?
  self.class.attributes.each do |attribute|
    return true unless send(attribute) == @internal_hash[attribute]
  end
  false
end

#model_class(name) ⇒ Object



121
122
123
# File 'lib/economic/base.rb', line 121

def model_class(name)
  Object.const_get("Economic::" + name.slice(0, 1).capitalize + name.slice(1..-1))
end

#repoObject



117
118
119
# File 'lib/economic/base.rb', line 117

def repo
  Object.const_get("#{self.class}Repo")
end

#to_h(only_fields: []) ⇒ Object



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
92
93
94
95
# File 'lib/economic/base.rb', line 67

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
      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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/economic/base.rb', line 41

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