ruby_list_comprehension 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ruby_list_comprehension.rb +29 -14
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25d56bf9d1cd2645750c99f4f46678893ba00e6d4075712a5ac7b845aa479412
|
4
|
+
data.tar.gz: f30a9fbd24e4595c1bde00bb344d9482f499e8ae87cb6e04035ef5123c94b8aa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90526fa524e2b0377da07dd436f6011fa2d38e1363639caf19d58ed8afb36c04501708058d3ab6feb86f4c8452af9d281d07a3cbd122eba6c51855e6e2c22461
|
7
|
+
data.tar.gz: 210c7af5d5a6355c7a38a9276e802c689561f299fed9ed23c24bbfe37c6b5c5b35bd27f9e4c7afc3f575cd0473d9f3b4e03ed444cad09ed0cd52a37b68b27ee2
|
@@ -1,14 +1,33 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'readline'
|
4
|
+
|
3
5
|
class ListComprehension
|
4
|
-
attr_accessor :cache, :caching, :mappable, :filterable, :iterable, :var, :list
|
5
|
-
attr_reader :c, :version, :op, :cache_count
|
6
|
+
attr_accessor :cache, :caching, :mappable, :filterable, :iterable, :var, :list, :location, :line
|
7
|
+
attr_reader :c, :version, :op, :cache_count, :filename
|
6
8
|
|
7
9
|
def [](list_comp)
|
8
|
-
|
10
|
+
if @filename == 'pry' || @filename == 'irb'
|
11
|
+
@line = Readline::HISTORY.to_a.uniq[-1].strip
|
12
|
+
start = @line.index('l[') + 2
|
13
|
+
ending = @line[start..-1].index('end') + 5
|
14
|
+
@line = @line[start...ending]
|
15
|
+
else
|
16
|
+
@location = caller_locations.last.to_s.scan(/\d+/).last.to_i
|
17
|
+
file = File.open($PROGRAM_NAME)
|
18
|
+
file_data = file.readlines.map(&:chomp)
|
19
|
+
file.close
|
20
|
+
@line = file_data[@location - 1].strip
|
21
|
+
start = @line.index('l[')
|
22
|
+
ending = @line[start..-1].index('end')
|
23
|
+
@line = @line[start+2...ending + 6].chop
|
24
|
+
end
|
25
|
+
# p @line
|
26
|
+
c[@line]
|
9
27
|
end
|
10
28
|
|
11
29
|
def initialize
|
30
|
+
@filename = $PROGRAM_NAME
|
12
31
|
@cache = {}
|
13
32
|
@count = 0
|
14
33
|
@op = {}
|
@@ -37,15 +56,13 @@ class ListComprehension
|
|
37
56
|
@var = arr[1]
|
38
57
|
|
39
58
|
# replace initial semicolon with do if needed for parser
|
40
|
-
if arr[3..-1].any?{|x|x.include?(';')} && !arr.include?('do')
|
59
|
+
if arr[3..-1].any? { |x| x.include?(';') } && !arr.include?('do')
|
41
60
|
arr = list.to_s.sub(';', ' do ').split
|
42
61
|
end
|
43
|
-
arr.insert(-2, 'do') if arr.none?{|x|x.include?('do')} && arr.none?{|x|x.include?('do')}
|
44
|
-
# p arr
|
62
|
+
arr.insert(-2, 'do') if arr.none? { |x| x.include?('do') } && arr.none? { |x| x.include?('do') }
|
45
63
|
|
46
64
|
# pre-eval to check for invalid syntax
|
47
65
|
begin
|
48
|
-
# p arr.join(' ')
|
49
66
|
res = instance_eval(arr.join(' '))
|
50
67
|
return [] if res.nil?
|
51
68
|
rescue SyntaxError => se
|
@@ -55,13 +72,12 @@ class ListComprehension
|
|
55
72
|
# check for hash to parse csv's
|
56
73
|
iterable = arr[3]
|
57
74
|
return [{}] if iterable == '{}'
|
75
|
+
|
58
76
|
m_data = copy1[3...copy1.rindex('do')].match(/({.+[=>:].+})/)
|
59
77
|
if m_data
|
60
78
|
first_hash = m_data[0].split(';')[0]
|
61
79
|
if @list.index(first_hash) == 9
|
62
|
-
if instance_eval(first_hash).is_a? Hash
|
63
|
-
iterable = first_hash
|
64
|
-
end
|
80
|
+
iterable = first_hash if instance_eval(first_hash).is_a? Hash
|
65
81
|
end
|
66
82
|
end
|
67
83
|
@iterable = instance_eval(iterable)
|
@@ -70,7 +86,6 @@ class ListComprehension
|
|
70
86
|
map_condition = arr[arr.index('do') + 1...(arr.index('if') || arr.index('end'))]
|
71
87
|
@filterable = if_condition.join(' ')
|
72
88
|
@mappable = map_condition.join(' ')
|
73
|
-
# p @mappable
|
74
89
|
### list_comprehension identity currently uses for(each) as if normal ruby
|
75
90
|
# p @filterable
|
76
91
|
# p @mappable
|
@@ -82,13 +97,13 @@ class ListComprehension
|
|
82
97
|
if @mappable == @var
|
83
98
|
@op[@count] = 'filter'
|
84
99
|
@count += 1
|
85
|
-
return @iterable.filter { |x| instance_eval(@filterable.gsub(@var, x.to_s))}
|
100
|
+
return @iterable.filter { |x| instance_eval(@filterable.gsub(@var, x.to_s)) }
|
86
101
|
end
|
87
102
|
|
88
103
|
if @filterable == 'true' || @filterable == @var
|
89
104
|
@op[@count] = 'map'
|
90
105
|
@count += 1
|
91
|
-
return @iterable.map { |x| instance_eval(@mappable.gsub(@var, x.to_s))}
|
106
|
+
return @iterable.map { |x| instance_eval(@mappable.gsub(@var, x.to_s)) }
|
92
107
|
end
|
93
108
|
|
94
109
|
filter_map_condition_args = "#@mappable if #@filterable"
|
@@ -98,7 +113,7 @@ class ListComprehension
|
|
98
113
|
@count += 1
|
99
114
|
return @iterable.filter_map { |x| instance_eval(filter_map_condition_args.gsub(@var, x.to_s)) }
|
100
115
|
else
|
101
|
-
@op[@count] =
|
116
|
+
@op[@count] ="map&compact"
|
102
117
|
@count += 1
|
103
118
|
@iterable.map { |x| instance_eval(filter_map_condition_args.gsub!(@var, x.to_s)) }.compact!
|
104
119
|
end
|