Module: Indicator::DataMapper
- Included in:
- Base
- Defined in:
- lib/indicator/data_mapper.rb
Defined Under Namespace
Classes: InvalidMapping, Map, SourceMustBeEnumerable
Instance Method Summary
collapse
Instance Method Details
#default_getter ⇒ Object
12
13
14
|
# File 'lib/indicator/data_mapper.rb', line 12
def default_getter
@default_getter || :close
end
|
#default_getter=(v) ⇒ Object
16
17
18
|
# File 'lib/indicator/data_mapper.rb', line 16
def default_getter= v
@default_getter = v
end
|
#map(ds, getter = default_getter) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/indicator/data_mapper.rb', line 20
def map ds, getter=default_getter
return nil unless ds
if ds.is_a? Map
source = ds.source
mapping = ds.getter
else
source = ds
end
raise SourceMustBeEnumerable unless source.is_a? Enumerable
raise ArgumentError unless getter
return nil unless source.length > 0
element = source.first
return source if element.respond_to?(:to_f) and mapping.nil?
mapping ||= getter
mapping_sym = mapping.to_sym rescue nil
map_proc =
if mapping.respond_to? :call
mapping
else
if not mapping_sym.nil? and element.respond_to?(mapping_sym)
mapping_sym.to_proc
elsif element.respond_to?(:[])
->(e) { e[mapping] }
end
end
raise InvalidMapping unless map_proc
source.collect { |e| map_proc.call(e) }
end
|