diff --git a/lib/openhab/core/types/time_series.rb b/lib/openhab/core/types/time_series.rb index 7b0c240bf..0811834f0 100644 --- a/lib/openhab/core/types/time_series.rb +++ b/lib/openhab/core/types/time_series.rb @@ -122,6 +122,25 @@ def add(timestamp, state) self end + # + # Appends an entry to self, returns self + # + # @param [Array] entry a two-element array with the timestamp and state. + # The timestamp can be an {Instant} or any object that responds to #to_zoned_date_time. + # @return [self] + # + # @example Append an entry + # time_series << [Time.at(2), 2] + # + def <<(entry) + raise ArgumentError, "entry must be an Array, but was #{entry.class}" unless entry.respond_to?(:to_ary) + + entry = entry.to_ary + raise ArgumentError, "entry must be an Array of size 2, but was #{entry.size}" unless entry.size == 2 + + add(entry[0], entry[1]) + end + private def to_instant(timestamp) diff --git a/spec/openhab/core/types/time_series_spec.rb b/spec/openhab/core/types/time_series_spec.rb index f70de194c..f6971b3d9 100644 --- a/spec/openhab/core/types/time_series_spec.rb +++ b/spec/openhab/core/types/time_series_spec.rb @@ -70,6 +70,17 @@ end end + describe "#<<" do + it "works" do + ts << [Time.at(3), 3] + expect(ts.size).to be 3 + expect(ts.first.state).to eql DecimalType.new(1) + expect(ts.begin).to eql Instant.of_epoch_second(1) + expect(ts.last.state).to eql DecimalType.new(3) + expect(ts.end).to eql Instant.of_epoch_second(3) + end + end + context "when accessed as an Array" do it "is frozen" do expect(ts.states).to be_frozen