Module: Lab42::OpenMap::Implementation

Included in:
Lab42::OpenMap
Defined in:
lib/lab42/open_map/implementation.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lab42/open_map/implementation.rb', line 43

def method_missing(name, *args)
  assignment = name[-1] == "="
  name = name[0..-2].to_sym if assignment
  super unless has_key?(name)
  if assignment
    raise ArgumentError, "assignment needs a value" if args.empty?
    self[name] = args.first
  else
    self[name]
  end
end

Instance Method Details

#[]=(key, value) ⇒ Object

Raises:

  • (ArgumentError)


5
6
7
8
# File 'lib/lab42/open_map/implementation.rb', line 5

def []=(key, value)
  raise ArgumentError, "#{key.inspect} is not a symbol" unless Symbol === key
  @hash[key] = value
end

#merge(**kwds) ⇒ Object



10
11
12
13
# File 'lib/lab42/open_map/implementation.rb', line 10

def merge(**kwds)
  _check_symbolic_keys!(kwds)
  self.class.new(**@hash.merge(kwds))
end

#sans(*keys) ⇒ Object



15
16
17
# File 'lib/lab42/open_map/implementation.rb', line 15

def sans(*keys)
  self.class.new(without(*keys))
end

#to_hObject



24
25
26
# File 'lib/lab42/open_map/implementation.rb', line 24

def to_h
  @hash.clone
end

#update(**kwds) ⇒ Object



19
20
21
22
# File 'lib/lab42/open_map/implementation.rb', line 19

def update(**kwds)
  _check_symbolic_keys!(kwds)
  @hash.update(kwds)
end

#without(*keys) ⇒ Object



28
29
30
31
32
33
# File 'lib/lab42/open_map/implementation.rb', line 28

def without(*keys)
  ( self.keys - keys.flatten )
    .inject( {} ) do |r, k|
      r.update(k => self[k])
    end
end