Class: INat::Report::List
- Inherits:
-
Object
- Object
- INat::Report::List
- 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
-
#lister ⇒ Object
readonly
Returns the value of attribute lister.
-
#sorter ⇒ Object
readonly
Returns the value of attribute sorter.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(some) ⇒ Object
- #<=>(other) ⇒ Object
- #[](key) ⇒ Object
- #add(other) ⇒ Object (also: #+)
- #add!(other) ⇒ Object
- #apply!(*data) ⇒ Object
- #clone ⇒ Object
- #count ⇒ Object
- #cover?(other) ⇒ Boolean
- #each ⇒ Object
- #empty? ⇒ Boolean
- #include?(key) ⇒ Boolean (also: #===)
-
#initialize(source, lister, sorter: nil, time: Time::new) ⇒ List
constructor
A new instance of List.
- #mul(other) ⇒ Object (also: #*)
- #mul!(other) ⇒ Object
- #observation_count ⇒ Object
- #sub(other) ⇒ Object (also: #-)
- #sub!(other) ⇒ Object
- #to_dataset ⇒ Object
- #where(&block) ⇒ Object
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
#lister ⇒ Object (readonly)
Returns the value of attribute lister.
12 13 14 |
# File 'lib/inat/data/sets/list.rb', line 12 def lister @lister end |
#sorter ⇒ Object (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
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 |
#clone ⇒ Object
145 146 147 |
# File 'lib/inat/data/sets/list.rb', line 145 def clone List::zero.apply! @lister, @sorter, @time, @data.dup end |
#count ⇒ Object
66 67 68 |
# File 'lib/inat/data/sets/list.rb', line 66 def count @data.size end |
#cover?(other) ⇒ Boolean
117 118 119 |
# File 'lib/inat/data/sets/list.rb', line 117 def cover? other other.any? { |ds| self.include?(ds.object) } end |
#each ⇒ Object
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 |
#include?(key) ⇒ Boolean Also known as: ===
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_count ⇒ Object
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_dataset ⇒ Object
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
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 |