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.
-
#lock_type ⇒ Object
Returns the value of attribute lock_type.
-
#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 18 |
# 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 @lock_type = :lock end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
20 21 22 |
# File 'lib/tes/request/ask.rb', line 20 def data @data end |
#greedy ⇒ Object
Returns the value of attribute greedy.
21 22 23 |
# File 'lib/tes/request/ask.rb', line 21 def greedy @greedy end |
#lock_type ⇒ Object
Returns the value of attribute lock_type.
21 22 23 |
# File 'lib/tes/request/ask.rb', line 21 def lock_type @lock_type end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
20 21 22 |
# File 'lib/tes/request/ask.rb', line 20 def reference @reference end |
Instance Method Details
#+(other) ⇒ Ask
合并叠加对资源的请求
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/tes/request/ask.rb', line 97 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.class.new('') ask2 = self.class.new('') ask1.data.merge!(self.data) ask1.data.merge!(other_gt_self) ask2.data.merge!(other.data) ask2.data.merge!(self_gt_other) ask = ask1 >= ask2 ? ask1 : ask2 ask.lock_type = (lock_type == other.lock_type ? lock_type : :lock) ask end end |
#-(other) ⇒ Ask
相对other要求多的和严格的
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/tes/request/ask.rb', line 83 def -(other) diff_data = @data.reject do |n, e| o_e = other.data[n] o_e && e <= o_e end diff_ask = self.class.new('') diff_ask.data.merge!(diff_data) diff_ask.lock_type = lock_type diff_ask end |
#<=>(other) ⇒ Object
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 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/tes/request/ask.rb', line 29 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 case [lock_type, other.lock_type] when [:lock, :share] return ret unless ret ret >=0 ? 1 : nil when [:share, :lock] return ret unless ret ret <=0 ? -1 : nil else ret end end |
#match?(data) ⇒ Boolean
计算数据是否满足要求
25 26 27 |
# File 'lib/tes/request/ask.rb', line 25 def match?(data) @data.all? {|_n, exp| exp.match?(data)} end |
#merge_able?(other) ⇒ Boolean
122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/tes/request/ask.rb', line 122 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
135 136 137 |
# File 'lib/tes/request/ask.rb', line 135 def to_s @data.values.map(&:to_s).join(',') end |