Class: Railscope::EntryData

Inherits:
Object
  • Object
show all
Defined in:
lib/railscope/entry_data.rb

Overview

Plain Ruby object representing an entry, used by both storage adapters This provides a consistent interface regardless of storage backend

Constant Summary collapse

ATTRIBUTES =
%i[
  uuid
  batch_id
  family_hash
  entry_type
  payload
  tags
  should_display_on_index
  occurred_at
  created_at
  updated_at
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ EntryData

Returns a new instance of EntryData.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/railscope/entry_data.rb', line 25

def initialize(attributes = {})
  attributes = attributes.symbolize_keys if attributes.respond_to?(:symbolize_keys)

  ATTRIBUTES.each do |attr|
    value = attributes[attr]
    # Handle time parsing for string values
    if %i[occurred_at created_at updated_at].include?(attr) && value.is_a?(String)
      value = begin
        Time.zone.parse(value)
      rescue StandardError
        value
      end
    end
    send("#{attr}=", value)
  end

  # Set defaults
  self.tags ||= []
  self.payload ||= {}
  self.should_display_on_index = true if should_display_on_index.nil?
end

Class Method Details

.from_active_record(record) ⇒ Object

Create from ActiveRecord Entry model



90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/railscope/entry_data.rb', line 90

def from_active_record(record)
  new(
    uuid: record.uuid,
    batch_id: record.batch_id,
    family_hash: record.family_hash,
    entry_type: record.entry_type,
    payload: record.payload,
    tags: record.tags,
    should_display_on_index: record.should_display_on_index,
    occurred_at: record.occurred_at,
    created_at: record.created_at,
    updated_at: record.updated_at
  )
end

.from_json(json_string) ⇒ Object

Create from JSON hash (Redis)



106
107
108
109
# File 'lib/railscope/entry_data.rb', line 106

def from_json(json_string)
  data = JSON.parse(json_string, symbolize_names: true)
  new(data)
end

Instance Method Details

#<=>(other) ⇒ Object

Comparison for sorting



82
83
84
85
86
# File 'lib/railscope/entry_data.rb', line 82

def <=>(other)
  return nil unless other.is_a?(EntryData)

  other.occurred_at <=> occurred_at # desc by default
end

#[](key) ⇒ Object

For compatibility with ActiveRecord-style access



73
74
75
# File 'lib/railscope/entry_data.rb', line 73

def [](key)
  send(key) if respond_to?(key)
end

#[]=(key, value) ⇒ Object



77
78
79
# File 'lib/railscope/entry_data.rb', line 77

def []=(key, value)
  send("#{key}=", value) if respond_to?("#{key}=")
end

#displayable? ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/railscope/entry_data.rb', line 68

def displayable?
  should_display_on_index
end

#serializable_hash ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/railscope/entry_data.rb', line 57

def serializable_hash
  to_h.transform_values do |value|
    case value
    when Time, DateTime, ActiveSupport::TimeWithZone
      value.iso8601(6)
    else
      value
    end
  end
end

#to_h ⇒ Object



47
48
49
50
51
# File 'lib/railscope/entry_data.rb', line 47

def to_h
  ATTRIBUTES.index_with do |attr|
    send(attr)
  end
end

#to_json(*args) ⇒ Object



53
54
55
# File 'lib/railscope/entry_data.rb', line 53

def to_json(*args)
  serializable_hash.to_json(*args)
end