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.



25
26
27
28
# File 'lib/simplefeed/providers/hash/provider.rb', line 25

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

Instance Attribute Details

#hObject

Returns the value of attribute h.



17
18
19
# File 'lib/simplefeed/providers/hash/provider.rb', line 17

def h
  @h
end

Class Method Details

.from_yaml(file) ⇒ Object



21
22
23
# File 'lib/simplefeed/providers/hash/provider.rb', line 21

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

Instance Method Details

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



37
38
39
40
41
42
43
44
# 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:) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# 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.user_id)
        __delete(key, event)
        event
      end
    end.compact
  end
end

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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_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:) ⇒ Object



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

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



95
96
97
98
99
100
# 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_record(key)[:last_read] = at
    at
  end
end

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



30
31
32
33
34
35
# 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:) ⇒ Object



102
103
104
105
106
# 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_bytesObject



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

def total_memory_bytes
  ObjectSpace.memsize_of(h)
end

#total_usersObject



124
125
126
# File 'lib/simplefeed/providers/hash/provider.rb', line 124

def total_users
  h.size
end

#unread_count(user_ids:) ⇒ Object



108
109
110
111
112
# 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_record(key).last_read.to_f }
  end
end

#wipe(user_ids:) ⇒ Object



57
58
59
60
61
62
63
# 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