Class: SimpleFeed::Providers::Hash::Provider

Inherits:
Base::Provider show all
Includes:
Paginator
Defined in:
lib/simplefeed/providers/hash/provider.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Paginator

#order_events, #paginate_items

Constructor Details

#initialize(**opts) ⇒ Provider

Returns a new instance of Provider.



27
28
29
30
# File 'lib/simplefeed/providers/hash/provider.rb', line 27

def initialize(**opts)
  self.h = {}
  h.merge!(opts)
end

Instance Attribute Details

#hObject

Returns the value of attribute h.



19
20
21
# File 'lib/simplefeed/providers/hash/provider.rb', line 19

def h
  @h
end

Class Method Details

.from_yaml(file) ⇒ Object



23
24
25
# File 'lib/simplefeed/providers/hash/provider.rb', line 23

def self.from_yaml(file)
  self.new(YAML.load(File.read(file)))
end

Instance Method Details

#delete(user_ids:, value:, at: nil) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/simplefeed/providers/hash/provider.rb', line 39

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:, &block) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/simplefeed/providers/hash/provider.rb', line 48

def delete_if(user_ids:, &block)
  with_response_batched(user_ids) do |key|
    activity(key).each do |event|
      __delete(key, event) if yield(key.user_id, event)
    end
  end
end

#fetch(user_ids:, since: nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/simplefeed/providers/hash/provider.rb', line 64

def fetch(user_ids:, since: nil)
  with_response_batched(user_ids) do |key|
    if since == :unread
      result = activity(key).reject { |event| event.at < user_record(key).last_read.to_f }
      reset_last_read(user_ids: user_ids)
      result
    elsif since
      activity(key).reject { |event| event.at < since.to_f }
    else
      activity(key)
    end
  end
end

#last_read(user_ids:) ⇒ Object



106
107
108
109
110
# File 'lib/simplefeed/providers/hash/provider.rb', line 106

def last_read(user_ids:)
  with_response_batched(user_ids) do |key|
    user_record(key).last_read
  end
end

#paginate(user_ids:, page:, per_page: feed.per_page, with_total: false, peek: false) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/simplefeed/providers/hash/provider.rb', line 78

def paginate(user_ids:, page:, per_page: feed.per_page, with_total: false, peek: false)
  reset_last_read(user_ids: user_ids) unless peek
  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) ⇒ Object



87
88
89
90
91
92
# File 'lib/simplefeed/providers/hash/provider.rb', line 87

def reset_last_read(user_ids:, at: Time.now)
  with_response_batched(user_ids) do |key|
    user_record(key)[:last_read] = at
    at
  end
end

#store(user_ids:, value:, at: Time.now) ⇒ Object



32
33
34
35
36
37
# File 'lib/simplefeed/providers/hash/provider.rb', line 32

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:) ⇒ Object



94
95
96
97
98
# File 'lib/simplefeed/providers/hash/provider.rb', line 94

def total_count(user_ids:)
  with_response_batched(user_ids) do |key|
    activity(key).size
  end
end

#total_memory_bytesObject



112
113
114
115
116
117
118
119
# File 'lib/simplefeed/providers/hash/provider.rb', line 112

def total_memory_bytes
  if defined?(::Knj)
    analyzer = Knj::Memory_analyzer::Object_size_counter.new(self.h)
    analyzer.calculate_size
  else
    raise LoadError, 'Please run "gem install knjrbfw" to get accurate hash size'
  end
end

#total_usersObject



121
122
123
# File 'lib/simplefeed/providers/hash/provider.rb', line 121

def total_users
  self.h.size
end

#unread_count(user_ids:) ⇒ Object



100
101
102
103
104
# File 'lib/simplefeed/providers/hash/provider.rb', line 100

def unread_count(user_ids:)
  with_response_batched(user_ids) do |key|
    activity(key).count { |event| event.at > user_record(key).last_read.to_f }
  end
end

#wipe(user_ids:) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/simplefeed/providers/hash/provider.rb', line 56

def wipe(user_ids:)
  with_response_batched(user_ids) do |key|
    deleted = activity(key).size > 0
    wipe_user_record(key)
    deleted
  end
end