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


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
55
56
57
58
59
# File 'lib/simplefeed/providers/hash/provider.rb', line 48

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

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


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simplefeed/providers/hash/provider.rb', line 69

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


111
112
113
114
115
# File 'lib/simplefeed/providers/hash/provider.rb', line 111

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


83
84
85
86
87
88
89
90
# File 'lib/simplefeed/providers/hash/provider.rb', line 83

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


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

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


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

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

#total_memory_bytesObject


117
118
119
120
121
122
123
124
# File 'lib/simplefeed/providers/hash/provider.rb', line 117

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


126
127
128
# File 'lib/simplefeed/providers/hash/provider.rb', line 126

def total_users
  self.h.size
end

#unread_count(user_ids:) ⇒ Object


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

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


61
62
63
64
65
66
67
# File 'lib/simplefeed/providers/hash/provider.rb', line 61

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