Class: Stripe::StripeContext
- Inherits:
-
Object
- Object
- Stripe::StripeContext
- 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
-
#segments ⇒ Object
readonly
Returns the value of attribute segments.
Class Method Summary collapse
-
.parse(context_str) ⇒ Object
Parses a context string into a StripeContext instance.
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Checks equality with another StripeContext.
-
#empty? ⇒ Boolean
Returns true if the context has no segments.
-
#initialize(segments = nil) ⇒ StripeContext
constructor
Creates a new StripeContext with the given segments.
-
#inspect ⇒ Object
Returns a human-readable representation for debugging.
-
#pop ⇒ Object
Creates a new StripeContext with the last segment removed.
-
#push(segment) ⇒ Object
Creates a new StripeContext with an additional segment appended.
-
#to_s ⇒ Object
Converts this context to its string representation.
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
#segments ⇒ Object (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.
64 65 66 |
# File 'lib/stripe/stripe_context.rb', line 64 def empty? @segments.empty? end |
#inspect ⇒ Object
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 |
#pop ⇒ Object
Creates a new StripeContext with the last segment removed. If there are no segments, returns a new empty StripeContext.
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.
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_s ⇒ Object
Converts this context to its string representation.
45 46 47 |
# File 'lib/stripe/stripe_context.rb', line 45 def to_s @segments.join("/") end |