class Opi::Resource

Attributes

after_filters[R]
before_filters[R]
options[R]
resources[R]
root[R]
routes[R]

Public Class Methods

new(root='', options={}, before=[], after=[], block=nil) click to toggle source
# File lib/opi/resource.rb, line 5
def initialize(root='', options={}, before=[], after=[], block=nil)
  @root = root
  @options = options
  @before_filters = before
  @after_filters = after
  @resources = []
  @routes = []
  instance_eval &block if block
end

Public Instance Methods

after(method) click to toggle source
# File lib/opi/resource.rb, line 35
def after(method)
  after_filters << method
end
before(method) click to toggle source
# File lib/opi/resource.rb, line 31
def before(method)
  before_filters << method
end
delete(path=nil, options={}, &block) click to toggle source
# File lib/opi/resource.rb, line 27
def delete(path=nil, options={}, &block)
  route 'DELETE', path, options, block
end
get(path=nil, options={}, &block) click to toggle source
# File lib/opi/resource.rb, line 15
def get(path=nil, options={}, &block)
  route 'GET', path, options, block
end
post(path=nil, options={}, &block) click to toggle source
# File lib/opi/resource.rb, line 19
def post(path=nil, options={}, &block)
  route 'POST', path, options, block
end
put(path=nil, options={}, &block) click to toggle source
# File lib/opi/resource.rb, line 23
def put(path=nil, options={}, &block)
  route 'PUT', path, options, block
end
resource(path, options={}, &block) click to toggle source
# File lib/opi/resource.rb, line 39
def resource(path, options={}, &block)
  # TODO: clean this up, should be able to determine the child resource path
  root = "#{self.root}/#{path}"
  # TODO: should be able to determine parent resource name, need to store name separately
  unless self.root.empty?
    # TODO: need to singularize ...
    parent_resource = self.root.split('/').last.gsub(/s$/, '')
    puts parent_resource
    root = "#{self.root}/:#{parent_resource}_id/#{path}"
  end
  resources << Resource.new(
    root,
    self.options.merge(options),
    self.before_filters.dup,
    self.after_filters.dup,
    block
  )
end

Private Instance Methods

route(method, path, options={}, block) click to toggle source
# File lib/opi/resource.rb, line 59
def route(method, path, options={}, block)
  path = ":#{path}" if path.is_a? Symbol # TODO: maybe not?
  routes.push Route.new(
    method,
    "#{self.root}/#{path}",
    self.options.merge(options),
    self.before_filters,
    self.after_filters,
    block
  )
end