Class: BloodContracts::Instrumentation::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/blood_contracts/instrumentation/session.rb

Overview

Basic class to hold data about matching process Start date, finish date, result type name and the validation context

Constant Summary collapse

NO_SCOPE =

Session scope fallback

"unscoped"
NO_VALIDATION_PATH =

Session validation path fallback

"undefined"
NO_TYPE_MATCH =

Session result type name fallback

"unmatched"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_name) ⇒ Nothing

Initialize the session with matcher type name with defaults

Parameters:

  • type_name (String)

    name of the type which owns the session



76
77
78
79
80
81
# File 'lib/blood_contracts/instrumentation/session.rb', line 76

def initialize(type_name)
  @id = SecureRandom.hex(10)
  @matcher_type_name = type_name
  @extras = {}
  @context = {}
end

Instance Attribute Details

#contextHash (readonly)

Frozen hash of matching pipeline context

Returns:

  • (Hash)


30
31
32
# File 'lib/blood_contracts/instrumentation/session.rb', line 30

def context
  @context
end

#extrasHash (readonly)

Additional data for instrumentaion stores here

Returns:

  • (Hash)


60
61
62
# File 'lib/blood_contracts/instrumentation/session.rb', line 60

def extras
  @extras
end

#finished_atTime (readonly)

Time when session finished

Returns:

  • (Time)


24
25
26
# File 'lib/blood_contracts/instrumentation/session.rb', line 24

def finished_at
  @finished_at
end

#idString (readonly)

Unique ID of the session

Returns:

  • (String)


12
13
14
# File 'lib/blood_contracts/instrumentation/session.rb', line 12

def id
  @id
end

#matcher_type_nameString (readonly)

Name of the type which owns the session

Returns:

  • (String)


42
43
44
# File 'lib/blood_contracts/instrumentation/session.rb', line 42

def matcher_type_name
  @matcher_type_name
end

#pathArray<String> (readonly)

List of matches in the pipeline run

Returns:

  • (Array<String>)


54
55
56
# File 'lib/blood_contracts/instrumentation/session.rb', line 54

def path
  @path
end

#result_type_nameString (readonly)

Name of the matching result type

Returns:

  • (String)


48
49
50
# File 'lib/blood_contracts/instrumentation/session.rb', line 48

def result_type_name
  @result_type_name
end

#scopeString (readonly)

Additional text about scope of the mathc (e.g. “User:12”)

Returns:

  • (String)


36
37
38
# File 'lib/blood_contracts/instrumentation/session.rb', line 36

def scope
  @scope
end

#started_atTime (readonly)

Time when session started

Returns:

  • (Time)


18
19
20
# File 'lib/blood_contracts/instrumentation/session.rb', line 18

def started_at
  @started_at
end

Instance Method Details

#finish(type_match) ⇒ Nothing

Marks the session as finished (with the type) If you inherit the Session this method runs right AFTER matching finished (even if an exception raised the method would be called)

Parameters:

  • type_match (BC::Refined)

    result type of matching pipeline

Returns:

  • (Nothing)


109
110
111
112
113
114
115
116
117
118
# File 'lib/blood_contracts/instrumentation/session.rb', line 109

def finish(type_match)
  @finished_at = Time.now
  @context = type_match.context.dup.freeze if type_match
  @valid = type_match&.valid?

  @result_type_name = type_match&.class&.name || NO_TYPE_MATCH
  @id =    @context.fetch(:session_id) { @id }
  @scope = @context.fetch(:scope) { NO_SCOPE }
  @path =  @context.fetch(:steps) { NO_VALIDATION_PATH }
end

#startNothing

Marks the session as started If you inherit the Session this method runs right BEFORE matching start

Returns:

  • (Nothing)


88
89
90
# File 'lib/blood_contracts/instrumentation/session.rb', line 88

def start
  @started_at = Time.now
end

#valid?Boolean

Whether the result was valid or not

Returns:

  • (Boolean)


66
67
68
# File 'lib/blood_contracts/instrumentation/session.rb', line 66

def valid?
  !!@valid
end