Module: Xipai::TableSet

Defined in:
lib/xipai/table_set.rb

Class Method Summary collapse

Class Method Details

.arrange(mode, attrs) ⇒ Object



26
27
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
69
70
71
72
73
74
75
76
77
78
# File 'lib/xipai/table_set.rb', line 26

def arrange(mode, attrs)
  params = {}
  Some[mode.intern].match do |m|
    m.some(->(x){x == :team}) {
      params[:mode]              = mode.intern
      params[:key_words]         = normalize_csv_array(attrs[:key_words])
      params[:hashcode]          = attrs[:hashcode]
      params[:without_hashcode]  = attrs[:without_hashcode]
      params[:pretty]            = attrs[:pretty]
      params[:items]             = normalize_csv_array(attrs[:items])
      params[:number_of_members] = attrs[:number_of_members]
      params[:verbose]           = attrs[:verbose]
      params[:replay_output]     = attrs[:replay_output]
    }
    m.some(->(x){x == :lottery}) {
      params[:mode]              = mode.intern
      params[:key_words]         = normalize_csv_array(attrs[:key_words])
      params[:hashcode]          = attrs[:hashcode]
      params[:without_hashcode]  = attrs[:without_hashcode]
      params[:pretty]            = attrs[:pretty]
      params[:items]             = normalize_csv_array(attrs[:items])
      params[:number_of_winners] = attrs[:number_of_winners]
      params[:verbose]           = attrs[:verbose]
      params[:replay_output]     = attrs[:replay_output]
    }
    m.some(->(x){x == :pair}) {
      params[:mode]              = mode.intern
      params[:key_words]         = normalize_csv_array(attrs[:key_words])
      params[:hashcode]          = attrs[:hashcode]
      params[:without_hashcode]  = attrs[:without_hashcode]
      params[:key_items]         = normalize_csv_array(attrs[:key_items])
      params[:pretty]            = attrs[:pretty]
      params[:value_items]       = normalize_csv_array(attrs[:value_items])
      params[:verbose]           = attrs[:verbose]
      params[:replay_output]     = attrs[:replay_output]
    }
    m.some(->(x){x == :order}) {
      params[:mode]              = mode.intern
      params[:key_words]         = normalize_csv_array(attrs[:key_words])
      params[:hashcode]          = attrs[:hashcode]
      params[:without_hashcode]  = attrs[:without_hashcode]
      params[:pretty]            = attrs[:pretty]
      params[:items]             = normalize_csv_array(attrs[:items])
      params[:verbose]           = attrs[:verbose]
      params[:replay_output]     = attrs[:replay_output]
    }
    m.some {raise "Error: mode invalid."}
  end

  table = Object.const_get("Xipai::Arrangement::#{mode.capitalize}").new(hash_deep_symbolize(params))

  return table if table.valid!
end

.hash_deep_symbolize(obj) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/xipai/table_set.rb', line 90

def hash_deep_symbolize(obj)
  return Some[obj].match do |m|
    m.some(->(x){x.is_a?(Hash)}) {
      obj.inject({}){|memo,(k,v)| memo[k.to_s.intern] = hash_deep_symbolize(v); memo}
    }
    m.some(->(x){x.is_a?(Array)}) {
      obj.inject([]){|memo,v| memo << hash_deep_symbolize(v); memo}
    }
    m.some {obj}
  end
end

.normalize_csv_array(obj) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/xipai/table_set.rb', line 80

def normalize_csv_array(obj)
  return Some[obj].match do |m|
    m.some(->(x){x.is_a?(String)}) {
      obj == "" ? [] : obj.split(",").map(&:strip)
    }
    m.some(->(x){x.is_a?(Array)})  { obj }
    m.some { "Error: normalize_csv_array invalid value(s)."}
  end
end

.parse_options(mode, options) ⇒ Object



22
23
24
# File 'lib/xipai/table_set.rb', line 22

def parse_options(mode, options)
  return arrange(mode, hash_deep_symbolize(options))
end

.parse_yaml(yamldata) ⇒ Object



17
18
19
20
# File 'lib/xipai/table_set.rb', line 17

def parse_yaml(yamldata)
  data = hash_deep_symbolize(YAML.load(yamldata))
  return arrange(data[:mode], data)
end