class Bario::Bar

Bar encapsulate all the logic to create, list, find, delete and update the progress bars

Constants

DEFAULT_TOTAL

Attributes

bar[R]

Public Class Methods

all() click to toggle source
# File lib/bario/bar.rb, line 32
def all
  collection(Storage.find(root: true))
end
collection(bars) click to toggle source
# File lib/bario/bar.rb, line 47
def collection(bars)
  bars.map { |item| new(item) }
end
create(opts = {}) click to toggle source
# File lib/bario/bar.rb, line 36
def create(opts = {})
  opts[:total] ||= DEFAULT_TOTAL
  opts[:root] = true unless opts.key?(:root)

  bar = Storage.create(opts)

  parent = opts[:parent]
  parent&.children&.push(bar)
  new(bar)
end
find(id) click to toggle source
# File lib/bario/bar.rb, line 22
def find(id)
  bar = Storage[id]
  bar ? new(bar) : nil
end
get(id) click to toggle source
# File lib/bario/bar.rb, line 27
def get(id)
  bar = Storage[id]
  bar ? new(bar) : NullBar.new
end

Private Class Methods

new(bar) click to toggle source
# File lib/bario/bar.rb, line 52
def initialize(bar)
  @bar = bar
end

Public Instance Methods

add_bar(opts = {}) click to toggle source
# File lib/bario/bar.rb, line 85
def add_bar(opts = {})
  opts[:total] ||= DEFAULT_TOTAL
  opts[:parent] = bar
  opts[:root] = false
  Bar.create(opts)
end
bars() click to toggle source
# File lib/bario/bar.rb, line 92
def bars
  self.class.collection(bar.children)
end
created_at() click to toggle source
# File lib/bario/bar.rb, line 58
def created_at
  Time.at(bar.created_at.to_i).utc
end
current() click to toggle source
# File lib/bario/bar.rb, line 70
def current
  [
    [0, bar.current].max,
    total
  ].min
end
decrement(by = 1) click to toggle source
# File lib/bario/bar.rb, line 100
def decrement(by = 1)
  bar.decrement(:current, by)
end
delete() click to toggle source
# File lib/bario/bar.rb, line 104
def delete
  parent = bar.parent
  parent&.children&.delete(bar)
  bar.delete
end
increment(by = 1) click to toggle source
# File lib/bario/bar.rb, line 96
def increment(by = 1)
  bar.increment(:current, by)
end
percent() click to toggle source
# File lib/bario/bar.rb, line 66
def percent
  current / total.to_f * 100
end
total() click to toggle source
# File lib/bario/bar.rb, line 77
def total
  [0, bar.total.to_i].max
end
total=(val) click to toggle source
# File lib/bario/bar.rb, line 81
def total=(val)
  bar.total = val
end
updated_at() click to toggle source
# File lib/bario/bar.rb, line 62
def updated_at
  Time.at(bar.updated_at.to_i).utc
end