Class: Tes::Request::Expression

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tes/request/expression.rb

Constant Summary collapse

REG_EXP_CHAIN =
/^(!)?([0-9A-Za-z_.]+)(\?|=|>=|<=|<|>)?(-?[\w]+(\.[\d]+)?)?/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(exp_str) ⇒ Expression

Returns a new instance of Expression.

Parameters:

  • exp_str (String)

    表达式字符串

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tes/request/expression.rb', line 9

def initialize(exp_str)
  mt = exp_str.match(REG_EXP_CHAIN)
  raise(ArgumentError, "非法的表达式片段:\t#{exp_str}") unless mt

  @data = {}
  @data[:revert] = true if mt[1]
  @data[:left_exp] = mt[2]
  if mt[3]
    @data[:op] = mt[3]
    if mt[4]
      @data[:expect_val] = mt[4]
      if @data[:expect_val] =~ /^-?\d+(\.[\d]+)?/
        @data[:expect_val] = mt[5] ? @data[:expect_val].to_f : @data[:expect_val].to_i
      end
    end
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



27
28
29
# File 'lib/tes/request/expression.rb', line 27

def data
  @data
end

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/tes/request/expression.rb', line 56

def <=>(other)
  return 0 if @data == other.data
  return nil if @data[:revert] != other.data[:revert] or @data[:left_exp] != other.data[:left_exp]

  if @data[:op] == other.data[:op]
    case @data[:op]
      when '=', '=='
        (@data[:expect_val] == other.data[:expect_val]) ? 0 : nil
      when '>', '>='
        @data[:expect_val] <=> other.data[:expect_val]
      when '<', '<='
        other.data[:expect_val] <=> @data[:expect_val]
      when '?', nil
        0
      else
        raise("内部错误:出现了不支持的表达式操作符号:#{@data[:op]}")
    end
  else
    case [@data[:op], other.data[:op]]
      when %w(< <=)
        ret = other.data[:expect_val] <=> @data[:expect_val]
        ret == 0 ? 1 : ret
      when %w(< =)
        @data[:expect_val] > other.data[:expect_val] ? -1 : nil
      when %w(= >)
        @data[:expect_val] > other.data[:expect_val] ? 1 : nil
      when %w(= >=)
        @data[:expect_val] >= other.data[:expect_val] ? 1 : nil
      when %w(= <)
        @data[:expect_val] < other.data[:expect_val] ? 1 : nil
      when %w(= <=)
        @data[:expect_val] <= other.data[:expect_val] ? 1 : nil
      when %w(> >=)
        ret = @data[:expect_val] <=> other.data[:expect_val]
        ret == 0 ? 1 : ret
      when %w(> =)
        @data[:expect_val] < other.data[:expect_val] ? -1 : nil
      when %w(>= =)
        @data[:expect_val] <= other.data[:expect_val] ? -1 : nil
      when %w(>= >)
        ret = @data[:expect_val] <=> other.data[:expect_val]
        ret == 0 ? -1 : ret
      when %w(<= =)
        @data[:expect_val] <= other.data[:expect_val] ? 1 : nil
      when %w(<= <)
        ret = other.data[:expect_val] <=> @data[:expect_val]
        ret == 0 ? -1 : ret
      else
        nil
    end
  end
end

#match?(data) ⇒ true, false

计算数据是否满足表达式(深度解析后)

Parameters:

  • data (Object)

Returns:

  • (true, false)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tes/request/expression.rb', line 32

def match?(data)
  op = @data[:op]
  unless data.respond_to?(:get_by_chain)
    raise(ArgumentError, 'data arg should be respond to :get_by_chain')
  else
    ret = if !op
            data.get_by_chain @data[:left_exp]
          elsif op == '?'
            chain_data = data.get_by_chain(@data[:left_exp])
            case chain_data
              when 'off', 'down', 'disable', '0', '', 0
                false
              else
                chain_data
            end
          else
            op = '==' if op == '='
            expect_val = @data[:expect_val]
            data.get_by_chain(@data[:left_exp]).send(op, expect_val)
          end
    @data[:revert] ? !ret : ret
  end
end

#to_sObject



109
110
111
112
113
# File 'lib/tes/request/expression.rb', line 109

def to_s
  ret = [:left_exp, :op, :expect_val].map { |k| @data[k] }.join
  ret = '!' + ret if @data[:revert]
  ret
end