Class: Traim::Controller
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
        show all
      
    
    - Defined in:
- lib/traim.rb
 
  Instance Attribute Summary collapse
  
  
    
      Instance Method Summary
      collapse
    
    
      
        - 
  
    
      #actions(name)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #attribute(name, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #create(options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #create_record(params)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #created  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #delete_record(id)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #destory(options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #execute(block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #find_record(id)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #has_many(name, options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #has_one(name, options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #headers(key = nil, value = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #initialize(model, actions, helper = nil, request = nil)  ⇒ Controller 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    
A new instance of Controller. 
 
- 
  
    
      #instance_record(id)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #is_collection?  ⇒ Boolean 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #logger  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #map_record  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #method_missing(m, *args, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #no_cotent  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #ok  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #record  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #record=(record)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #show(options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #update(options = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      #update_record(id, params)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
  Constructor Details
  
    
  
  
    #initialize(model, actions, helper = nil, request = nil)  ⇒ Controller 
  
  
  
  
    
Returns a new instance of Controller.
   
 
  
  
    | 
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355 | # File 'lib/traim.rb', line 339
def initialize(model, actions, helper = nil, request = nil)
  @model   = model 
  @request = request
  @headers = {}
  @actions = actions
  @fields  = []
  ok
  unless helper.nil?
    @helper = helper
    @helper.request = @request
    @helper.status  = @status
    @helper.fields  = @fields
    @helper.model   = @model
  end
end | 
 
  
 
  Dynamic Method Handling
  
    This class handles dynamic methods through the method_missing method
    
  
  
    
  
  
    #method_missing(m, *args, &block)  ⇒ Object 
  
  
  
  
    | 
416
417
418 | # File 'lib/traim.rb', line 416
def method_missing(m, *args, &block)
  record.send(m, *args, &block)
end | 
 
  
 
  
    Instance Attribute Details
    
      
      
      
  
  
    #fields  ⇒ Object 
  
  
  
  
    
Returns the value of attribute fields.
   
 
  
  
    | 
363
364
365 | # File 'lib/traim.rb', line 363
def fields
  @fields
end | 
 
    
      
      
      
  
  
    #helper  ⇒ Object 
  
  
  
  
    
Returns the value of attribute helper.
   
 
  
  
    | 
361
362
363 | # File 'lib/traim.rb', line 361
def helper
  @helper
end | 
 
    
      
      
      
  
  
    #id  ⇒ Object 
  
  
  
  
    
Returns the value of attribute id.
   
 
  
  
    | 
357
358
359 | # File 'lib/traim.rb', line 357
def id
  @id
end | 
 
    
      
      
      
  
  
    #model  ⇒ Object 
  
  
  
  
    
Returns the value of attribute model.
   
 
  
  
    | 
358
359
360 | # File 'lib/traim.rb', line 358
def model
  @model
end | 
 
    
      
      
      
  
  
    #params  ⇒ Object 
  
  
  
  
    
Returns the value of attribute params.
   
 
  
  
    | 
359
360
361 | # File 'lib/traim.rb', line 359
def params
  @params
end | 
 
    
      
      
      
  
  
    #request  ⇒ Object 
  
  
  
  
    
Returns the value of attribute request.
   
 
  
  
    | 
360
361
362 | # File 'lib/traim.rb', line 360
def request
  @request
end | 
 
    
      
      
      
  
  
    #status  ⇒ Object 
  
  
  
  
    
Returns the value of attribute status.
   
 
  
  
    | 
362
363
364 | # File 'lib/traim.rb', line 362
def status
  @status
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #actions(name)  ⇒ Object 
  
  
  
  
    | 
377 | # File 'lib/traim.rb', line 377
def actions(name); @actions[name] end | 
 
    
      
  
  
    #attribute(name, &block)  ⇒ Object 
  
  
  
  
    | 
379
380
381 | # File 'lib/traim.rb', line 379
def attribute(name, &block)
  fields << {name: name, type: 'attribute', block: block} 
end | 
 
    
      
  
  
    #create(options = {}, &block)  ⇒ Object 
  
  
  
  
    | 
373 | # File 'lib/traim.rb', line 373
def create(options = {}, &block);  @actions["POST"]  = {block: block, options: options} end | 
 
    
      
  
  
    #create_record(params)  ⇒ Object 
  
  
  
  
    | 
402
403
404
405
406 | # File 'lib/traim.rb', line 402
def create_record(params)
  resource = @model.new(params)
  resource.save
  resource
end | 
 
    
      
  
  
    #created  ⇒ Object 
  
  
  
  
    | 
369 | # File 'lib/traim.rb', line 369
def created;   @status = 201 end | 
 
    
      
  
  
    #delete_record(id)  ⇒ Object 
  
  
  
  
    | 
412
413
414 | # File 'lib/traim.rb', line 412
def delete_record(id)
  find_record(id).delete
end | 
 
    
      
  
  
    #destory(options = {}, &block)  ⇒ Object 
  
  
  
  
    | 
375 | # File 'lib/traim.rb', line 375
def destory(options = {}, &block); @actions["DELETE"] = {block: block, options: options} end | 
 
    
      
  
  
    #execute(block)  ⇒ Object 
  
  
  
  
    | 
438
439
440 | # File 'lib/traim.rb', line 438
def execute(block)
  @record = instance_eval(&block) 
end | 
 
    
      
  
  
    #find_record(id)  ⇒ Object 
  
  
  
  
    | 
408
409
410 | # File 'lib/traim.rb', line 408
def find_record(id)
  @model.find id
end | 
 
    
      
  
  
    #has_many(name, options = {}, &block)  ⇒ Object 
  
  
  
  
    | 
382
383
384 | # File 'lib/traim.rb', line 382
def has_many(name, options={}, &block)
  fields << {name: name, type: 'association', resource: options[:resource], block: block}
end | 
 
    
      
  
  
    #has_one(name, options = {}, &block)  ⇒ Object 
  
  
  
  
    | 
385
386
387 | # File 'lib/traim.rb', line 385
def has_one(name, options={}, &block)
  fields << {name: name, type: 'connection', resource: options[:resource], block: block}
end | 
 
    
      
  
  
    | 
394
395
396
397 | # File 'lib/traim.rb', line 394
def (key = nil, value = nil)
  return @headers if key.nil?
  @headers[key] = value 
end | 
 
    
      
  
  
    #instance_record(id)  ⇒ Object 
  
  
  
  
    | 
389
390
391
392 | # File 'lib/traim.rb', line 389
def instance_record(id)
  @id     = id
  @record = find_record(@id)
end | 
 
    
      
  
  
    #is_collection?  ⇒ Boolean 
  
  
  
  
    | 
427 | # File 'lib/traim.rb', line 427
def is_collection?; record.is_a?(Enumerable) end | 
 
    
      
  
  
    #logger  ⇒ Object 
  
  
  
  
    | 
365 | # File 'lib/traim.rb', line 365
def logger; Traim.logger  end | 
 
    
      
  
  
    #map_record  ⇒ Object 
  
  
  
  
    | 
429
430
431
432
433
434
435
436 | # File 'lib/traim.rb', line 429
def map_record
  @record.map do |item|
    new_controller = self.class.new(@model, @actions)
    new_controller.record = item 
    new_controller.fields = self.fields
    yield item, new_controller 
  end
end | 
 
    
      
  
  
    #no_cotent  ⇒ Object 
  
  
  
  
    | 
370 | # File 'lib/traim.rb', line 370
def no_cotent; @status = 204 end | 
 
    
      
  
  
    #ok  ⇒ Object 
  
  
  
  
  
    | 
368 | # File 'lib/traim.rb', line 368
def ok;        @status = 200 end | 
 
    
      
  
  
    #record  ⇒ Object 
  
  
  
  
    | 
400 | # File 'lib/traim.rb', line 400
def record; @record ||= find_record(id) end | 
 
    
      
  
  
    #record=(record)  ⇒ Object 
  
  
  
  
    | 
399 | # File 'lib/traim.rb', line 399
def record=(record); @record = record end | 
 
    
      
  
  
    #show(options = {}, &block)  ⇒ Object 
  
  
  
  
    | 
372 | # File 'lib/traim.rb', line 372
def show(options = {}, &block);    @actions["GET"]    = {block: block, options: options} end | 
 
    
      
  
  
    #update(options = {}, &block)  ⇒ Object 
  
  
  
  
    | 
374 | # File 'lib/traim.rb', line 374
def update(options = {}, &block);  @actions["PUT"]  = {block: block, options: options} end | 
 
    
      
  
  
    #update_record(id, params)  ⇒ Object 
  
  
  
  
    | 
420
421
422
423
424
425 | # File 'lib/traim.rb', line 420
def update_record(id, params)
  resource = find_record(id)
  resource.assign_attributes(params)
  resource.save
  resource 
end |