Module: Finix::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/finix/utils.rb

Instance Method Summary collapse

Instance Method Details

#callable(callable_or_not) ⇒ Object



10
11
12
# File 'lib/finix/utils.rb', line 10

def callable(callable_or_not)
  callable_or_not.respond_to?(:call) ? callable_or_not : lambda { callable_or_not }
end

#demodulize(class_name_in_module) ⇒ Object



43
44
45
# File 'lib/finix/utils.rb', line 43

def demodulize(class_name_in_module)
  class_name_in_module.to_s.sub(/^.*::/, '')
end

#eval_class(slf, cls) ⇒ Object



3
4
5
6
7
8
# File 'lib/finix/utils.rb', line 3

def eval_class(slf, cls)
  mod = slf.class.name.sub(/::.*/, '') unless slf.kind_of? Class or slf.kind_of? Module
  mod = slf.name.sub(/::.*/, '') if mod.nil?
  name = demodulize cls.name
  self.instance_eval "#{mod}::#{name}"
end

#indifferent_read_access(base = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/finix/utils.rb', line 14

def indifferent_read_access(base = {})
  indifferent = Hash.new do |hash, key|
    hash[key.to_s] if key.is_a? Symbol
  end
  base.each_pair do |key, value|
    if value.is_a? Hash
      value = indifferent_read_access value
    elsif value.respond_to? :each
      if value.respond_to? :map!
        value.map! do |v|
          if v.is_a? Hash
            v = indifferent_read_access v
          end
          v
        end
      else
        value.map do |v|
          if v.is_a? Hash
            v = indifferent_read_access v
          end
          v
        end
      end
    end
    indifferent[key.to_s] = value
  end
  indifferent
end