Class: OpenAPISourceTools::Ordering::Order
- Inherits:
-
Object
- Object
- OpenAPISourceTools::Ordering::Order
- Defined in:
- lib/openapi/sourcetools/order.rb
Overview
Handles ordering keys or an array using the given key order.
Instance Attribute Summary collapse
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
Class Method Summary collapse
Instance Method Summary collapse
- #apply(item) ⇒ Object
- #apply_array(array) ⇒ Object
- #apply_hash(hash) ⇒ Object
-
#initialize(keys, regexp_prefix) ⇒ Order
constructor
A new instance of Order.
Constructor Details
#initialize(keys, regexp_prefix) ⇒ Order
Returns a new instance of Order.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/openapi/sourcetools/order.rb', line 165 def initialize(keys, regexp_prefix) @patterns = [] has_asterisk = false keys.each_with_index do |string, idx| if string.start_with?(regexp_prefix) @patterns.push(KeyPattern.new(Regexp.new(string[regexp_prefix.size..]), idx, idx)) elsif string == '*' @patterns.push(KeyPattern.new(Regexp.new('^.*$'), idx, keys.size)) unless has_asterisk # First used. has_asterisk = true else @patterns.push(KeyPattern.new(Regexp.new("^#{Regexp.escape(string)}$"), idx, idx)) end end @patterns.push(KeyPattern.new(Regexp.new('^.*$'), @patterns.size, keys.size + 1)) unless has_asterisk end |
Instance Attribute Details
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
163 164 165 |
# File 'lib/openapi/sourcetools/order.rb', line 163 def patterns @patterns end |
Class Method Details
.orderable?(item) ⇒ Boolean
157 158 159 160 161 |
# File 'lib/openapi/sourcetools/order.rb', line 157 def self.orderable?(item) return true if item.is_a?(Hash) return true if item.is_a?(Array) false end |
Instance Method Details
#apply(item) ⇒ Object
248 249 250 251 252 |
# File 'lib/openapi/sourcetools/order.rb', line 248 def apply(item) return apply_array(item) if item.is_a?(Array) return apply_hash(item) if item.is_a?(Hash) false end |
#apply_array(array) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/openapi/sourcetools/order.rb', line 181 def apply_array(array) return true if array.size < 2 values = [] array.each do |item| amended = { item: item, values: [] } unless item.is_a?(Hash) amended[:values] = Array.new(@patterns.size, nil) values.push(amended) next end @patterns.each do |kp| if kp.any? amended[:values].push(nil) next end # Find first matching key. v = nil item.each_key do |item_key| next unless kp.match?(item_key) v = item[item_key] break end amended[:values].push((v.is_a?(Array) || v.is_a?(Hash)) ? nil : v) end values.push(amended) end values.sort! { |a, b| OpenAPISourceTools::Ordering.array_item_compare(a, b) } values.each_with_index { |amended, i| array[i] = amended[:item] } true end |
#apply_hash(hash) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/openapi/sourcetools/order.rb', line 215 def apply_hash(hash) return true if hash.size < 2 groups = @patterns.map do |kp| { kp: kp, keys: [] } end s2orig = {} hash.each_key do |key| skey = key.is_a?(String) ? key : key.to_s s2orig[skey] = key best_priority = groups.size + 1 best_idx = nil groups.each do |group| kp = group[:kp] next if best_priority <= kp.priority next unless kp.match?(skey) best_priority = kp.priority best_idx = kp.index end groups[best_idx][:keys].push(skey) unless best_idx.nil? end ordered = {} groups.each do |group| group[:keys].sort! group[:keys].each { |k| ordered[s2orig[k]] = hash[s2orig[k]] } end hash.clear hash.merge!(ordered) true end |