Class: TTK::Weights::WExpr

Inherits:
Weight show all
Defined in:
lib/ttk/weights/WExpr.rb

Constant Summary

Constants inherited from Weight

TTK::Weights::Weight::BASE

Instance Attribute Summary collapse

Attributes inherited from Weight

#max, #min

Instance Method Summary collapse

Methods inherited from Weight

#fail?, #long_get, #pass?, #start?

Constructor Details

#initialize(aFloat, min = 0, max = 1, expr = nil) ⇒ WExpr

Returns a new instance of WExpr.



15
16
17
18
# File 'lib/ttk/weights/WExpr.rb', line 15

def initialize ( aFloat, min=0, max=1, expr=nil )
  super(aFloat, min, max)
  @expr = expr || "#{to_f}[#@min, #@max]"
end

Instance Attribute Details

#exprObject

Returns the value of attribute expr.



13
14
15
# File 'lib/ttk/weights/WExpr.rb', line 13

def expr
  @expr
end

Instance Method Details

#*(rhs) ⇒ Object



26
27
28
29
30
# File 'lib/ttk/weights/WExpr.rb', line 26

def * ( rhs )
  x = super
  x.expr = "(#{to_s} * #{rhs.to_s})[#{x.min}, #{x.max}]"
  x
end

#+(rhs) ⇒ Object



20
21
22
23
24
# File 'lib/ttk/weights/WExpr.rb', line 20

def + ( rhs )
  x = super
  x.expr = "(#{@expr} + #{rhs.to_s})[#{x.min}, #{x.max}]"
  x
end

#getObject



48
49
50
# File 'lib/ttk/weights/WExpr.rb', line 48

def get
  @expr + " == #{to_f.to_s} == #{(to_f * 100).floor}%"
end

#normalize(weight) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ttk/weights/WExpr.rb', line 33

def normalize ( weight )
  diff = @max - @min
  if diff.zero?
    if @max.zero? and weight.zero?
      self.class.new(:PASS)
    else
      self.class.new(:FAIL)
    end
  else
    self.class.new((self - @min) / diff, 0, 1,
                "((#{to_s} - #{min.to_s}) / (#{max.to_s} - #{min.to_s}))")
  end
end

#to_sObject



52
53
54
# File 'lib/ttk/weights/WExpr.rb', line 52

def to_s
  @expr
end

#to_yaml(opts = {}) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/ttk/weights/WExpr.rb', line 56

def to_yaml ( opts={} )
  if opts[:ttk]
    to_f.to_yaml(opts)
  else
    super
  end
end