Method: Confstruct::HashWithStructAccess#method_missing

Defined in:
lib/confstruct/hash_with_struct_access.rb

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



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/confstruct/hash_with_struct_access.rb', line 134

def method_missing sym, *args, &block
  name = sym.to_s.chomp('=').to_sym
  result = nil
  
  if name.to_s =~ /^add_(.+)!$/
    name = $1.to_sym
    self.assign_property(name, []) unless self.has_key?(name)
    unless self[name].is_a?(Array)
      raise TypeError, "Cannot #add! to a #{self[name].class}"
    end
    if args.length > 0
      local_args = args.collect { |a| self.class.structurize a }
      result = self[name].push *local_args
    elsif block_given?
      result = HashWithStructAccess.new
      self[name].push result
    end
  elsif args.length == 1
    self.assign_property(name, args[0])
    result = self[name]
  elsif args.length > 1
    super(sym,*args,&block)
  else
    result = self[name]
    if result.nil? and block_given?
      self.assign_property(name, HashWithStructAccess.new)
      result = self[name]
    end
  end
  if block_given?
    eval_or_yield result, &block
  end
  result
end