Class: INat::Report::List

Inherits:
Object
  • Object
show all
Includes:
Comparable, Enumerable, INat, INat::Report
Defined in:
lib/inat/data/sets/list.rb

Constant Summary collapse

DEFAULT_SORTER =
lambda { |ds| ds.object.respond_to?(:sort_key) ? ds.object.sort_key : ds.object }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, lister, sorter: nil, time: Time::new) ⇒ List

Returns a new instance of List.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/inat/data/sets/list.rb', line 16

def initialize source, lister, sorter: nil, time: Time::new
  @lister = lister
  @sorter = sorter || DEFAULT_SORTER
  @time = time
  @data = {}
  source.each do |observation|
    key = @lister.call observation
    if key != nil
      @data[key] ||= DataSet::new key, [], time: @time
      @data[key] << observation
    end
  end
end

Instance Attribute Details

#listerObject (readonly)

Returns the value of attribute lister.



12
13
14
# File 'lib/inat/data/sets/list.rb', line 12

def lister
  @lister
end

#sorterObject (readonly)

Returns the value of attribute sorter.



12
13
14
# File 'lib/inat/data/sets/list.rb', line 12

def sorter
  @sorter
end

Class Method Details

.zero(lister = Listers::SPECIES) ⇒ Object



30
31
32
# File 'lib/inat/data/sets/list.rb', line 30

def self.zero lister = Listers::SPECIES
  new [], lister
end

Instance Method Details

#<<(some) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/inat/data/sets/list.rb', line 83

def << some
  case some
  when Entity::Observation
    key = @lister.call some
    if key != nil
      @data[key] ||= DataSet::new key, [], time: @time
      @data[key] << some
    end
  when DataSet
    if !some.empty?
      valid = some.object != nil && some.all? { |o| @lister.call(o) == some.object }
      if valid
        if @data[some.object]
          @data[some.object] = @data[some.object] | some
        else
          @data[some.object] = some
        end
      else
        some.each do |o|
          self << o
        end
      end
    end
  when Array
    some.each do |o|
      self << o
    end
  else
    raise TypeError, "Can not add this object: #{ some.inspect }!", caller
  end
end

#<=>(other) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/inat/data/sets/list.rb', line 123

def <=> other
  return nil unless List === other
  if self.cover?(other)
    if other.cover?(self)
      return 0
    else
      return 1
    end
  else
    if other.cover?(self)
      return -1
    else
      return nil
    end
  end
end

#[](key) ⇒ Object



36
37
38
# File 'lib/inat/data/sets/list.rb', line 36

def [] key
  @data[key]
end

#add(other) ⇒ Object Also known as: +



171
172
173
# File 'lib/inat/data/sets/list.rb', line 171

def add other
  clone.add! other
end

#add!(other) ⇒ Object



149
150
151
152
153
154
# File 'lib/inat/data/sets/list.rb', line 149

def add! other
  other.each do |ds|
    self << ds
  end
  self
end

#apply!(*data) ⇒ Object



140
141
142
143
# File 'lib/inat/data/sets/list.rb', line 140

def apply! *data
  @lister, @sorter, @time, @data = data
  self
end

#cloneObject



145
146
147
# File 'lib/inat/data/sets/list.rb', line 145

def clone
  List::zero.apply! @lister, @sorter, @time, @data.dup
end

#countObject



66
67
68
# File 'lib/inat/data/sets/list.rb', line 66

def count
  @data.size
end

#cover?(other) ⇒ Boolean

Returns:



117
118
119
# File 'lib/inat/data/sets/list.rb', line 117

def cover? other
  other.any? { |ds| self.include?(ds.object) }
end

#eachObject



40
41
42
43
44
45
46
47
48
# File 'lib/inat/data/sets/list.rb', line 40

def each
  if block_given?
    @data.values.sort_by { |ds| @sorter.call(ds) }.each do |ds|
      yield ds
    end
  else
    to_enum :each
  end
end

#empty?Boolean

Returns:



62
63
64
# File 'lib/inat/data/sets/list.rb', line 62

def empty?
  @data.empty?
end

#include?(key) ⇒ Boolean Also known as: ===

Returns:



58
59
60
# File 'lib/inat/data/sets/list.rb', line 58

def include? key
  @data.has_key? key
end

#mul(other) ⇒ Object Also known as: *



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/inat/data/sets/list.rb', line 175

def mul other
  # А вот здесь будет эффективней старое решение
  result = List::new [], @lister, sorter: @sorter, time: @time
  @data.each do |key, ds|
    if other.include?(key)
      summa = ds | other[key]
      self << summa
    end
  end
  result
end

#mul!(other) ⇒ Object



156
157
158
159
160
161
162
163
164
# File 'lib/inat/data/sets/list.rb', line 156

def mul! other
  @data.delete_if { | key, _ | !other.include?(key) }
  other.each do |ds|
    if self.include?(ds.object)
      self << ds
    end
  end
  self
end

#observation_countObject



70
71
72
# File 'lib/inat/data/sets/list.rb', line 70

def observation_count
  @data.values.map { |ds| ds.count }.sum
end

#sub(other) ⇒ Object Also known as: -



187
188
189
# File 'lib/inat/data/sets/list.rb', line 187

def sub other
  clone.sub! other
end

#sub!(other) ⇒ Object



166
167
168
169
# File 'lib/inat/data/sets/list.rb', line 166

def sub! other
  @data.delete_if { | key, _ | other.include?(key) }
  self
end

#to_datasetObject



50
51
52
53
54
55
56
# File 'lib/inat/data/sets/list.rb', line 50

def to_dataset
  observations = []
  @data.values.each do |ds|
    observations += ds.observations
  end
  DataSet::new nil, observations, time: @time
end

#where(&block) ⇒ Object

Raises:

  • (ArgumentError)


74
75
76
77
78
79
80
81
# File 'lib/inat/data/sets/list.rb', line 74

def where &block
  raise ArgumentError, "Block must be provided!", caller unless block_given?
  result = List::new [], @lister, sorter: @sorter, time: @time
  self.each do |ds|
    result << ds if yield(ds)
  end
  result
end