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
-
#fields ⇒ 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
|
# File 'lib/traim.rb', line 339
def initialize(model, actions, helper = nil, request = nil)
@model = model
@request = request
@headers = {}
@actions = actions
ok
unless helper.nil?
@helper = helper
@helper.request = @request
@helper.status = @status
@helper.model = @model
end
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *args, &block) ⇒ Object
414
415
416
|
# File 'lib/traim.rb', line 414
def method_missing(m, *args, &block)
record.send(m, *args, &block)
end
|
Instance Attribute Details
#helper ⇒ Object
Returns the value of attribute helper.
359
360
361
|
# File 'lib/traim.rb', line 359
def helper
@helper
end
|
#id ⇒ Object
Returns the value of attribute id.
355
356
357
|
# File 'lib/traim.rb', line 355
def id
@id
end
|
#model ⇒ Object
Returns the value of attribute model.
356
357
358
|
# File 'lib/traim.rb', line 356
def model
@model
end
|
#params ⇒ Object
Returns the value of attribute params.
357
358
359
|
# File 'lib/traim.rb', line 357
def params
@params
end
|
#request ⇒ Object
Returns the value of attribute request.
358
359
360
|
# File 'lib/traim.rb', line 358
def request
@request
end
|
#status ⇒ Object
Returns the value of attribute status.
360
361
362
|
# File 'lib/traim.rb', line 360
def status
@status
end
|
Instance Method Details
#actions(name) ⇒ Object
374
|
# File 'lib/traim.rb', line 374
def actions(name); @actions[name] end
|
#attribute(name, &block) ⇒ Object
377
378
379
|
# File 'lib/traim.rb', line 377
def attribute(name, &block)
fields << {name: name, type: 'attribute', block: block}
end
|
#create(options = {}, &block) ⇒ Object
370
|
# File 'lib/traim.rb', line 370
def create(options = {}, &block); @actions["POST"] = {block: block, options: options} end
|
#create_record(params) ⇒ Object
400
401
402
403
404
|
# File 'lib/traim.rb', line 400
def create_record(params)
resource = @model.new(params)
resource.save
resource
end
|
#created ⇒ Object
366
|
# File 'lib/traim.rb', line 366
def created; @status = 201 end
|
#delete_record(id) ⇒ Object
410
411
412
|
# File 'lib/traim.rb', line 410
def delete_record(id)
find_record(id).delete
end
|
#destory(options = {}, &block) ⇒ Object
372
|
# File 'lib/traim.rb', line 372
def destory(options = {}, &block); @actions["DELETE"] = {block: block, options: options} end
|
#execute(block) ⇒ Object
435
436
437
|
# File 'lib/traim.rb', line 435
def execute(block)
@record = instance_eval(&block)
end
|
#fields ⇒ Object
376
|
# File 'lib/traim.rb', line 376
def fields; @fields ||= [] end
|
#find_record(id) ⇒ Object
406
407
408
|
# File 'lib/traim.rb', line 406
def find_record(id)
@model.find id
end
|
#has_many(name, options = {}, &block) ⇒ Object
380
381
382
|
# File 'lib/traim.rb', line 380
def has_many(name, options={}, &block)
fields << {name: name, type: 'association', resource: options[:resource], block: block}
end
|
#has_one(name, options = {}, &block) ⇒ Object
383
384
385
|
# File 'lib/traim.rb', line 383
def has_one(name, options={}, &block)
fields << {name: name, type: 'connection', resource: options[:resource], block: block}
end
|
392
393
394
395
|
# File 'lib/traim.rb', line 392
def (key = nil, value = nil)
return @headers if key.nil?
@headers[key] = value
end
|
#instance_record(id) ⇒ Object
387
388
389
390
|
# File 'lib/traim.rb', line 387
def instance_record(id)
@id = id
@record = find_record(@id)
end
|
#is_collection? ⇒ Boolean
425
|
# File 'lib/traim.rb', line 425
def is_collection?; record.is_a?(Enumerable) end
|
#logger ⇒ Object
362
|
# File 'lib/traim.rb', line 362
def logger; Traim.logger end
|
#map_record ⇒ Object
427
428
429
430
431
432
433
|
# File 'lib/traim.rb', line 427
def map_record
@record.map do |item|
new_controller = self.class.new(@model, @actions)
new_controller.record = item
yield item, new_controller
end
end
|
#no_cotent ⇒ Object
367
|
# File 'lib/traim.rb', line 367
def no_cotent; @status = 204 end
|
#ok ⇒ Object
365
|
# File 'lib/traim.rb', line 365
def ok; @status = 200 end
|
#record ⇒ Object
398
|
# File 'lib/traim.rb', line 398
def record; @record ||= find_record(id) end
|
#record=(record) ⇒ Object
397
|
# File 'lib/traim.rb', line 397
def record=(record); @record = record end
|
#show(options = {}, &block) ⇒ Object
369
|
# File 'lib/traim.rb', line 369
def show(options = {}, &block); @actions["GET"] = {block: block, options: options} end
|
#update(options = {}, &block) ⇒ Object
371
|
# File 'lib/traim.rb', line 371
def update(options = {}, &block); @actions["PUT"] = {block: block, options: options} end
|
#update_record(id, params) ⇒ Object
418
419
420
421
422
423
|
# File 'lib/traim.rb', line 418
def update_record(id, params)
resource = find_record(id)
resource.assign_attributes(params)
resource.save
resource
end
|