Class: HashStruct

Inherits:
Hash
  • Object
show all
Defined in:
lib/hashstruct.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ HashStruct

Returns a new instance of HashStruct.



17
18
19
20
# File 'lib/hashstruct.rb', line 17

def initialize(args={})
  super()
  args.each { |key, value| self[key] = value }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/hashstruct.rb', line 31

def method_missing(method_id, *args)
  method_name = method_id.to_s
  if method_name =~ /=$/
    raise ArgumentError, "wrong number of arguments for method #{method_name.inspect} (#{args.length} for 1)", caller(1) if args.length != 1
    raise TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
    self[method_name.chop] = args.first
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 0)", caller(1) if args.length != 0
    self[method_id]
  end
end

Class Method Details

.make(obj) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/hashstruct.rb', line 6

def self.make(obj)
  case obj
  when Hash
    new(obj.transform_values { |v| make(v) })
  when Array
    obj.map { |v| make(v) }
  else
    obj
  end
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
25
# File 'lib/hashstruct.rb', line 22

def [](key)
  key = _make_key(key)
  has_key?(key) ? fetch(key) : nil
end

#[]=(key, value) ⇒ Object



27
28
29
# File 'lib/hashstruct.rb', line 27

def []=(key, value)
  store(_make_key(key), _convert_object(value))
end

#_convert_object(obj) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hashstruct.rb', line 47

def _convert_object(obj)
  case obj
  when String
    case obj.strip
    # URI
    when %r{^(ftp|http|https|mailto):}
      URI.parse(obj) rescue obj
    # integer
    when %r{^-?[1-9]\d*$}
      obj.to_i
    # hex integer
    when %r{^0x[0-9a-f]+$}i
      obj.hex
    # float
    when %r{^-?\d+\.\d+$}
      obj.to_f
    # percent
    when %r{^-?\d+(\.\d+)?%$}
      obj.to_f / 100
    # rational
    when %r{^(\d+)/(\d+)$}
      Rational($1.to_i, $2.to_i)
    # date
    when %r{^\d{4}-\d{2}-\d{2}},      # 2010-06-06
         %r{^\d{1,2}/\d{1,2}/\d{4}},  # 06/06/2010
         %r{^\d{4}/\d{1,2}/\d{1,2}}   # 2010/06/06
      Date.parse(obj)
    # boolean true
    when 'true', 'yes', 'on'
      true
    # boolean false
    when 'false', 'no', 'off'
      false
    # nil or empty string
    when nil, ''
      nil
    else
      obj
    end
  when Array
    obj.map { |o| _convert_object(o) }
  when Hash
    HashStruct.new(obj)
  when HashStruct
    obj
  else
    obj
  end
end

#_make_key(obj) ⇒ Object



43
44
45
# File 'lib/hashstruct.rb', line 43

def _make_key(obj)
  obj.to_s.downcase.gsub(/[^\w]/, '_').to_sym
end