Class: Opi::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = '', options = {}, before = [], after = [], block = nil) ⇒ Resource

Returns a new instance of Resource.



5
6
7
8
9
10
11
12
13
# 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

Instance Attribute Details

#after_filtersObject (readonly)

Returns the value of attribute after_filters.



3
4
5
# File 'lib/opi/resource.rb', line 3

def after_filters
  @after_filters
end

#before_filtersObject (readonly)

Returns the value of attribute before_filters.



3
4
5
# File 'lib/opi/resource.rb', line 3

def before_filters
  @before_filters
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/opi/resource.rb', line 3

def options
  @options
end

#resourcesObject (readonly)

Returns the value of attribute resources.



3
4
5
# File 'lib/opi/resource.rb', line 3

def resources
  @resources
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/opi/resource.rb', line 3

def root
  @root
end

#routesObject (readonly)

Returns the value of attribute routes.



3
4
5
# File 'lib/opi/resource.rb', line 3

def routes
  @routes
end

Instance Method Details

#after(method) ⇒ Object



35
36
37
# File 'lib/opi/resource.rb', line 35

def after(method)
  after_filters << method
end

#before(method) ⇒ Object



31
32
33
# File 'lib/opi/resource.rb', line 31

def before(method)
  before_filters << method
end

#delete(path = nil, options = {}, &block) ⇒ Object



27
28
29
# File 'lib/opi/resource.rb', line 27

def delete(path=nil, options={}, &block)
  route 'DELETE', path, options, block
end

#get(path = nil, options = {}, &block) ⇒ Object



15
16
17
# File 'lib/opi/resource.rb', line 15

def get(path=nil, options={}, &block)
  route 'GET', path, options, block
end

#post(path = nil, options = {}, &block) ⇒ Object



19
20
21
# File 'lib/opi/resource.rb', line 19

def post(path=nil, options={}, &block)
  route 'POST', path, options, block
end

#put(path = nil, options = {}, &block) ⇒ Object



23
24
25
# File 'lib/opi/resource.rb', line 23

def put(path=nil, options={}, &block)
  route 'PUT', path, options, block
end

#resource(path, options = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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