Class: Traim::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/traim.rb

Direct Known Subclasses

ArController

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#fieldsObject

Returns the value of attribute fields.



363
364
365
# File 'lib/traim.rb', line 363

def fields
  @fields
end

#helperObject

Returns the value of attribute helper.



361
362
363
# File 'lib/traim.rb', line 361

def helper
  @helper
end

#idObject

Returns the value of attribute id.



357
358
359
# File 'lib/traim.rb', line 357

def id
  @id
end

#modelObject

Returns the value of attribute model.



358
359
360
# File 'lib/traim.rb', line 358

def model
  @model
end

#paramsObject

Returns the value of attribute params.



359
360
361
# File 'lib/traim.rb', line 359

def params
  @params
end

#requestObject

Returns the value of attribute request.



360
361
362
# File 'lib/traim.rb', line 360

def request
  @request
end

#statusObject

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

#createdObject



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

#headers(key = nil, value = nil) ⇒ Object



394
395
396
397
# File 'lib/traim.rb', line 394

def headers(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

Returns:

  • (Boolean)


427
# File 'lib/traim.rb', line 427

def is_collection?; record.is_a?(Enumerable) end

#loggerObject



365
# File 'lib/traim.rb', line 365

def logger; Traim.logger  end

#map_recordObject



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_cotentObject



370
# File 'lib/traim.rb', line 370

def no_cotent; @status = 204 end

#okObject

status code sytax suger



368
# File 'lib/traim.rb', line 368

def ok;        @status = 200 end

#recordObject



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