Class: RSpec::OpenHAB::Core::Mocks::PersistenceService

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rspec/openhab/core/mocks/persistence_service.rb

Defined Under Namespace

Classes: HistoricItem

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePersistenceService

Returns a new instance of PersistenceService.



25
26
27
28
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 25

def initialize
  @id = "default"
  reset
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



23
24
25
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 23

def id
  @id
end

Instance Method Details

#get_default_strategiesObject

rubocop:disable Naming/AccessorMethodName must match Java interface



85
86
87
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 85

def get_default_strategies # rubocop:disable Naming/AccessorMethodName must match Java interface
  [org.openhab.core.persistence.strategy.PersistenceStrategy::Globals::CHANGE]
end

#get_item_infoObject

rubocop:disable Naming/AccessorMethodName must match Java interface



79
80
81
82
83
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 79

def get_item_info # rubocop:disable Naming/AccessorMethodName must match Java interface
  @data.map do |(n, entries)|
    [n, entries.length, entries.first.timestamp, entries.last.timestamp]
  end.to_set
end

#query(filter) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 61

def query(filter)
  result = []

  query_internal(filter) do |item_history, index|
    result << item_history[index]

    return result if filter.page_number.zero? && result.length == filter.page_size && filter.item_name
  end

  result.sort_by! { |hi| hi.timestamp.to_instant.to_epoch_milli } unless filter.item_name

  unless filter.page_number.zero?
    result = result.slice(filter.page_number * filter.page_size, filter.page_size)
  end

  result
end

#remove(filter) ⇒ Object



54
55
56
57
58
59
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 54

def remove(filter)
  query_internal(filter) do |item_history, index|
    historic_item = item_history.delete_at(index)
    @data.delete(historic_item.name) if item_history.empty?
  end
end

#resetObject



30
31
32
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 30

def reset
  @data = Hash.new { |h, k| h[k] = [] }
end

#store(item, date = nil, state = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec/openhab/core/mocks/persistence_service.rb', line 34

def store(item, date = nil, state = nil)
  date = nil if date.is_a?(String) # alias overload
  state ||= item.state
  date ||= ZonedDateTime.now

  new_item = HistoricItem.new(date, state, item.name)

  item_history = @data[item.name]

  insert_index = item_history.bsearch_index do |i|
    i.timestamp.compare_to(date).positive?
  end

  return item_history << new_item unless insert_index

  return item_history[insert_index].state = state if item_history[insert_index].timestamp == date

  item_history.insert(insert_index, new_item)
end