class Twoffein::Drinks

Attributes

all[R]

Public Class Methods

[](key)
Alias for: find
all() click to toggle source
# File lib/twoffein-client/drinks.rb, line 27
def all;    new.all;                 end
brands() click to toggle source
# File lib/twoffein-client/drinks.rb, line 30
def brands; all.map { |d| d.brand }; end
find(key) click to toggle source
# File lib/twoffein-client/drinks.rb, line 36
def find(key)
  key = key.to_sym
  all.each { |d| return d if d.key == key }
  nil
end
Also aliased as: []
get() click to toggle source
# File lib/twoffein-client/drinks.rb, line 32
def get
  HTTP.get("drinks", :drink => :all)
end
keys() click to toggle source
# File lib/twoffein-client/drinks.rb, line 29
def keys;   all.map { |d| d.key   }; end
names() click to toggle source
# File lib/twoffein-client/drinks.rb, line 28
def names;  all.map { |d| d.name  }; end
new(subset=nil) click to toggle source
# File lib/twoffein-client/drinks.rb, line 8
def initialize(subset=nil)
  unless subset
    @all ||= Drinks.get
    return @all.map! do |drink|
      name = drink[:drink]
      key = drink[:key].to_sym
      brand = (drink[:brand] == "0" ? false : true)
      Drink.new(name, key, brand)
    end
  end

  if subset.is_a? Array
    @all = subset
  else
    @all = [subset]
  end
end

Public Instance Methods

add(drink) click to toggle source
# File lib/twoffein-client/drinks.rb, line 51
def add(drink)
  raise ArgumentError, "drink has to be of type Drink" unless drink.is_a? Drink
  @all << drink
end
to_s() click to toggle source
# File lib/twoffein-client/drinks.rb, line 56
def to_s
  name_max_length = @all.map { |d| d.name.length }.max
  pre_key  = "  ("
  post_key = ")"

  # Calc max length of line
  length = lambda do |drink|
    key = drink.key
    [pre_key, key, post_key].map { |cont|
      cont.length
    }.push(name_max_length).reduce(&:+)
  end
  max_line = @all.map { |d| length.call(d) }.max

  # Header
  header = ["Drink".ljust(name_max_length), pre_key, "key", post_key].join
  line = "-"*max_line

  # Drinks
  drinks = @all.map { |d|
    name = d.name
    key = d.key
    [name.ljust(name_max_length), pre_key, key, post_key].join
  }.join("\n")

  # All
  [header, line, drinks].join("\n")
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source

Delegates method call to @all if it has defined this method

Calls superclass method
# File lib/twoffein-client/drinks.rb, line 88
def method_missing(method, *args, &block)
  if @all.respond_to? method
    @all.send(method, *args, &block)
  else
    super(method, *args, &block)
  end
end