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

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

Defined Under Namespace

Modules: PersistedState Classes: HistoricItem

Constant Summary collapse

OPERATOR_TO_SYMBOL =
{
  EQ: :==,
  NEQ: :!=,
  GT: :>,
  LT: :<,
  GTE: :>=,
  LTE: :<=
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePersistenceService

Returns a new instance of PersistenceService.



49
50
51
52
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 49

def initialize
  @id = "default"
  reset
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



47
48
49
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 47

def id
  @id
end

Instance Method Details

#get_default_strategiesObject

rubocop:disable Naming/AccessorMethodName must match Java interface



112
113
114
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 112

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



106
107
108
109
110
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 106

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

#query(filter) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 90

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!(&:timestamp) unless filter.item_name

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

  result
end

#remove(filter) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 82

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
  true
end

#resetObject



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

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

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



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

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

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

  item_history = @data[item_alias]

  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