Class: Map::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/map/struct.rb

Constant Summary collapse

Keys =
lambda{|*keys| keys.flatten!; keys.compact!; keys.map!{|key| key.to_s}}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(map) ⇒ Struct

Returns a new instance of Struct.



8
9
10
# File 'lib/map/struct.rb', line 8

def initialize(map)
  @map = map
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/map/struct.rb', line 12

def method_missing(method, *args, &block)
  method = method.to_s
  case method
    when /=$/
      key = method.chomp('=')
      value = args.shift
      @map[key] = value
    when /\?$/
      key = method.chomp('?')
      value = @map.has?( key )
    else
      key = method
      raise(IndexError, key) unless @map.has_key?(key)
      value = @map[key]
  end
  value.is_a?(Map) ? value.struct : value
end

Instance Attribute Details

#mapObject (readonly)

Returns the value of attribute map.



6
7
8
# File 'lib/map/struct.rb', line 6

def map
  @map
end