Class: Corpshort::Backends::Memory
- Defined in:
- lib/corpshort/backends/memory.rb
Instance Attribute Summary collapse
-
#links ⇒ Object
readonly
Returns the value of attribute links.
-
#links_by_url ⇒ Object
readonly
Returns the value of attribute links_by_url.
Instance Method Summary collapse
- #delete_link(link) ⇒ Object
- #get_link(name) ⇒ Object
-
#initialize ⇒ Memory
constructor
A new instance of Memory.
- #list_links(token: nil, limit: 30) ⇒ Object
- #list_links_by_url(url) ⇒ Object
- #put_link(link, create_only: false) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize ⇒ Memory
Returns a new instance of Memory.
8 9 10 11 12 |
# File 'lib/corpshort/backends/memory.rb', line 8 def initialize() @lock = Mutex.new @links = {} @links_by_url = {} end |
Instance Attribute Details
#links ⇒ Object (readonly)
Returns the value of attribute links.
14 15 16 |
# File 'lib/corpshort/backends/memory.rb', line 14 def links @links end |
#links_by_url ⇒ Object (readonly)
Returns the value of attribute links_by_url.
14 15 16 |
# File 'lib/corpshort/backends/memory.rb', line 14 def links_by_url @links_by_url end |
Instance Method Details
#delete_link(link) ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/corpshort/backends/memory.rb', line 41 def delete_link(link) @lock.synchronize do name = link.is_a?(String) ? link : link.name data = @links[name] if @links[name] @links.delete name @links_by_url[data[:url]].delete name end end end |
#get_link(name) ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/corpshort/backends/memory.rb', line 32 def get_link(name) data = @links[name] if data && !data.empty? Link.new(data, backend: self) else nil end end |
#list_links(token: nil, limit: 30) ⇒ Object
56 57 58 |
# File 'lib/corpshort/backends/memory.rb', line 56 def list_links(token: nil, limit: 30) [@links.keys, nil] end |
#list_links_by_url(url) ⇒ Object
52 53 54 |
# File 'lib/corpshort/backends/memory.rb', line 52 def list_links_by_url(url) @links_by_url[url].keys end |
#put_link(link, create_only: false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/corpshort/backends/memory.rb', line 16 def put_link(link, create_only: false) @lock.synchronize do old_link = @links[link.name] if create_only && old_link raise ConflictError end old_url = old_link&.fetch(:url, nil) if old_link && link.url != old_url @links_by_url[old_url].delete(link.name) end @links[link.name] = link.to_h (@links_by_url[link.url] ||= {})[link.name] = true end nil end |