Class: RestArea::Resource

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klss) ⇒ Resource

Returns a new instance of Resource.



6
7
8
9
# File 'lib/rest_area/resource.rb', line 6

def initialize(klss)
  @actions = []
  @klass = klss.to_s.classify.constantize
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



4
5
6
# File 'lib/rest_area/resource.rb', line 4

def actions
  @actions
end

#klassObject (readonly)

Returns the value of attribute klass.



4
5
6
# File 'lib/rest_area/resource.rb', line 4

def klass
  @klass
end

Instance Method Details

#action(*args) ⇒ Object



11
12
13
14
15
# File 'lib/rest_area/resource.rb', line 11

def action(*args)
  @actions ||= []
  @actions += args
  @actions.uniq!
end

#can_do?(act) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rest_area/resource.rb', line 53

def can_do?(act)
  self.actions.empty? || self.actions.include?(act)
end

#can_send?(msg) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rest_area/resource.rb', line 57

def can_send?(msg)
  self.messages.include?(msg.to_sym)
end

#find(*args) ⇒ Object

Wrapped Methods



66
67
68
69
70
71
72
# File 'lib/rest_area/resource.rb', line 66

def find(*args)
  if key == :id
    klass.find(*args)
  else
    klass.where(key => args[0]).first!
  end
end

#headers(hdrs = nil) ⇒ Object Also known as: headers=



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rest_area/resource.rb', line 36

def headers(hdrs = nil)
  @headers ||= {}
  if hdrs
    @headers.merge! hdrs
  else
    @headers.inject({}){ |hash, (key, value)|
      value = value.call if value.kind_of? Proc
      hash.merge(key => value)
    }
  end
end

#key(key = nil) ⇒ Object



61
62
63
# File 'lib/rest_area/resource.rb', line 61

def key(key = nil)
  key ? @key = key : (@key || :id)
end

#message(msg) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/rest_area/resource.rb', line 26

def message(msg)
  @messages ||= []
  if klass.method_defined? msg
    @messages << msg
    @messages.uniq!
  else
    raise NoMethodError.new("#{klass} will not respond to #{msg}")
  end
end

#messages(*args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rest_area/resource.rb', line 17

def messages(*args)
  @messages ||= []
  if args.any?
    args.each do |m| message(m) end
  else
    @messages
  end
end

#read_only!Object



49
50
51
# File 'lib/rest_area/resource.rb', line 49

def read_only!
  @actions = [:index, :show]
end