Class: Datasource::Attributes::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/datasource/attributes/loader.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

._load_procObject

Returns the value of attribute _load_proc.



6
7
8
# File 'lib/datasource/attributes/loader.rb', line 6

def _load_proc
  @_load_proc
end

._optionsObject

Returns the value of attribute _options.



5
6
7
# File 'lib/datasource/attributes/loader.rb', line 5

def _options
  @_options
end

Class Method Details

.default_valueObject



16
17
18
# File 'lib/datasource/attributes/loader.rb', line 16

def default_value
  self._options[:default]
end

.inherited(base) ⇒ Object



8
9
10
# File 'lib/datasource/attributes/loader.rb', line 8

def inherited(base)
  base._options = (_options || {}).dup
end

.load(*args, &block) ⇒ 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
# File 'lib/datasource/attributes/loader.rb', line 20

def load(*args, &block)
  args = args.slice(0, _load_proc.arity) if _load_proc.arity >= 0
  results = _load_proc.call(*args, &block)

  if _options[:group_by]
    results = Array(results)
    send_args = if results.first && results.first.kind_of?(Hash)
      [:[]]
    else
      []
    end

    if _options[:one]
      results.inject({}) do |hash, r|
        key = r.send(*send_args, _options[:group_by])
        hash[key] = r
        hash
      end
    else
      results.inject({}) do |hash, r|
        key = r.send(*send_args, _options[:group_by])
        (hash[key] ||= []).push(r)
        hash
      end
    end
  elsif _options[:array_to_hash]
    Array(results).inject({}) do |hash, r|
      hash[r[0]] = r[1]
      hash
    end
  else
    results
  end
end

.options(hash) ⇒ Object



12
13
14
# File 'lib/datasource/attributes/loader.rb', line 12

def options(hash)
  self._options.merge!(hash)
end