Class: Tes::Request::Ask
- Inherits:
-
Object
- Object
- Tes::Request::Ask
- Includes:
- Comparable
- Defined in:
- lib/tes/request/ask.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#greedy ⇒ Object
Returns the value of attribute greedy.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
Instance Method Summary collapse
-
#+(other) ⇒ Ask
合并叠加对资源的请求.
-
#-(other) ⇒ Ask
相对other要求多的和严格的.
- #<=>(other) ⇒ Object
-
#initialize(ask_str) ⇒ Ask
constructor
A new instance of Ask.
-
#match?(data) ⇒ Boolean
计算数据是否满足要求.
- #merge_able?(other) ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(ask_str) ⇒ Ask
Returns a new instance of Ask.
10 11 12 13 14 15 16 17 |
# File 'lib/tes/request/ask.rb', line 10 def initialize(ask_str) snippets = ask_str.strip.sub(/^\*\d+:/, '').sub(/^&\d+\./, '').split(/\s*,\s*/) expressions = snippets.map { |s| Expression.new(s) } @data = expressions.inject({}) { |t, e| t.merge(e.data[:left_exp] => e) } # @reference 是表达式列表 @reference = [] @greedy = false end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
19 20 21 |
# File 'lib/tes/request/ask.rb', line 19 def data @data end |
#greedy ⇒ Object
Returns the value of attribute greedy.
20 21 22 |
# File 'lib/tes/request/ask.rb', line 20 def greedy @greedy end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
19 20 21 |
# File 'lib/tes/request/ask.rb', line 19 def reference @reference end |
Instance Method Details
#+(other) ⇒ Ask
合并叠加对资源的请求
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/tes/request/ask.rb', line 85 def +(other) if self == other self elsif self >=other self elsif self <other other else raise('不能合并叠加') unless merge_able?(other) self_gt_other = self.-(other) other_gt_self = other.-(self) ask1 = self.new('') ask2 = self.new('') ask1.data.merge!(self.data) ask1.data.merge!(other_gt_self) ask2.data.merge!(other.data) ask2.data.merge!(self_gt_other) ask1 >= ask2 ? ask1 : ask2 end end |
#-(other) ⇒ Ask
相对other要求多的和严格的
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/tes/request/ask.rb', line 72 def -(other) diff_data = @data.reject do |n, e| o_e = other.data[n] o_e && e <= o_e end diff_ask = self.new('') diff_ask.data.merge!(diff_data) diff_ask end |
#<=>(other) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 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 |
# File 'lib/tes/request/ask.rb', line 28 def <=>(other) exp_compare_lab = -> (a, b) do if a.nil? and b.nil? 0 elsif a.nil? -1 elsif b.nil? 1 else a <=> b end end ret = if @data == other.data 0 elsif self.data > other.data 1 elsif self.data < other.data -1 else a_to_b_results = @data.map { |n, e| exp_compare_lab.call(e, other.data[n]) } if a_to_b_results.all? { |ret| ret and ret <= 0 } -1 elsif a_to_b_results.all? { |ret| ret and ret >= 0 } # 这时必须要求other的表达式全部能被self覆盖. (other.data.keys - @data.keys).size > 0 ? nil : 1 else nil end end case [greedy, other.greedy] when [true, false] return ret unless ret ret >=0 ? 1 : nil when [false, true] return ret unless ret ret <=0 ? -1 : nil else ret end end |
#match?(data) ⇒ Boolean
计算数据是否满足要求
24 25 26 |
# File 'lib/tes/request/ask.rb', line 24 def match?(data) @data.all? { |_n, exp| exp.match?(data) } end |
#merge_able?(other) ⇒ Boolean
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/tes/request/ask.rb', line 108 def merge_able?(other) if self.<=> other true else self_gt_other = self.-(other) other_gt_self = other.-(self) conflict_exp_keys = self_gt_other.data.keys & other_gt_self.data.keys conflict_exp_keys.empty? end rescue ArgumentError false end |
#to_s ⇒ Object
121 122 123 |
# File 'lib/tes/request/ask.rb', line 121 def to_s @data.values.map(&:to_s).join(',') end |