Class: Stripe::StripeContext

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/stripe/stripe_context.rb

Overview

Represents hierarchical context for Stripe API operations.

This class is immutable - all methods return new instances rather than modifying the existing instance. It provides utilities for building context hierarchies and converting to/from string representations.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(segments = nil) ⇒ StripeContext

Creates a new StripeContext with the given segments.



15
16
17
# File 'lib/stripe/stripe_context.rb', line 15

def initialize(segments = nil)
  @segments = (segments || []).map(&:to_s).freeze
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



12
13
14
# File 'lib/stripe/stripe_context.rb', line 12

def segments
  @segments
end

Class Method Details

.parse(context_str) ⇒ Object

Parses a context string into a StripeContext instance.



20
21
22
23
24
# File 'lib/stripe/stripe_context.rb', line 20

def self.parse(context_str)
  return new if context_str.nil? || context_str.empty?

  new(context_str.split("/"))
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Checks equality with another StripeContext.



50
51
52
# File 'lib/stripe/stripe_context.rb', line 50

def ==(other)
  other.is_a?(StripeContext) && @segments == other.segments
end

#empty?Boolean

Returns true if the context has no segments.

Returns:

  • (Boolean)

    true if empty, false otherwise



64
65
66
# File 'lib/stripe/stripe_context.rb', line 64

def empty?
  @segments.empty?
end

#inspectObject

Returns a human-readable representation for debugging.



58
59
60
# File 'lib/stripe/stripe_context.rb', line 58

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} segments=#{@segments.inspect}>"
end

#popObject

Creates a new StripeContext with the last segment removed. If there are no segments, returns a new empty StripeContext.

Raises:

  • (IndexError)


37
38
39
40
41
42
# File 'lib/stripe/stripe_context.rb', line 37

def pop
  raise IndexError, "No segments to pop" if @segments.empty?

  new_segments = @segments[0...-1]
  self.class.new(new_segments)
end

#push(segment) ⇒ Object

Creates a new StripeContext with an additional segment appended.

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
# File 'lib/stripe/stripe_context.rb', line 27

def push(segment)
  segment_str = segment.to_s.strip
  raise ArgumentError, "Segment cannot be empty or whitespace" if segment_str.empty?

  new_segments = @segments + [segment_str]
  self.class.new(new_segments)
end

#to_sObject

Converts this context to its string representation.



45
46
47
# File 'lib/stripe/stripe_context.rb', line 45

def to_s
  @segments.join("/")
end