Class: Sticky::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/sticky/store.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Sticky::Config.new) ⇒ Store

Returns a new instance of Store.



8
9
10
11
# File 'lib/sticky/store.rb', line 8

def initialize(config = Sticky::Config.new)
  klass = Object.const_get(config.fetch(:store))
  @store = klass.new(config.fetch(:sticky_path))
end

Instance Attribute Details

#storeObject (readonly)

Returns the value of attribute store.



6
7
8
# File 'lib/sticky/store.rb', line 6

def store
  @store
end

Instance Method Details

#delete!(tag) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/sticky/store.rb', line 27

def delete!(tag)
  if tag
    store.transaction do
      store[:sticky_notes] || []
      store[:sticky_notes].reject! { |sticky| sticky.tag == tag }
    end
  end
end

#fetch(tag = nil, date = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sticky/store.rb', line 13

def fetch(tag = nil, date = nil)
  # FIXME: this is ugly, refactor this
  store.transaction do
    store[:sticky_notes] || []
    store[:sticky_notes].each do |sticky|
      if block_given? && ((tag.nil? && date.nil?) \
          || tag == sticky.tag \
          || date&.to_date == sticky.timestamp&.to_date)
        yield sticky
      end
    end
  end
end

#save!(sticky) ⇒ Object



36
37
38
39
40
41
# File 'lib/sticky/store.rb', line 36

def save!(sticky)
  store.transaction do
    store[:sticky_notes] ||= []
    store[:sticky_notes] << sticky
  end
end