Class: Teron

Inherits:
Object
  • Object
show all
Extended by:
TeronAssociation, TeronField
Includes:
TeronRecords
Defined in:
lib/teron.rb

Overview

Initializtion Helper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TeronField

field

Methods included from TeronAssociation

belongs_to, has_many, has_one

Methods included from TeronRecords

#attributes, #destroy!, #reload, #save!

Constructor Details

#initialize(params = {}) ⇒ Teron

Returns a new instance of Teron.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/teron.rb', line 40

def initialize(params = {})
  self.class.class_variable_get(:@@params).each do |param|
    value = if params.key? param[:name]
              params[param[:name]]
            else
              default = param.dig(:options, :default)
              if default.instance_of?(Proc)
                default.call
              else
                # Ensure Deep Clone
                param.dig(:options, :dump) ? Marshal.load(Marshal.dump(default)) : default

              end
            end

    instance_variable_set("@#{param[:name]}", value) if value
  end
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



24
25
26
# File 'lib/teron.rb', line 24

def id
  @id
end

Class Method Details

._dataObject



60
61
62
# File 'lib/teron.rb', line 60

def _data
  class_variable_get(:@@data)
end

._mutexObject



69
70
71
# File 'lib/teron.rb', line 69

def _mutex
  class_variable_get(:@@mutex)
end

._obj_clear(obj_id) ⇒ Object



97
98
99
100
101
# File 'lib/teron.rb', line 97

def _obj_clear(obj_id)
  _mutex.synchronize do
    _object_list.delete obj_id if _object_list.key? obj_id
  end
end

._obj_create(obj_data) ⇒ Object



89
90
91
92
93
94
95
# File 'lib/teron.rb', line 89

def _obj_create(obj_data)
  _mutex.synchronize do
    _object_list[obj_data[:id]] = new obj_data unless _object_list.key? obj_data[:id]
  end

  _object_list[obj_data[:id]]
end

._object_listObject

Store of In Memory already loaded objects



65
66
67
# File 'lib/teron.rb', line 65

def _object_list
  class_variable_get(:@@object_list)
end

.allObject



126
127
128
129
130
# File 'lib/teron.rb', line 126

def all
  return [] if count.zero?

  _data.values.map { |x| _obj_create x }
end

.countObject



85
86
87
# File 'lib/teron.rb', line 85

def count
  _data.size
end

.find(key) ⇒ Object



132
133
134
135
136
137
138
139
# File 'lib/teron.rb', line 132

def find(key)
  return nil if key.nil?
  return nil if count.zero?

  return nil unless _data.key? key

  _obj_create _data[key]
end

.find_by(args) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/teron.rb', line 103

def find_by(args)
  return nil if args.nil?
  return nil if count.zero?

  key, value = args.first
  obj = _data.values.find { |x| x[key] == value }

  return nil unless obj

  _obj_create obj
end

.firstObject



73
74
75
76
77
# File 'lib/teron.rb', line 73

def first
  return nil if count.zero?

  _obj_create _data.values[0]
end

.lastObject



79
80
81
82
83
# File 'lib/teron.rb', line 79

def last
  return nil if count.zero?

  _obj_create _data.values[-1]
end

.where(args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
# File 'lib/teron.rb', line 115

def where(args)
  return [] if args.nil?
  return [] if count.zero?

  objs = _data.values.select do |value|
    args.all? { |k, v| value[k] == v }
  end

  objs.map { |x| _obj_create x }
end

Instance Method Details

#inspectObject



30
31
32
# File 'lib/teron.rb', line 30

def inspect
  "#<#{self.class.name} #{inspect_vars.join(', ')}>"
end

#inspect_varsObject



34
35
36
37
38
# File 'lib/teron.rb', line 34

def inspect_vars
  instance_variables.map do |name|
    "#{name}: #{instance_variable_get(name)}"
  end
end