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_getterObject



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

  # ds can be supplied as an array of [source, mapping] or simply
  # the source
  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 straight away if the data source is empty. The
  # downstream ta-lib functions can handle a nil argument.
  return nil unless source.length > 0

  # No need to go any further if no mapping was specified and
  # the specified data source responds to 'to_f'.
  element = source.first
  return source if element.respond_to?(:to_f) and mapping.nil?

  mapping ||= getter
  mapping_sym = mapping.to_sym rescue nil

  # Figure out how to use the mapping value.
  # It can either be a directly callable mapping, a function name
  # or a hash index.
  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

  # Returned the mapped data
  source.collect { |e| map_proc.call(e) }
end