Module: Wukong::DynamicGet

Included in:
Processor::Bin, Processor::Extract, Processor::Group, Processor::Sort
Defined in:
lib/wukong/widget/utils.rb

Overview

:nodoc:

This code is gross and nasty.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object

:nodoc:



9
10
11
# File 'lib/wukong/widget/utils.rb', line 9

def self.included klass
  klass.send(:field, :separator, String, :default => "\t", :doc => "The default separator between fields on a single line")
end

Instance Method Details

#get(field, obj) ⇒ Object

:nodoc:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wukong/widget/utils.rb', line 14

def get field, obj
  return obj unless field
  case
  when field.to_s.to_i > 0 && obj.is_a?(String)
    obj.split(separator)[field.to_s.to_i - 1]
  when field.to_s.to_i > 0
    obj[field.to_s.to_i - 1]
  when field.to_s.to_i == 0 && obj.is_a?(String) && obj =~ /^\s*\{/
    begin
      get_nested(field, MultiJson.load(obj))
    rescue MultiJson::DecodeError => e
    end
  when field.to_s.to_i == 0 && (!field.to_s.include?('.')) && obj.respond_to?(field.to_s)
    obj.send(field.to_s)
  when field.to_s.to_i == 0 && obj.respond_to?(:[])
    get_nested(field, obj)
  else obj
  end
end

#get_nested(fields, obj) ⇒ Object

:nodoc:



35
36
37
38
39
40
41
42
43
# File 'lib/wukong/widget/utils.rb', line 35

def get_nested fields, obj
  parts = fields.to_s.split('.')
  field = parts.shift
  return unless field
  if slice = obj[field]
    return slice if parts.empty?
    get_nested(parts.join('.'), slice)
  end
end