Class: Xipai::Arrangement::Pair

Inherits:
Base
  • Object
show all
Defined in:
lib/xipai/arrangement/pair.rb

Instance Attribute Summary

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#__attributes__, #__require_attributes__, attributes, #dump_replay_yaml, #initialize, #method_missing, #method_missing_hook, require_attributes, #to_replay_data, #valid!

Constructor Details

This class inherits a constructor from Xipai::Arrangement::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Xipai::Arrangement::Base

Instance Method Details

#applyObject



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/xipai/arrangement/pair.rb', line 15

def apply
  _hashcode, _shuffled_keys = Xipai::Core.scrumble!(
    key_words, hashcode, key_items, params
  ).tap {|me| break *[me[:hashcode], me[:shuffled]]}

  _hashcode, _shuffled_values = Xipai::Core.scrumble!(
    key_words, _hashcode, value_items, {}
  ).tap {|me| break *[me[:hashcode], me[:shuffled]]}

  result_set = pair(_shuffled_keys, _shuffled_values)

  return Xipai::Result.new(mode, _hashcode, result_set, params)
end

#pair(_keys, _values) ⇒ 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
# File 'lib/xipai/arrangement/pair.rb', line 29

def pair(_keys, _values)
  return Hash[Some[{a: _keys.size, b: _values.size}].match { |m|
    m.some(->(x){x[:a] > x[:b]}) {
      result = [].tap {|me|
        _keys.each.with_index do |key, index|
          Some[_values[index]].match do |mat|
            mat.some(->(x){!x.nil?}) { me.push([key, _values[index]]) }
            mat.some(->(x){ x.nil?}) { me.push([key, :null]) }
          end
        end
      }
    }
    m.some(->(x){x[:a] < x[:b]}) {
      result = [].tap {|me|
        _values.each.with_index do |value, index|
          Some[_keys[index]].match do |mat|
            mat.some(->(x){!x.nil?}) { me.push([key[index], value]) }
            mat.some(->(x){ x.nil?}) { me.push([index.to_s, value]) }
          end
        end
      }
    }
    m.some(->(x){x[:a] == x[:b]}) { |x|
      result = [].tap {|me|
        [*0..(x[:a].to_i-1)].each do |index|
          me.push([_keys[index], _values[index]])
        end
      }
    }
  }]
end