Class: LaunchDarkly::Impl::Model::Segment

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/model/segment.rb

Overview

Since:

  • 5.5.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, logger = nil) ⇒ Segment

Returns a new instance of Segment.

Parameters:

  • data (Hash)
  • logger (Logger|nil) (defaults to: nil)

Raises:

  • (ArgumentError)

Since:

  • 5.5.0



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ldclient-rb/impl/model/segment.rb', line 13

def initialize(data, logger = nil)
  raise ArgumentError, "expected hash but got #{data.class}" unless data.is_a?(Hash)
  errors = []
  @data = data
  @key = data[:key]
  @version = data[:version]
  @deleted = !!data[:deleted]
  return if @deleted
  @included = data[:included] || []
  @excluded = data[:excluded] || []
  @included_contexts = (data[:includedContexts] || []).map do |target_data|
    SegmentTarget.new(target_data)
  end
  @excluded_contexts = (data[:excludedContexts] || []).map do |target_data|
    SegmentTarget.new(target_data)
  end
  @rules = (data[:rules] || []).map do |rule_data|
    SegmentRule.new(rule_data, errors)
  end
  @unbounded = !!data[:unbounded]
  @unbounded_context_kind = data[:unboundedContextKind] || LDContext::KIND_DEFAULT
  @generation = data[:generation]
  @salt = data[:salt]
  unless logger.nil?
    errors.each do |message|
      logger.error("[LDClient] Data inconsistency in segment \"#{@key}\": #{message}")
    end
  end
end

Instance Attribute Details

#dataHash (readonly)

Returns:

  • (Hash)

Since:

  • 5.5.0



44
45
46
# File 'lib/ldclient-rb/impl/model/segment.rb', line 44

def data
  @data
end

#deletedBoolean (readonly)

Returns:

  • (Boolean)

Since:

  • 5.5.0



50
51
52
# File 'lib/ldclient-rb/impl/model/segment.rb', line 50

def deleted
  @deleted
end

#excludedArray<String> (readonly)

Returns:

  • (Array<String>)

Since:

  • 5.5.0



54
55
56
# File 'lib/ldclient-rb/impl/model/segment.rb', line 54

def excluded
  @excluded
end

#excluded_contextsArray<LaunchDarkly::Impl::Model::SegmentTarget> (readonly)

Returns:

Since:

  • 5.5.0



58
59
60
# File 'lib/ldclient-rb/impl/model/segment.rb', line 58

def excluded_contexts
  @excluded_contexts
end

#generationInteger|nil (readonly)

Returns:

  • (Integer|nil)

Since:

  • 5.5.0



66
67
68
# File 'lib/ldclient-rb/impl/model/segment.rb', line 66

def generation
  @generation
end

#includedArray<String> (readonly)

Returns:

  • (Array<String>)

Since:

  • 5.5.0



52
53
54
# File 'lib/ldclient-rb/impl/model/segment.rb', line 52

def included
  @included
end

#included_contextsArray<LaunchDarkly::Impl::Model::SegmentTarget> (readonly)

Returns:

Since:

  • 5.5.0



56
57
58
# File 'lib/ldclient-rb/impl/model/segment.rb', line 56

def included_contexts
  @included_contexts
end

#keyString (readonly)

Returns:

  • (String)

Since:

  • 5.5.0



46
47
48
# File 'lib/ldclient-rb/impl/model/segment.rb', line 46

def key
  @key
end

#rulesArray<SegmentRule> (readonly)

Returns:

Since:

  • 5.5.0



60
61
62
# File 'lib/ldclient-rb/impl/model/segment.rb', line 60

def rules
  @rules
end

#saltString (readonly)

Returns:

  • (String)

Since:

  • 5.5.0



68
69
70
# File 'lib/ldclient-rb/impl/model/segment.rb', line 68

def salt
  @salt
end

#unboundedBoolean (readonly)

Returns:

  • (Boolean)

Since:

  • 5.5.0



62
63
64
# File 'lib/ldclient-rb/impl/model/segment.rb', line 62

def unbounded
  @unbounded
end

#unbounded_context_kindString (readonly)

Returns:

  • (String)

Since:

  • 5.5.0



64
65
66
# File 'lib/ldclient-rb/impl/model/segment.rb', line 64

def unbounded_context_kind
  @unbounded_context_kind
end

#versionInteger (readonly)

Returns:

  • (Integer)

Since:

  • 5.5.0



48
49
50
# File 'lib/ldclient-rb/impl/model/segment.rb', line 48

def version
  @version
end

Instance Method Details

#==(other) ⇒ Object

Since:

  • 5.5.0



77
78
79
# File 'lib/ldclient-rb/impl/model/segment.rb', line 77

def ==(other)
  other.is_a?(Segment) && other.data == self.data
end

#[](key) ⇒ Object

This method allows us to read properties of the object as if it's just a hash. Currently this is necessary because some data store logic is still written to expect hashes; we can remove it once we migrate entirely to using attributes of the class.

Since:

  • 5.5.0



73
74
75
# File 'lib/ldclient-rb/impl/model/segment.rb', line 73

def [](key)
  @data[key]
end

#as_jsonObject

parameter is unused, but may be passed if we're using the json gem

Since:

  • 5.5.0



81
82
83
# File 'lib/ldclient-rb/impl/model/segment.rb', line 81

def as_json(*) # parameter is unused, but may be passed if we're using the json gem
  @data
end

#to_json(*a) ⇒ Object

Same as as_json, but converts the JSON structure into a string.

Since:

  • 5.5.0



86
87
88
# File 'lib/ldclient-rb/impl/model/segment.rb', line 86

def to_json(*a)
  as_json.to_json(*a)
end