Class: Mappum::DSL::Field

Inherits:
Mappet show all
Defined in:
lib/mappum/dsl.rb

Direct Known Subclasses

Context

Instance Method Summary collapse

Methods inherited from Mappet

#<<, #<=>, #>>, #mpun_definition

Constructor Details

#initialize(parent, name, clazz, placeholder = false, src_ref = nil) ⇒ Field

Returns a new instance of Field.



250
251
252
253
254
255
256
257
258
# File 'lib/mappum/dsl.rb', line 250

def initialize(parent, name, clazz, placeholder = false, src_ref = nil)
  @def ||=  Mappum::Field.new
  @def.parent = parent
  @def.name = name
  @def.clazz = clazz
  @def.is_root = false
  @def.is_placeholder = placeholder
  @def.src_ref = src_ref
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/mappum/dsl.rb', line 276

def method_missing(symbol, *args, &block)
  if @def.is_root
    if(symbol == :self)
      return Field.new(@def, nil, args[0], true)
    end
    return Field.new(@def, symbol, args[0], false, DSL.get_src_ref)
  end
  
  if symbol == :[]
    #empty [] is just indication that field is an array not function      
    if args.size == 0
      @def.enum_type = Array
      return self
    end
    #[n] indicates both mapping function and Hash
    if args.size == 1
      @def.enum_type = Hash
    end
  end
  #this functions also indicate enumerable -> element
  if symbol == :find or symbol == :detect or symbol == :select 
    @def.enum_type = Array
  end
  arguments = args.clone.collect do |a|
    ret = nil
    if a.kind_of? Symbol
      ret = ":'#{a}'"
    elsif a.kind_of? String
      ret = "'#{a}'"
    else
      ret = a
    end
  end
  unless block.nil?
    arguments << "&mappum_block"
    @def.block = block
  end
  if @def.func.nil?
    @def.func =  "self.#{symbol}(#{arguments.join(", ")})"
  else
    @def.func += ".#{symbol}(#{arguments.join(", ")})"
  end
  return self
end

Instance Method Details

#id(*attr) ⇒ Object



272
273
274
# File 'lib/mappum/dsl.rb', line 272

def id(*attr)
  method_missing(:id, *attr)
end

#inspectObject



266
267
268
# File 'lib/mappum/dsl.rb', line 266

def inspect
  method_missing(:inspect)
end

#methodsObject



263
264
265
# File 'lib/mappum/dsl.rb', line 263

def methods
  method_missing(:methods)
end

#to_sObject



260
261
262
# File 'lib/mappum/dsl.rb', line 260

def to_s
  method_missing(:to_s)
end

#type(*attr) ⇒ Object



269
270
271
# File 'lib/mappum/dsl.rb', line 269

def type(*attr)
  method_missing(:type, *attr)
end