Module: Controll::Helper::HashAccess::ClassMethods

Defined in:
lib/controll/helper/hash_access.rb

Instance Method Summary collapse

Instance Method Details

#hash_access_methods(*args) ⇒ Object Also known as: hash_access_method

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/controll/helper/hash_access.rb', line 6

def hash_access_methods *args
  options = args.extract_options!
  hash_name = options[:hash]
  names = args

  raise ArgumentError, "Must take a :hash option indicating the hash name to access" unless hash_name
  raise ArgumentError, "Must take one or more names of methods to create" if names.blank? 

  names.each do |name|
    define_method name do            
      unless instance_variable_get("@#{name}")
        instance_variable_set "@#{name}", (send(hash_name)[name.to_sym]) || options[:default]
      end
    end
  end
end