Class: OpenHAB::RSpec::Mocks::PersistenceService
- Inherits:
-
Object
- Object
- OpenHAB::RSpec::Mocks::PersistenceService
- Defined in:
- lib/openhab/rspec/mocks/persistence_service.rb
Defined Under Namespace
Modules: HistoricState Classes: HistoricItem
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Instance Method Summary collapse
-
#get_default_strategies ⇒ Object
rubocop:disable Naming/AccessorMethodName must match Java interface.
-
#get_item_info ⇒ Object
rubocop:disable Naming/AccessorMethodName must match Java interface.
-
#initialize ⇒ PersistenceService
constructor
A new instance of PersistenceService.
- #query(filter) ⇒ Object
- #remove(filter) ⇒ Object
- #reset ⇒ Object
- #store(item, date = nil, state = nil) ⇒ Object
Constructor Details
#initialize ⇒ PersistenceService
Returns a new instance of PersistenceService.
39 40 41 42 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 39 def initialize @id = "default" reset end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
37 38 39 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 37 def id @id end |
Instance Method Details
#get_default_strategies ⇒ Object
rubocop:disable Naming/AccessorMethodName must match Java interface
97 98 99 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 97 def get_default_strategies # rubocop:disable Naming/AccessorMethodName must match Java interface [org.openhab.core.persistence.strategy.PersistenceStrategy::Globals::CHANGE] end |
#get_item_info ⇒ Object
rubocop:disable Naming/AccessorMethodName must match Java interface
91 92 93 94 95 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 91 def get_item_info # rubocop:disable Naming/AccessorMethodName must match Java interface @data.to_set do |(n, entries)| [n, entries.length, entries.first., entries.last.] end end |
#query(filter) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 75 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
68 69 70 71 72 73 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 68 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 |
#reset ⇒ Object
44 45 46 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 44 def reset @data = Hash.new { |h, k| h[k] = [] } end |
#store(item, date = nil, state = nil) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/openhab/rspec/mocks/persistence_service.rb', line 48 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..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]. == date item_history.insert(insert_index, new_item) end |