Module: Hash::Slop

Included in:
LocalConf
Defined in:
lib/vex/base/hash/slop.rb

Defined Under Namespace

Modules: Etest

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object (private)



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/vex/base/hash/slop.rb', line 27

def method_missing(sym, *args, &block)
  return super if block_given?

  if args.length == 1 && sym.to_s =~ /^(.*)=$/
    return self[lookup_sloppy_key($1)] = args.first
  elsif args.length == 0
    if sym.to_s =~ /^(.*)\?$/
      # Return false if the entry does not exist.
      # Return true if the entry exists and evaluates to false
      # Return the entry otherwise.
      key = lookup_sloppy_key($1)
      return false if !key?(key)
      return (self[key] || true).slop!
    else
      # fetch the entry, if it exists, or raise an IndexError
      return fetch(lookup_sloppy_key(sym)).slop!
    end
  end
  
  super
rescue IndexError
  super
end

Instance Method Details

#respond_to?(sym) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
# File 'lib/vex/base/hash/slop.rb', line 53

def respond_to?(sym)
  super || case sym.to_s
  when /^(.*)[=\?]$/
    true
  else
    key? lookup_sloppy_key(sym)
  end
end