Class: Condition::ParamItem

Inherits:
Object
  • Object
show all
Defined in:
lib/condition/param_item.rb

Instance Method Summary collapse

Constructor Details

#initialize(rows) ⇒ ParamItem

Returns a new instance of ParamItem.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/condition/param_item.rb', line 7

def initialize(rows)
  @name = rows[0][0].to_sym
  @options = rows[0].size > 1 ? rows[0][1..rows[0].size - 1] : []
  body = rows[1..rows.size - 1]
  @values = []
  @keys = []
  @params = {}
  @refs = []
  @used_values = []
  body.each do |row|
    index = 0
    key = row[0].to_sym
    @keys = key
    @params[key] = ("$" + key.to_s).to_sym
    items = row[1..row.size - 1]
    value = nil
    items.each do |item|
      value = @values[index] ? @values[index] : {}
      value[key] = calc_item(item, index, key)
      @values[index] = value
      index += 1
    end
  end
end

Instance Method Details

#apply_ref(param) ⇒ Object



73
74
75
76
77
# File 'lib/condition/param_item.rb', line 73

def apply_ref(param)
  @refs.each do |it|
    @values[it[:index]][it[:key]] = param.get(it[:name], it[:count])
  end
end

#calc_item(item, index, key) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/condition/param_item.rb', line 40

def calc_item(item, index, key)
  if '#NULL' == item
    nil
  elsif '#TRUE' == item
    true
  elsif '#FALSE' == item
    false
  elsif '#EMPTY' == item
    ''
  elsif '#NOW' == item
    Time.now
  elsif /^#NOW\((.+)\)$/ =~ item
    Time.now + ($1.to_i)
  elsif /^#REF\((.+)\)$/ =~ item
    ary = $1.split(/,/)
    count = ary.size > 1 ? ary[1].strip.to_i : nil
    @refs << {index: index, key: key, name: ary[0].strip.to_sym, count: count}
    item
  elsif /^#JSON\((.+)\)$/ =~ item
    JSON.parse($1, {:symbolize_names => true})
  elsif /^#TIME\((.+)\)$/ =~ item
    Time.parse($1)
  elsif /^#INT\((.+)\)$/ =~ item
    $1.to_i
  elsif /^#REGEXP\((.+)\)$/ =~ item
    Regexp.new($1)
  elsif /^#EVAL\((.+)\)$/ =~ item
    eval($1)
  else
    item
  end
end

#check_line(real, index) ⇒ Object



181
182
183
184
185
186
187
188
189
# File 'lib/condition/param_item.rb', line 181

def check_line(real, index)
  value_index = 0
  @values.each do |value|
    targetFlag = @options.empty? ? value_index == index : true
    return if check_value(real, value, targetFlag)
    value_index += 1
  end
  raise "#{@name} not found " + real.to_s
end

#check_value(real, value, targetFlag) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/condition/param_item.rb', line 161

def check_value(real, value, targetFlag)
  matchFlag = true
  @unmatch_info = []
  value.each_pair do |k, v|
    match = value_match?(v, real[k])
    @unmatch_info << "key=#{k.to_s} " + v.to_s + " <> " + real[k].to_s if !match
    whereKeyFlag = nil != @options.index(k.to_s)
    matchFlag = false if !match
    targetFlag = false if whereKeyFlag && !match
  end
  if targetFlag && matchFlag
    @used_values << value
    return true
  elsif !targetFlag
    return false
  else
    raise "#{@name} not match " + real.to_s + "\nexpected <> real\n" + @unmatch_info.join("\n")
  end
end

#clear_used_valuesObject



32
33
34
# File 'lib/condition/param_item.rb', line 32

def clear_used_values
  @used_values = []
end

#is_remain_valueObject



36
37
38
# File 'lib/condition/param_item.rb', line 36

def is_remain_value
  @values.size > @used_values.size
end

#nameObject



79
80
81
# File 'lib/condition/param_item.rb', line 79

def name
  @name
end

#optionsObject



95
96
97
# File 'lib/condition/param_item.rb', line 95

def options
  @options
end

#paramsObject



91
92
93
# File 'lib/condition/param_item.rb', line 91

def params
  @params
end

#valueObject



83
84
85
# File 'lib/condition/param_item.rb', line 83

def value
  @values[0]
end

#value_match?(expected, real) ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/condition/param_item.rb', line 99

def value_match?(expected, real)
  if "#PRESENT" == expected
    (!real.nil?) && ("" != real.to_s)
  elsif expected == nil && real != nil || expected != nil && real == nil
    false
  elsif Regexp === expected
    expected =~ real.to_s
  elsif Time === real
    real == Time.parse(expected)
  elsif nil == real
    expected == nil
  elsif Hash === real
    if !(Hash === expected)
      false
    elsif real == expected
      true
    else
      result = true
      expected.each_pair do |k, v|
        res = value_match?(v, real[k])
        if !res
          @unmatch_info << "key=#{k.to_s} #{v.to_s} <> #{real[k].to_s}"
          result = false
        end
      end
      result
    end
  elsif Array === real
    if !(Array === expected)
      @unmatch_info << "real is Array and expected is not Array"
      false
    elsif real.size() == 0 && expected.size() == 0
      true
    elsif real == expected
      true
    elsif real.size() != expected.size()
      @unmatch_info << "real array size=#{real.size().to_s} <> expected size=#{expected.size().to_s}"
      false
    else
      index = 0
      result = true
      while true
        break if index >= expected.size
        res = value_match?(expected[index], real[index])
        if !res
          @unmatch_info << expected[index].to_s + " <> " + real[index].to_s
          result = false
          break
        end
        index += 1
      end
      result
    end
  elsif Integer === expected
    real.to_i == expected
  elsif Array === expected && real.respond_to?("to_a")
    real.to_a == expected
  else
    real.to_s == expected.to_s
  end
end

#valuesObject



87
88
89
# File 'lib/condition/param_item.rb', line 87

def values
  @values
end