Class: StormForge::Dsl::TestCase::Definition

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serializers::JSON, ActiveModel::Validations, AttributeAccess
Defined in:
lib/stormforge/dsl/test_case/definition.rb

Defined Under Namespace

Classes: SessionProbabilityValidator

Constant Summary collapse

ARRIVAL_PHASE_OPTIONS =
{
  warmup:        false,

  duration:      nil,
  duration_unit: :second,

  rate:          nil,
  rate_unit:     :second
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeAccess

#method_missing, #respond_to_missing?

Constructor Details

#initialize(slug, callable = nil, &block) ⇒ Definition

Returns a new instance of Definition.



37
38
39
40
41
42
43
44
45
46
# File 'lib/stormforge/dsl/test_case/definition.rb', line 37

def initialize(slug, callable=nil, &block)
  @slug = slug

  @attributes = {
    arrival_phases: [],
    sessions: {},
    data_sources: nil
  }
  instance_eval(&(callable || block))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class StormForge::Dsl::TestCase::AttributeAccess

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/stormforge/dsl/test_case/definition.rb', line 6

def attributes
  @attributes
end

Instance Method Details

#arrival_phase(options) ⇒ Object



86
87
88
89
90
# File 'lib/stormforge/dsl/test_case/definition.rb', line 86

def arrival_phase(options)
  @attributes[:arrival_phases] << ARRIVAL_PHASE_OPTIONS.each_with_object({}) do |(option, default), phase_options|
    phase_options[option] = options.fetch(option, default)
  end
end

#as_json(*args) ⇒ Object



64
65
66
67
68
69
# File 'lib/stormforge/dsl/test_case/definition.rb', line 64

def as_json(*args)
  hash = super
  hash["dsl_version"] = self.dsl_version.to_s
  hash["slug"] = slug
  hash
end

#available_attributesObject



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/stormforge/dsl/test_case/definition.rb', line 96

def available_attributes
  [
    :title,
    :description,
    :version,
    :targets,
    :arrival_phases,
    :sessions,
    :cloud,
    :data_sources
  ]
end

#cloud(provider, callable = nil, &block) ⇒ Object



109
110
111
# File 'lib/stormforge/dsl/test_case/definition.rb', line 109

def cloud(provider, callable=nil, &block)
  @attributes[:cloud] = StormForge::Dsl::TestCase::Cloud.new(provider, &(callable || block))
end

#data_sources(callable = nil, &block) ⇒ Object



113
114
115
116
117
118
119
# File 'lib/stormforge/dsl/test_case/definition.rb', line 113

def data_sources(callable=nil, &block)
  if (callable || block)
    @attributes[:data_sources] = StormForge::Dsl::TestCase::DataSource.new(&(callable || block))
  else
    @attributes[:data_sources]
  end
end

#dsl_versionObject



125
126
127
# File 'lib/stormforge/dsl/test_case/definition.rb', line 125

def dsl_version
  :v1
end

#read_attribute_for_serialization(key) ⇒ Object

TODO this feels messy. is this really the only way to serialize complex attributes?



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/stormforge/dsl/test_case/definition.rb', line 49

def read_attribute_for_serialization(key)
  case key
  when :sessions
    @attributes[:sessions].each_with_object({}) do |(session_name, session), hash|
      hash[session_name] = session.serializable_hash
    end
  when :cloud
    @attributes[:cloud].serializable_hash
  when :data_sources
    @attributes[:data_sources].serializable_hash if @attributes[:data_sources]
  else
    super
  end
end

#session(name, probability, callable = nil, &block) ⇒ Object



92
93
94
# File 'lib/stormforge/dsl/test_case/definition.rb', line 92

def session(name, probability, callable=nil, &block)
  @attributes[:sessions][name] = StormForge::Dsl::TestCase::Session.new(name, probability, @attributes[:data_sources], &(callable || block))
end

#slugObject



121
122
123
# File 'lib/stormforge/dsl/test_case/definition.rb', line 121

def slug
  @slug.to_s.downcase.gsub(/[^a-z0-9\-_]/, "_").gsub(/_+/, "_")
end

#targets(*targets) ⇒ Object



71
72
73
74
# File 'lib/stormforge/dsl/test_case/definition.rb', line 71

def targets(*targets)
  return @attributes[:targets] if targets.length == 0
  @attributes[:targets] = targets.flatten
end