Class: ProjectInfo::HashBuilder

Inherits:
BasicObject
Defined in:
lib/reap/projectinfo.rb

Overview

Build a hash from missing method calls.

Instance Method Summary collapse

Constructor Details

#initialize(blockstr = nil, &block) ⇒ HashBuilder

Returns a new instance of HashBuilder.



224
225
226
227
228
229
230
231
232
233
# File 'lib/reap/projectinfo.rb', line 224

def initialize( blockstr=nil, &block )
  @hash = {}
  @flag = {}
  raise "both string and block given" if blockstr and block_given?
  if blockstr
    instance_eval blockstr
  else
    instance_eval &block
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/reap/projectinfo.rb', line 237

def method_missing( sym, *args, &block )
  sym = sym.to_s.downcase

  if @hash.key?(sym)
    unless @flag[sym]
      @hash[sym] = [ @hash[sym] ]
      @flag[sym] = true
    end
    if block_given?
      @hash[sym] << self.__class__.new( &block ).to_h
    else
      @hash[sym] << args[0]
    end
  else
    if block_given?
      @hash[sym] = self.__class__.new( &block ).to_h
    else
      @hash[sym] = args[0]
    end
  end

end

Instance Method Details

#to_hObject



235
# File 'lib/reap/projectinfo.rb', line 235

def to_h ; @hash ; end