Class: INat::Report::DataSet

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, observations, time: Time::new) ⇒ DataSet

Returns a new instance of DataSet.



15
16
17
18
19
20
21
22
23
24
# File 'lib/inat/data/sets/dataset.rb', line 15

def initialize object, observations, time: Time::new
  @object = object
  @time = time
  @observations = observations.sort_by { |o| o.sort_key }.uniq
  @sorted = true
  @by_id = {}
  @observations.each do |o|
    @by_id[o.id] = o
  end
end

Instance Attribute Details

#objectObject

TODO: переделать select так, чтобы не было необходимости во внешнем присваивании



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

def object
  @object
end

#observationsObject (readonly)

Returns the value of attribute observations.



13
14
15
# File 'lib/inat/data/sets/dataset.rb', line 13

def observations
  @observations
end

#timeObject (readonly)

Returns the value of attribute time.



11
12
13
# File 'lib/inat/data/sets/dataset.rb', line 11

def time
  @time
end

Class Method Details

.zeroObject



26
27
28
# File 'lib/inat/data/sets/dataset.rb', line 26

def self.zero
  new nil, []
end

Instance Method Details

#&(other) ⇒ Object



94
95
96
97
# File 'lib/inat/data/sets/dataset.rb', line 94

def & other
  obj = @object == other.object ? @object : nil
  INat::Report::DataSet::new obj, @observations.select { |o| other.include?(o) }, time: Time::new
end

#-(other) ⇒ Object



99
100
101
102
# File 'lib/inat/data/sets/dataset.rb', line 99

def - other
  obj = @object == other.object ? @object : nil
  INat::Report::DataSet::new obj, @observations.select { |o| !other.include?(o) }, time: Time::new
end

#<<(observation) ⇒ Object

Raises:

  • (TypeError)


79
80
81
82
83
84
85
86
87
# File 'lib/inat/data/sets/dataset.rb', line 79

def << observation
  raise TypeError, "Argument must be an Observation (#{ observation.inspect })!" unless Entity::Observation === observation
  # TODO: добавить массивы и прочее
  if !self.include?(observation)
    @observations << observation
    @by_id[observation.id] = observation
    @sorted = false
  end
end

#countObject



56
57
58
# File 'lib/inat/data/sets/dataset.rb', line 56

def count
  @observations.size
end

#eachObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/inat/data/sets/dataset.rb', line 32

def each
  if block_given?
    @observations.sort_by! { |o| o.sort_key } unless @sorted
    @sorted = true
    @observations.each do |o|
      yield o
    end
  else
    to_enum :each
  end
end

#empty?Boolean

Returns:



68
69
70
# File 'lib/inat/data/sets/dataset.rb', line 68

def empty?
  @observations.empty?
end

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

Returns:



64
65
66
# File 'lib/inat/data/sets/dataset.rb', line 64

def include? observation
  @by_id.has_key? observation.id
end

#reverse_eachObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/inat/data/sets/dataset.rb', line 44

def reverse_each
  if block_given?
    @observations.sort_by! { |o| o.sort_key } unless @sorted
    @sorted = true
    @observations.reverse_each do |o|
      yield o
    end
  else
    to_enum :reverse_each
  end
end

#to_aObject



104
105
106
# File 'lib/inat/data/sets/dataset.rb', line 104

def to_a
  @observations.dup
end

#to_list(lister = Listers::SPECIES, sorter: nil) ⇒ Object



60
61
62
# File 'lib/inat/data/sets/dataset.rb', line 60

def to_list lister = Listers::SPECIES, sorter: nil
  List::new self, lister, sorter: sorter, time: @time
end

#where(&block) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
# File 'lib/inat/data/sets/dataset.rb', line 72

def where &block
  raise ArgumentError, "Block must be provided!", caller unless block_given?
  DataSet::new nil, @observations.select(&block), time: @time
end

#|(other) ⇒ Object



89
90
91
92
# File 'lib/inat/data/sets/dataset.rb', line 89

def | other
  obj = @object == other.object ? @object : nil
  INat::Report::DataSet::new obj, @observations + other.observations, time: Time::new
end