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.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



67
68
69
70
71
# File 'lib/condition/param_item.rb', line 67

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
# 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 /^#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)
  else
    item
  end
end

#check_line(real) ⇒ Object



168
169
170
171
172
173
# File 'lib/condition/param_item.rb', line 168

def check_line(real)
  @values.each do |value|
    return if check_value(real, value)
  end
  raise "#{@name} not found " + real.to_s
end

#check_value(real, value) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/condition/param_item.rb', line 147

def check_value(real, value)
  targetFlag = true
  matchFlag = true
  @unmatch_info = []
  value.each_pair do |k, v|
    match = value_match?(v, real[k])
    @unmatch_info << 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 + "\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



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

def name
  @name
end

#optionsObject



89
90
91
# File 'lib/condition/param_item.rb', line 89

def options
  @options
end

#paramsObject



85
86
87
# File 'lib/condition/param_item.rb', line 85

def params
  @params
end

#valueObject



77
78
79
# File 'lib/condition/param_item.rb', line 77

def value
  @values[0]
end

#value_match?(expected, real) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
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
# File 'lib/condition/param_item.rb', line 93

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 << v.to_s + " <> " + real[k].to_s
          result = false
        end
      end
      result
    end
  elsif Array === real
    if !(Array === expected)
      false
    elsif real.size() == 0 && expected.size() == 0
      true
    elsif real == expected
      true
    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
  else
    real.to_s == expected
  end
end

#valuesObject



81
82
83
# File 'lib/condition/param_item.rb', line 81

def values
  @values
end