Class: CamelAndSankeStruct

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/easy_struct.rb

Overview

A small hack written by David Roy to make OpenStruct not care about calling a object via snake_case when it is camelCase

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



8
9
10
11
12
13
14
15
16
# File 'lib/easy_struct.rb', line 8

def method_missing(method, *args, &blk)
  (/(?<name>.+?)(?<setter>=)?$/ =~ method.to_s)
  property = name.camelcase(:lower).to_sym
  if @table.has_key?(property)
    setter ? @table[property] = args.first : @table[property]
  else
    super
  end
end