Class: NoSE::WorkloadDSL

Inherits:
Object show all
Defined in:
lib/nose/workload.rb

Overview

A helper class for DSL creation to avoid messing with Workload

Instance Method Summary collapse

Constructor Details

#initialize(arg) ⇒ WorkloadDSL

Returns a new instance of WorkloadDSL.



144
145
146
147
148
149
150
151
# File 'lib/nose/workload.rb', line 144

def initialize(arg)
  if arg.is_a? Workload
    @workload = arg
    @model = arg.model
  elsif arg.is_a? Model
    @model = arg
  end
end

Instance Method Details

#DefaultMix(mix) ⇒ void

This method returns an undefined value.

Allow setting the default workload mix



206
207
208
# File 'lib/nose/workload.rb', line 206

def DefaultMix(mix)
  @workload.mix = mix
end

#Entity(*args, &block) ⇒ Entity

Shortcut to add a new #Entity to the workload

Returns:



162
163
164
# File 'lib/nose/workload.rb', line 162

def Entity(*args, &block)
  @model.add_entity Entity.new(*args, &block)
end

#Group(name, weight = 1.0, **mixes, &block) ⇒ void

This method returns an undefined value.

Allow grouping statements with an associated weight



212
213
214
215
216
217
218
219
220
221
# File 'lib/nose/workload.rb', line 212

def Group(name, weight = 1.0, **mixes, &block)
  fail 'Groups require a workload' if @workload.nil?

  # Apply the DSL
  dsl = GroupDSL.new
  dsl.instance_eval(&block) if block_given?
  dsl.statements.each do |statement|
    Q(statement, weight, **mixes, group: name)
  end
end

#HasMany(from_name, to_name, entities, **options) ⇒ void

This method returns an undefined value.

Add a HasMany relationship which is just the opposite of HasOne



168
169
170
# File 'lib/nose/workload.rb', line 168

def HasMany(from_name, to_name, entities, **options)
  HasOne to_name, from_name, Hash[[entities.first.reverse]], **options
end

#HasOne(from_name, to_name, entities, **options) ⇒ void

This method returns an undefined value.

Separate function for foreign keys to avoid circular dependencies



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/nose/workload.rb', line 174

def HasOne(from_name, to_name, entities, **options)
  from_entity, to_entity = entities.first
  from_field = Fields::ForeignKeyField.new from_name,
                                           @model[to_entity],
                                           **options

  # Add the key in the opposite direction
  options[:count] = @model[from_entity].count
  options[:relationship] = :many
  to_field = Fields::ForeignKeyField.new to_name,
                                         @model[from_entity],
                                         **options

  # Set the opposite keys and add to entities
  to_field.reverse = from_field
  from_field.reverse = to_field
  @model[from_entity] << from_field
  @model[to_entity] << to_field
end

#Model(name) ⇒ Object

Allow the use of an external model



156
157
158
# File 'lib/nose/workload.rb', line 156

def Model(name)
  @workload.instance_variable_set(:@model, NoSE::Model.load(name))
end

#Q(statement, weight = 1.0, group: nil, label: nil, **mixes) ⇒ void

This method returns an undefined value.

Shortcut to add a new Statement to the workload



196
197
198
199
200
201
202
# File 'lib/nose/workload.rb', line 196

def Q(statement, weight = 1.0, group: nil, label: nil, **mixes)
  fail 'Statements require a workload' if @workload.nil?

  return if weight.zero? && mixes.empty?
  mixes = { default: weight } if mixes.empty?
  @workload.add_statement statement, mixes, group: group, label: label
end