Class: INatGet::Data::DSL::List
- Inherits:
-
Object
- Object
- INatGet::Data::DSL::List
show all
- Includes:
- Enumerable, INatGet::Data::DSL
- Defined in:
- lib/inat-get/data/dsl/list.rb
#AND, #ANYTHING, #NOT, #NOTHING, #OR, #Q, #console, #erb_report, #finish_time, #get_identification, #get_observation, #get_place, #get_project, #get_taxon, #get_user, #now, #select_identifications, #select_observations, #select_places, #select_projects, #select_taxa, #select_users, #start_time, #time_range, #today, #version, #version!, #version?, #version_alias
Class Method Details
.commons(num, *src) ⇒ List
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
# File 'lib/inat-get/data/dsl/list.rb', line 231
def self.commons num, *src
keys = {}
values = {}
src.each do |s|
s.each do |value|
key = value.key
if value
keys[key] ||= 0
keys[key] += 1
values[key] ||= []
values[key] << value
end
end
end
selected_keys = keys.select { |_, value| value >= num }.keys
result = new
values.slice(*selected_keys).each_value do |value|
result.add! value.reduce(:+)
end
result
end
|
Instance Method Details
#*(other) ⇒ List
119
120
121
|
# File 'lib/inat-get/data/dsl/list.rb', line 119
def * other
copy.mul! other
end
|
#+(other) ⇒ List
114
115
116
|
# File 'lib/inat-get/data/dsl/list.rb', line 114
def + other
copy.add! other
end
|
#-(other) ⇒ List
124
125
126
|
# File 'lib/inat-get/data/dsl/list.rb', line 124
def - other
copy.sub! other
end
|
27
28
29
|
# File 'lib/inat-get/data/dsl/list.rb', line 27
def [] key
@datasets[key]
end
|
#add!(other) ⇒ self
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/inat-get/data/dsl/list.rb', line 50
def add! other
other = [ other ] if other.is_a?(INatGet::Data::DSL::Dataset)
other.each do |ds|
if @datasets.has_key?(ds.key)
@datasets[ds.key] += ds
else
@datasets[ds.key] = ds
end
end
self
end
|
#count ⇒ Integer
Also known as:
size
166
167
168
|
# File 'lib/inat-get/data/dsl/list.rb', line 166
def count
@datasets.count
end
|
#datasets ⇒ Array<Dataset>
Also known as:
to_a, values
41
42
43
44
45
46
47
|
# File 'lib/inat-get/data/dsl/list.rb', line 41
def datasets
if @sorter
@datasets.values.sort_by(&@sorter)
else
@datasets.values
end
end
|
#each {|ds| ... } ⇒ self
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
# File 'lib/inat-get/data/dsl/list.rb', line 137
def each
return to_enum(__method__) unless block_given?
sorted = if @sorter
@datasets.values.sort_by(&@sorter)
else
@datasets.values
end
total = sorted.size
Thread::current[:total] ||= 0
Thread::current[:total] += total
console.update status: 'listing...', total: Thread::current[:total]
current = 0
Thread::current[:current] ||= 0
sorted.each do |ds|
current += 1
Thread::current[:current] += 1
console.update status: 'listing...', current: Thread::current[:current]
yield ds
end
self
ensure
if current < total
Thread::current[:total] -= total - current
console.update total: Thread::current[:total]
end
console.update status: 'listed'
end
|
#empty? ⇒ Boolean
216
217
218
|
# File 'lib/inat-get/data/dsl/list.rb', line 216
def empty?
@datasets.empty?
end
|
#filter {|ds| ... } ⇒ List
199
200
201
202
|
# File 'lib/inat-get/data/dsl/list.rb', line 199
def filter &block
return to_enum(__method__) unless block_given?
copy.filter!(&block)
end
|
#filter! {|ds| ... } ⇒ self
175
176
177
178
179
180
181
182
|
# File 'lib/inat-get/data/dsl/list.rb', line 175
def filter!
if block_given?
@datasets.filter! do |_, value|
yield value
end
end
self
end
|
#filter_keys {|key| ... } ⇒ List
207
208
209
210
|
# File 'lib/inat-get/data/dsl/list.rb', line 207
def filter_keys &block
return to_enum(__method__) unless block_given?
copy.filter_keys!(&block)
end
|
#filter_keys! {|key| ... } ⇒ self
187
188
189
190
191
192
193
194
|
# File 'lib/inat-get/data/dsl/list.rb', line 187
def filter_keys!
if block_given?
@datasets.filter! do |key, _|
yield key
end
end
self
end
|
#has_key?(key) ⇒ Boolean
212
213
214
|
# File 'lib/inat-get/data/dsl/list.rb', line 212
def has_key? key
@datasets.has_key? key
end
|
#keys ⇒ Array<Object>
22
23
24
|
# File 'lib/inat-get/data/dsl/list.rb', line 22
def keys
@datasets.keys
end
|
#mul!(other) ⇒ self
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/inat-get/data/dsl/list.rb', line 63
def mul! other
result = {}
case other
when INatGet::Data::DSL::List
other.each do |ds|
sds = @datasets[ds.key]
if sds
result[ds.key] = sds + ds
end
end
when Enumerable
other.each do |key|
sds = @datasets[key]
if sds
result[key] = sds
end
end
end
@datasets = result
self
end
|
#sort {|ds| ... } ⇒ List
259
260
261
262
263
264
265
|
# File 'lib/inat-get/data/dsl/list.rb', line 259
def sort &block
if block_given?
copy.sort!(&block)
else
copy.sort!
end
end
|
#sort! {|ds| ... } ⇒ self
269
270
271
272
273
274
275
276
|
# File 'lib/inat-get/data/dsl/list.rb', line 269
def sort! &block
if block_given?
@sorter = block
else
@sorter = lambda { |ds| ds.key }
end
self
end
|
#sub!(other) ⇒ self
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/inat-get/data/dsl/list.rb', line 86
def sub! other
case other
when INatGet::Data::DSL::List
other.each do |ds|
@datasets.delete ds.key
end
when Enumerable
other.each do |key|
@datasets.delete key
end
end
self
end
|
101
102
103
104
105
106
107
|
# File 'lib/inat-get/data/dsl/list.rb', line 101
def to_dataset
result = INatGet::Data::DSL::Dataset::new(nil, NOTHING, true)
@datasets.each do |_, ds|
result += ds
end
result
end
|
#to_h ⇒ Hash
224
225
226
|
# File 'lib/inat-get/data/dsl/list.rb', line 224
def to_h
@datasets.dup
end
|