class SimpleFeed::Providers::Hash::Provider

Attributes

h[RW]

Public Class Methods

from_yaml(file) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 21
def self.from_yaml(file)
  new(YAML.parse(File.read(file)))
end
new(opts) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 25
def initialize(opts)
  self.h = {}
  h.merge!(opts)
end

Public Instance Methods

delete(user_ids:, value:, at: nil) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 37
def delete(user_ids:, value:, at: nil)
  event = create_event(value, at)
  with_response_batched(user_ids) do |key|
    changed_activity_size?(key) do
      __delete(key, event)
    end
  end
end
delete_if(user_ids:) { |event, consumer| ... } click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 46
def delete_if(user_ids:)
  with_response_batched(user_ids) do |key|
    activity(key).map do |event|
      if yield(event, key.consumer)
        __delete(key, event)
        event
      end
    end.compact
  end
end
fetch(user_ids:, since: nil, reset_last_read: false) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 80
def fetch(user_ids:, since: nil, reset_last_read: false)
  response = with_response_batched(user_ids) do |key|
    if since == :unread
      activity(key).reject { |event| event.at < user_meta_record(key).last_read.to_f }
    elsif since
      activity(key).reject { |event| event.at < since.to_f }
    else
      activity(key)
    end
  end
  reset_last_read_value(user_ids: user_ids, at: reset_last_read) if reset_last_read

  response
end
last_read(user_ids:) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 114
def last_read(user_ids:)
  with_response_batched(user_ids) do |key|
    user_meta_record(key).last_read
  end
end
paginate(user_ids:, page:, per_page: feed.per_page, with_total: false, reset_last_read: false) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 65
def paginate(user_ids:,
             page:,
             per_page: feed.per_page,
             with_total: false,
             reset_last_read: false)

  reset_last_read_value(user_ids: user_ids, at: reset_last_read) if reset_last_read

  with_response_batched(user_ids) do |key|
    activity = activity(key)
    result = page && page > 0 ? activity[((page - 1) * per_page)...(page * per_page)] : activity
    with_total ? { events: result, total_count: activity.length } : result
  end
end
reset_last_read(user_ids:, at: Time.now) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 95
def reset_last_read(user_ids:, at: Time.now)
  with_response_batched(user_ids) do |key|
    user_meta_record(key)[:last_read] = at
    at
  end
end
store(user_ids:, value:, at: Time.now) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 30
def store(user_ids:, value:, at: Time.now)
  event = create_event(value, at)
  with_response_batched(user_ids) do |key|
    add_event(event, key)
  end
end
total_count(user_ids:) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 102
def total_count(user_ids:)
  with_response_batched(user_ids) do |key|
    activity(key).size
  end
end
total_memory_bytes() click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 120
def total_memory_bytes
  ObjectSpace.memsize_of(h)
end
total_users() click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 124
def total_users
  h.size / 2
end
unread_count(user_ids:) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 108
def unread_count(user_ids:)
  with_response_batched(user_ids) do |key|
    activity(key).count { |event| event.at > user_meta_record(key).last_read.to_f }
  end
end
wipe(user_ids:) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 57
def wipe(user_ids:)
  with_response_batched(user_ids) do |key|
    deleted = !activity(key).empty?
    wipe_user_record(key)
    deleted
  end
end

Private Instance Methods

__delete(key, event) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 188
def __delete(key, event)
  user_data_record(key)[:activity].delete(event)
end
__last_read(key, _value = nil) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 184
def __last_read(key, _value = nil)
  user_meta_record(key)[:last_read]
end
activity(key, event = nil) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 166
def activity(key, event = nil)
  user_data_record(key)[:activity] << event if event
  user_data_record(key)[:activity].to_a
end
add_event(event, key) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 171
def add_event(event, key)
  uas = user_data_record(key)[:activity]
  if uas.include?(event)
    false
  else
    uas << event.dup
    if uas.size > feed.max_size
      uas.delete(uas.to_a.last)
    end
    true
  end
end
changed_activity_size?(key) { |key, ua| ... } click to toggle source
Methods below operate on a single user only
# File lib/simplefeed/providers/hash/provider.rb, line 134
def changed_activity_size?(key)
  ua          = activity(key)
  size_before = ua.size
  yield(key, ua)
  size_after = activity(key).size
  (size_before > size_after)
end
create_data_record() click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 148
def create_data_record
  Hashie::Mash.new(
    { activity: SortedSet.new }
  )
end
create_event(*args, **opts) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 192
def create_event(*args, **opts)
  ::SimpleFeed::Event.new(*args, **opts)
end
create_meta_record() click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 142
def create_meta_record
  Hashie::Mash.new(
    { last_read: 0 }
  )
end
user_data_record(key) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 154
def user_data_record(key)
  h[key.data] ||= create_data_record
end
user_meta_record(key) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 158
def user_meta_record(key)
  h[key.meta] ||= create_meta_record
end
wipe_user_record(key) click to toggle source
# File lib/simplefeed/providers/hash/provider.rb, line 162
def wipe_user_record(key)
  h[key.data] = create_data_record
end