Class: Rox::Core::RoxString

Inherits:
Object
  • Object
show all
Defined in:
lib/rox/core/entities/rox_string.rb

Direct Known Subclasses

Flag, RoxDouble, RoxInt, Server::RoxString

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_value, options = []) ⇒ RoxString

Returns a new instance of RoxString.

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/rox/core/entities/rox_string.rb', line 11

def initialize(default_value, options = [])
  @default_value = default_value
  @options = options.clone
  raise ArgumentError, 'options should be an array' unless options.is_a?(Array)
  if options.length > 0 && options.any? { |x| x.class != default_value.class }
    raise ArgumentError, 'options should be the same type as default value'
  end
  @options << default_value unless options.include?(default_value)

  @condition = nil
  @parser = nil
  @impression_invoker = nil
  @client_experiment = nil
  @name = nil
end

Instance Attribute Details

#client_experimentObject

Returns the value of attribute client_experiment.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def client_experiment
  @client_experiment
end

#conditionObject

Returns the value of attribute condition.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def condition
  @condition
end

#default_valueObject

Returns the value of attribute default_value.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def default_value
  @default_value
end

#impression_invokerObject

Returns the value of attribute impression_invoker.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def impression_invoker
  @impression_invoker
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def name
  @name
end

#optionsObject

Returns the value of attribute options.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def options
  @options
end

#parserObject

Returns the value of attribute parser.



8
9
10
# File 'lib/rox/core/entities/rox_string.rb', line 8

def parser
  @parser
end

Instance Method Details

#external_typeObject



67
68
69
# File 'lib/rox/core/entities/rox_string.rb', line 67

def external_type
  'String'
end

#internal_value(context, nil_instead_of_default, evaluated_type = String) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rox/core/entities/rox_string.rb', line 51

def internal_value(context, nil_instead_of_default, evaluated_type = String)
  return_value = nil_instead_of_default ? nil : @default_value
  merged_context = MergedContext.new(@parser&.global_context, context)

  unless @parser.nil? || @condition.nil? || @condition.empty?
    evaluation_result = @parser.evaluate_expression(@condition, merged_context)
    unless evaluation_result.nil?
      value = evaluated_type == String ? evaluation_result.string_value : evaluation_result.value
      is_empty = value.is_a?(String) && value.empty?
      return_value = value if !value.nil? && !is_empty
    end
  end

  return_value
end

#send_impressions(return_value, merged_context) ⇒ Object



27
28
29
30
# File 'lib/rox/core/entities/rox_string.rb', line 27

def send_impressions(return_value, merged_context)
  reporting_value = ReportingValue.new(@name, return_value, @client_experiment)
  @impression_invoker&.invoke(reporting_value, @client_experiment, merged_context)
end

#set_for_evaluation(parser, experiment, impression_invoker) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rox/core/entities/rox_string.rb', line 32

def set_for_evaluation(parser, experiment, impression_invoker)
  if experiment.nil?
    @client_experiment = nil
    @condition = ''
  else
    @client_experiment = Experiment.new(experiment)
    @condition = experiment.condition
  end

  @parser = parser
  @impression_invoker = impression_invoker
end

#value(context = nil) ⇒ Object



45
46
47
48
49
# File 'lib/rox/core/entities/rox_string.rb', line 45

def value(context = nil)
  return_value = internal_value(context, false)
  send_impressions(return_value, context)
  return_value
end