Module: Elements::Util

Defined in:
lib/elements/util.rb

Class Method Summary collapse

Class Method Details

.convert_to_elements_object(data) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elements/util.rb', line 25

def self.convert_to_elements_object(data)
  case data
  when Hash
    data = symbolize_names(data)
    klass = object_classes.fetch(data[:type], ElementsObject)
    id = data.delete(:id)
    attributes = {}
    data.each do |key, value|
      attributes[key] = convert_to_elements_object(value)
    end
    klass.new(id, attributes)
  when Array
    data.map { |i| convert_to_elements_object(i) }
  else
    data
  end
end

.monotonic_timeObject



47
48
49
# File 'lib/elements/util.rb', line 47

def self.monotonic_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

.object_classesObject



43
44
45
# File 'lib/elements/util.rb', line 43

def self.object_classes
  @object_classes ||= Elements::ObjectTypes.object_names_to_classes
end

.symbolize_names(object) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/elements/util.rb', line 5

def self.symbolize_names(object)
  case object
  when Hash
    new_hash = {}
    object.each do |key, value|
      key = (begin
        key.to_sym
      rescue StandardError
        key
      end) || key
      new_hash[key] = symbolize_names(value)
    end
    new_hash
  when Array
    object.map { |value| symbolize_names(value) }
  else
    object
  end
end