Module: Babaloa
- Defined in:
- lib/babaloa.rb,
lib/babaloa/config.rb,
lib/babaloa/version.rb
Defined Under Namespace
Classes: BabaloaError, Configuration
Constant Summary
collapse
- VERSION =
"0.1.1"
Class Method Summary
collapse
Class Method Details
.configuration ⇒ Object
7
8
9
|
# File 'lib/babaloa/config.rb', line 7
def configuration
@configuration ||= Configuration.new
end
|
3
4
5
|
# File 'lib/babaloa/config.rb', line 3
def configure
yield(configuration)
end
|
.except_by(header, **options) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/babaloa.rb', line 73
def except_by(, **options)
except = options[:except] || configuration.define(options[:name], :except) || configuration.default[:except]
return unless except
if except.is_a?(Array)
.reject! {|k| except.include?(k) }
elsif except.is_a?(Symbol)
.reject! {|k| except == k }
else
raise BabaloaError, "except option must be Array, Symbol"
end
end
|
.only_by(header, **options) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/babaloa.rb', line 58
def only_by(, **options)
only = options[:only] || configuration.define(options[:name], :only) || configuration.default[:only]
return unless only
if only.is_a?(Array)
.select! {|k| only.include?(k) }
elsif only.is_a?(Symbol)
.select! {|k| only == k }
else
raise BabaloaError, "only option must be Array, Symbol"
end
end
|
.order_by(data, header, **options) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/babaloa.rb', line 39
def order_by(data, , **options)
sort = options[:sort] || configuration.define(options[:name], :sort) || configuration.default[:sort]
return data unless sort
if sort.is_a?(Hash)
k, v = sort.first
k = .index(k.to_sym) if data.first.is_a?(Array)
data.sort_by! {|col| col[k] }
data.reverse! if v == :desc
elsif sort.is_a?(Symbol) || sort.is_a?(String)
sort = .index(sort.to_sym) if data.first.is_a?(Array)
data.sort_by! {|col| col[sort] }
else
raise BabaloaError, "sort option must be Hash, Symbol, String."
end
data
end
|
.to_csv(data, h = true, **options) ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/babaloa.rb', line 9
def to_csv(data, h = true, **options)
raise BabaloaError, "data must be Array" unless data.is_a?(Array)
raise BabaloaError, "content must be Array or Hash" unless data.empty? || data.first.is_a?(Array) || data.first.is_a?(Hash)
if h && !data.empty?
if data.first.is_a?(Hash)
= data.first.keys.map(&:to_sym)
= only_by(, **options)
= except_by(, **options)
else
= data.first.map(&:to_sym)
data = data.drop(1)
end
data = order_by(data, , **options)
end
CSV.generate do |csv|
csv << transrate_by(.dup, **options) if h &&
data.each{|res|
if res.is_a?(Hash)
raise BabaloaError, "Header required if content is Hash" unless h
csv << .map {|k| res[k] || res[k.to_s] }
else
csv << res
end
}
end
end
|
.transrate_by(header, **options) ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/babaloa.rb', line 88
def transrate_by(, **options)
t = options[:t] || configuration.define(options[:name], :t) || configuration.default[:t]
return unless t
if t.is_a?(Hash)
.map! {|k| t[k] || t[k.to_s] }.compact!
else
raise BabaloaError, "t option must be Hash"
end
end
|