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.



177
178
179
180
181
182
183
184
185
186
# File 'lib/reap/projectinfo.rb', line 177

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



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/reap/projectinfo.rb', line 190

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



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

def to_h ; @hash ; end