merge_enum 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7a7cbcc12b63d8c7a55b8d4c4d3f8de7a749c400
4
- data.tar.gz: 197e9e1c595d09167bf84fec15ee6488febd3f15
3
+ metadata.gz: b344598810bdbe8112d13374ee3ef4ca181c72d5
4
+ data.tar.gz: c996eeb69073556486627ab077c283b36087bed4
5
5
  SHA512:
6
- metadata.gz: 6f3eb08663c8186902352a71b5b007bdb08f4214087a0f18b578d48e3a01af04bd760824db481a351aa4c7d6e27f5dda7c9b949f887d829d5141bb5650663a1f
7
- data.tar.gz: ea1d6992ceeff7c5832f8c01e6187a40672379db773f4215841005d07666114f1ce7adb49af717ffebf95e0235727f7c61b09bf3f66449a890081798abccbc0d
6
+ metadata.gz: 0c06db945cffc49ab77aed6a1dd72c09d1eb55141301562ef2ddab741bfece4e195006d40f5514cc9cef9235fdaff2b660297e77b52c03f69b875c118fc34577
7
+ data.tar.gz: 97a5db5273067661a5f5a321a75ac289664ec1306be13bb0704b1de854d280417a4c5e5ec10424ce6f72d56205c7a4a9d8acb08d167a0d10515f91e283c23cb4
@@ -20,18 +20,7 @@ module MergeEnum
20
20
  # options
21
21
  opt_fst = @options[:first]
22
22
  opt_fst = opt_fst.to_i if opt_fst
23
- opt_cmpct = @options[:compact]
24
- opt_cmpct_proc = if opt_cmpct.is_a? Proc
25
- if opt_cmpct.arity == 0
26
- -> (e) { opt_cmpct.call }
27
- else
28
- opt_cmpct
29
- end
30
- elsif opt_cmpct
31
- -> (e) { e.nil? }
32
- else
33
- -> (e) { false }
34
- end
23
+ (opt_proc, _proc) = merge_options_proc @options
35
24
 
36
25
  # local variables
37
26
  cnt = 0
@@ -53,17 +42,17 @@ module MergeEnum
53
42
  c = c.call fst
54
43
  called_first = true
55
44
  else
56
- c = c.call fst, opt_cmpct
45
+ c = c.call fst, opt_proc
57
46
  called_first = true
58
47
  end
59
48
  end
60
49
 
61
50
  if fst
62
51
  # with first option
63
- c = c.first fst unless called_first or opt_cmpct
52
+ c = c.first fst unless called_first or _proc
64
53
  _cnt = 0
65
54
  c.each do |e|
66
- next if opt_cmpct_proc.call e
55
+ next if _proc and not opt_proc.call e
67
56
  block.call e
68
57
  _cnt += 1
69
58
  break if fst <= _cnt
@@ -72,7 +61,7 @@ module MergeEnum
72
61
  else
73
62
  # without first option
74
63
  c.each do |e|
75
- next if opt_cmpct_proc.call e
64
+ next if _proc and not opt_proc.call e
76
65
  block.call e
77
66
  end
78
67
  end
@@ -80,6 +69,45 @@ module MergeEnum
80
69
  self
81
70
  end
82
71
 
72
+ private
73
+
74
+ def merge_options_proc options
75
+ opts = []
76
+
77
+ opt_cmpct = options[:compact]
78
+ opts << if opt_cmpct.is_a? Proc
79
+ if opt_cmpct.arity == 0
80
+ [-> (e) { not opt_cmpct.call }, true]
81
+ else
82
+ [-> (e) { not opt_cmpct.call e }, true]
83
+ end
84
+ elsif opt_cmpct
85
+ [-> (e) { not e.nil? }, true]
86
+ else
87
+ [-> (e) { true }, false]
88
+ end
89
+
90
+ opt_slct = options[:select]
91
+ opts << if opt_slct.is_a? Proc
92
+ if opt_slct.arity == 0
93
+ [-> (e) { opt_slct.call }, true]
94
+ else
95
+ [opt_slct, true]
96
+ end
97
+ elsif opt_slct
98
+ raise ":select is must be a Proc"
99
+ else
100
+ [-> (e) { true }, false]
101
+ end
102
+
103
+ [
104
+ -> (e) { opts.all?{ |opt| opt[0].call e } },
105
+ opts.any?{ |opt| opt[1] }
106
+ ]
107
+ end
108
+
109
+ public
110
+
83
111
  def concat arg
84
112
  self.class.new *@collections, arg, @options
85
113
  end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  module MergeEnum
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5