Class: Ruco::OptionAccessor

Inherits:
Object show all
Defined in:
lib/ruco/option_accessor.rb

Overview

Can be used like a hash, but also allows .key access

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapped = {}) ⇒ OptionAccessor

Returns a new instance of OptionAccessor.



7
8
9
# File 'lib/ruco/option_accessor.rb', line 7

def initialize(wrapped={})
  @wrapped = wrapped
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



19
20
21
22
23
# File 'lib/ruco/option_accessor.rb', line 19

def method_missing(method, *args)
  base = method.to_s.sub('=','').to_sym
  raise if args.size != 1
  @wrapped[base] = args.first
end

Instance Attribute Details

#wrappedObject (readonly)

Returns the value of attribute wrapped.



4
5
6
# File 'lib/ruco/option_accessor.rb', line 4

def wrapped
  @wrapped
end

Instance Method Details

#nested(key) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/ruco/option_accessor.rb', line 11

def nested(key)
  Hash[wrapped.map do |k,v|
    if k.to_s =~ /^#{key}_(.*)$/
      [$1.to_sym,v]
    end
  end.compact]
end