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.



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

def initialize
  @id = "default"
  reset
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

Instance Method Details

#get_default_strategiesObject

rubocop:disable Naming/AccessorMethodName must match Java interface



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

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



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

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



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

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



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

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



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

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

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



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

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