Class: Traim::Resource

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

Constant Summary collapse

ACTION_METHODS =
{create: 'POST', show: 'GET', update: 'PUT', destory: 'DELETE'}

Instance Method Summary collapse

Constructor Details

#initialize(block, helper) ⇒ Resource

Returns a new instance of Resource.


234
235
236
237
# File 'lib/traim.rb', line 234

def initialize(block, helper) 
  instance_eval(&block) 
  @helper = helper
end

Instance Method Details

#action(name, options = {}, &block) ⇒ Object


250
251
252
# File 'lib/traim.rb', line 250

def action(name, options = {}, &block)
  actions[ACTION_METHODS[name]] = {block: block, options: options}
end

#actionsObject


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

def actions; @actions ||= {} end

#attribute(name, &block) ⇒ Object


274
275
276
# File 'lib/traim.rb', line 274

def attribute(name, &block)
  fields << {name: name, type: 'attribute', block: block} 
end

#build_controller(request) ⇒ Object


286
287
288
# File 'lib/traim.rb', line 286

def build_controller(request)
  controller.new(model, actions.dup, @helper.clone, request)
end

#collection(name, &block) ⇒ Object


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

def collection(name, &block)
  collections[name] = block
end

#collectionsObject


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

def collections; @collections ||= {} end

#controller(object = nil) ⇒ Object


244
245
246
247
# File 'lib/traim.rb', line 244

def controller(object = nil)
  @controller = object unless object.nil?
  @controller || ArController
end

#fieldsObject


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

def fields; @fields ||= [] end

#has_many(name, options = {}, &block) ⇒ Object


278
279
280
# File 'lib/traim.rb', line 278

def has_many(name, options={}, &block)
  fields << {name: name, type: 'association', resource: options[:resource], block: block}
end

#has_one(name, options = {}, &block) ⇒ Object


282
283
284
# File 'lib/traim.rb', line 282

def has_one(name, options={}, &block)
  fields << {name: name, type: 'connection', resource: options[:resource], block: block}
end

#loggerObject


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

def logger; Traim.logger  end

#member(name, &block) ⇒ Object


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

def member(name, &block)
  members[name] = block
end

#membersObject


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

def members; @members ||= {} end

#model(object = nil, options = {}) ⇒ Object


239
240
241
242
# File 'lib/traim.rb', line 239

def model(object = nil, options = {})
  @model = object unless object.nil? 
  @model
end

#resource(object = nil) ⇒ Object


256
257
258
259
# File 'lib/traim.rb', line 256

def resource(object = nil)
  @resource = object unless object.nil?
  @resource
end

#to_hash(controller, resources, nested_associations = []) ⇒ Object


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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'lib/traim.rb', line 290

def to_hash(controller, resources, nested_associations = []) 
  return if controller.nil?

  hash_fields = fields
  hash_fields += controller.fields if controller.respond_to? :fields
  hash_fields.inject({}) do | hash, attr|
    name = attr[:name]

    hash[name] = if attr[:type] == 'attribute'
      if attr[:block].nil?
        controller.attributes[name.to_s]
      else
        attribute_controller = build_controller(controller.request)
        attribute_controller.record = controller.record
        attribute_controller.execute(attr[:block])
      end
    elsif  attr[:type] == 'association'
      raise Error if nested_associations.include?(name)
      nested_associations << name
      resource = resources[attr[:resource] || name]
      associated_controller = resource.build_controller(controller.request)
      associated_controller.record = if attr[:block].nil?
        controller.send(name)
      else
        controller.dup.execute(attr[:block])
      end
      associated_controller.map_record do |item, controller|
        resource.to_hash(controller, resources, nested_associations.dup) 
      end
    else
      resource_name = name.to_s.pluralize.to_sym
      raise Error.new(message: "Inifinite Association") if nested_associations.include?(resource_name)
      nested_associations << resource_name 
      resource = resources[attr[:resource] || resource_name]
      connected_controller = resource.build_controller(controller.request)
      connected_controller.record = if attr[:block].nil?
        controller.send(name) 
      else
        controller.dup.execute(attr[:block])
      end
      resource.to_hash(connected_controller, resources, nested_associations.dup)
    end
    hash
  end
end