Class: OmfEc::ExperimentProperty

Inherits:
Object
  • Object
show all
Defined in:
omf_ec/lib/omf_ec/experiment_property.rb

Overview

This class defines an Experiment Property, and also holds all of the Experiment Properties defined for a given experiment. Most of this implementation is re-used from OMF 5.4

Constant Summary

@@properties =

Contains all the experiment properties

Hashie::Mash.new
@@creation_observers =

Holds all observers on any Experiment Property creation

[]

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ExperimentProperty) initialize(name, value = nil, description = nil)

Create a new Experiment Property

  • name = name of the property to create/return

  • value = value to assign to this property

  • description = short string description for this property



136
137
138
139
140
141
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 136

def initialize(name, value = nil, description = nil)
  @name = name.to_s
  @description = description
  @change_observers = Array.new
  set(value)
end

Instance Attribute Details

- (Object) description

Returns the value of attribute description



125
126
127
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 125

def description
  @description
end

- (Object) id (readonly)

Returns the value of attribute id



124
125
126
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 124

def id
  @id
end

- (Object) name (readonly)

Returns the value of attribute name



124
125
126
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 124

def name
  @name
end

- (Object) value (readonly)

Returns the value of attribute value



124
125
126
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 124

def value
  @value
end

Class Method Details

+ (Object) [](name)

Returns a given property

  • name =nameof the property to return

Return

a property



29
30
31
32
33
34
35
36
37
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 29

def self.[](name)
  p = @@properties[name.to_s.to_sym]
  if p.nil?
    raise OEDLCommandException.new(name,
      "Unknown experiment property '#{name}'\n\tKnown properties are "+
      "'#{ExperimentProperty.names.join(', ')}'")
  end
  return p
end

+ (Object) []=(name, val)



39
40
41
42
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 39

def self.[]=(name, val)
  p = ExperimentProperty[name.to_sym]
  p.set(val)
end

+ (Object) add_observer(&proc)

Add an observer for any creation of a new Experiment Property

  • proc = block to execute when a new Experiment Property is created



122
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 122

def self.add_observer(&proc) @@creation_observers << proc end

+ (Object) create(name, value = nil, description = nil)

Return an existing Experiment Property, or create a new one

  • name = name of the property to create/return

  • value = value to assign to this property

  • description = short string description for this property

Return

an Experiment Property



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 90

def self.create(name, value = nil, description = nil)
  name = name.to_s
  # http://stackoverflow.com/questions/4378670/what-is-a-ruby-regex-to-match-a-function-name
  if /[@$"]/ =~ name.to_sym.inspect
    raise OEDLCommandException.new("ExperimentProperty.create",
      "Cannot create property '#{name}', its name is not a valid Ruby name")
  end
  p = nil
  name = name.to_sym
  if (p = @@properties[name]) != nil
    p.set(value) if value != nil
    p.description = description if description != nil
  else
    p = ExperimentProperty.new(name, value, description)
    @@properties[name] = p
    # Let the observers know that we created a new property
    @@creation_observers.each { |proc| proc.call(:create, p) }
  end
  return p
end

+ (Object) each(sort_names = false, &block)

Iterate over all Experiment Properties. The block will be called with the respective property as single argument

  • sort_names = if 'true' sort the properties (default: true)

  • &block = the block of commands to call



76
77
78
79
80
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 76

def self.each(sort_names = false, &block)
  names = @@properties.keys
  names = names.sort_by {|sym| sym.to_s} if (sort_names)
  names.each { |n| block.call(@@properties[n]) }
end

+ (Object) length



44
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 44

def self.length; @@properties.length end

+ (Object) method_missing(name, args = nil)

Handles missing method, allows to access an existing Experiment Property with the syntax 'propcontext.propname'



56
57
58
59
60
61
62
63
64
65
66
67
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 56

def self.method_missing(name, args = nil)
  name = name.to_s
  if setter = (name[-1] == ?=)
    name.chop!
  end
  p = ExperimentProperty[name.to_sym]
  if setter
    p.set(args)
  else
    return p
  end
end

+ (Object) names

Return the names of the all defined Experiment Properties

Return

an Array with the names of all defined Experiment Properties



116
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 116

def self.names() return @@properties.keys end

+ (Object) to_str

Minitest needs to be able to turn this Class into a string, this is normally done through the default 'method_missing' of the Classe but we redefined that… so to run minitest we need to explicitly define 'to_str' for this Class



50
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 50

def self.to_str; "ExperimentProperty" end

Instance Method Details

- (Object) *(right)

Multiplication operator for Integer and Float properties



188
189
190
191
192
193
194
195
196
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 188

def *(right)
  if @value.kind_of?(Integer) || @value.kind_of?(Float)
    return (@value * right)
  else
    raise OEDLCommandException.new("*", "Illegal operation, "+
      "the value of Experiment Property '#{@name}' is not numerical "+
      "(current value is of type #{value.class})")
  end
end

- (Object) +(right)

Addition operator for Integer, Float, and String properties



210
211
212
213
214
215
216
217
218
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 210

def +(right)
  if @value.kind_of?(Integer) || @value.kind_of?(Float) || @value.kind_of?(String)
    return (@value + right)
  else
    raise OEDLCommandException.new("+", "Illegal operation, "+
      "The value of Experiment Property '#{@name}' does not support addition "+
      "(current value is of type #{value.class})")
  end
end

- (Object) -(right)

Substraction operator for Integer and Float properties



199
200
201
202
203
204
205
206
207
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 199

def -(right)
  if @value.kind_of?(Integer) || @value.kind_of?(Float)
    return (@value - right)
  else
    raise OEDLCommandException.new("-", "Illegal operation, "+
      "the value of Experiment Property '#{@name}' is not numerical "+
      "(current value is of type #{value.class})")
  end
end

- (Object) /(right)

Division operator for Integer and Float properties



177
178
179
180
181
182
183
184
185
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 177

def /(right)
  if @value.kind_of?(Integer) || @value.kind_of?(Float)
    return (@value / right)
  else
    raise OEDLCommandException.new("/", "Illegal operation, "+
      "the value of Experiment Property '#{@name}' is not numerical "+
      "(current value is of type #{value.class})")
  end
end

- (Object) coerce(other)

Explicit Coercion for Integer, Float, and String properties (allow property to be on the right-hand of an operator such as +)



222
223
224
225
226
227
228
229
230
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 222

def coerce(other)
  if @value.kind_of?(Integer) || @value.kind_of?(Float) || @value.kind_of?(String)
    return other, @value
  else
    raise OEDLCommandException.new("coercion", "Illegal operation, "+
      "The value of Experiment Property '#{@name}' cannot be coerced to allow "+
      " the requested operation (current value is of type #{value.class})")
  end
end

- (Object) on_change(&block)

Add a block of command to the list of actions to do when this property is being changed

  • &block = the block of command to add



149
150
151
152
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 149

def on_change (&block)
  debug "Somebody bound to me"
  @change_observers << block
end

- (Object) set(value)

Update the value of this Experiment Property

  • value = new value for this property



159
160
161
162
163
164
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 159

def set(value)
  @value = value
  info "#{name} = #{value.inspect} (#{value.class})"
  @change_observers.each { |proc| proc.call(value) }
  OmfEc.experiment.(name, value, :prop)
end

- (Object) to_f

More convenient conversion



170
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 170

def to_f() @value.to_f end

- (Object) to_i



171
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 171

def to_i() @value.to_i end

- (Object) to_str Also known as: to_s

Implicit conversion to String (required for + operator)



167
# File 'omf_ec/lib/omf_ec/experiment_property.rb', line 167

def to_str() @value.to_s end