Class: Twoffein::Drinks
- Inherits:
-
Object
show all
- Defined in:
- lib/twoffein-client/drinks.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(subset = nil) ⇒ Drinks
Returns a new instance of Drinks.
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# 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
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Delegates method call to @all if it has defined this method
88
89
90
91
92
93
94
|
# 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
|
Instance Attribute Details
#all ⇒ Object
Returns the value of attribute all.
6
7
8
|
# File 'lib/twoffein-client/drinks.rb', line 6
def all
@all
end
|
Class Method Details
.all ⇒ Object
27
|
# File 'lib/twoffein-client/drinks.rb', line 27
def all; new.all; end
|
.brands ⇒ Object
30
|
# File 'lib/twoffein-client/drinks.rb', line 30
def brands; all.map { |d| d.brand }; end
|
.find(key) ⇒ Object
Also known as:
[]
36
37
38
39
40
|
# 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
|
.get ⇒ Object
32
33
34
|
# File 'lib/twoffein-client/drinks.rb', line 32
def get
HTTP.get("drinks", :drink => :all)
end
|
.keys ⇒ Object
29
|
# File 'lib/twoffein-client/drinks.rb', line 29
def keys; all.map { |d| d.key }; end
|
.names ⇒ Object
28
|
# File 'lib/twoffein-client/drinks.rb', line 28
def names; all.map { |d| d.name }; end
|
.search(search) ⇒ Object
43
44
45
46
47
48
|
# File 'lib/twoffein-client/drinks.rb', line 43
def search search
search = /#{search}/i unless search.is_a? Regexp
selected = all.select { |drink| drink.to_s =~ search }
new(selected)
end
|
Instance Method Details
#add(drink) ⇒ Object
51
52
53
54
|
# 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 ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# 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 = ")"
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
= ["Drink".ljust(name_max_length), pre_key, "key", post_key].join
line = "-"*max_line
drinks = @all.map { |d|
name = d.name
key = d.key
[name.ljust(name_max_length), pre_key, key, post_key].join
}.join("\n")
[, line, drinks].join("\n")
end
|