Module: Octopi::Resource::ClassMethods

Defined in:
lib/octopi/resource.rb

Instance Method Summary collapse

Instance Method Details

#create_path(path) ⇒ Object



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

def create_path(path)
  (@path_spec||={})[:create] = path
end

#declassify(s) ⇒ Object



66
67
68
# File 'lib/octopi/resource.rb', line 66

def declassify(s)
  (s.split('::').last || '').downcase if s
end

#delete_path(path) ⇒ Object



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

def delete_path(path)
  (@path_spec||={})[:delete] = path
end

#find(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/octopi/resource.rb', line 39

def find(*args)
  api = args.last.is_a?(Api) ? args.pop : ANONYMOUS_API
  args = args.join('/') if args.is_a? Array
  result = api.find(path_for(:resource), @resource_name[:singular], args)
  key = result.keys.first

  if result[key].is_a? Array
    result[key].map { |r| new(api, r) }
  else  
    Resource.for(key).new(api, result[key])
  end  
end

#find_all(*s) ⇒ Object



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

def find_all(*s)
  api = s.last.is_a?(Api) ? s.pop : ANONYMOUS_API
  find_plural(s, :find, api)
end

#find_path(path) ⇒ Object



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

def find_path(path)
  (@path_spec||={})[:find] = path
end

#find_plural(s, path, api = ANONYMOUS_API) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/octopi/resource.rb', line 57

def find_plural(s, path, api = ANONYMOUS_API)
  s = s.join('/') if s.is_a? Array
  api.find_all(path_for(path), @resource_name[:plural], s).
    map do |item|
      payload = block_given? ? yield(item) : item
      new(api, payload)
    end
end

#path_for(type) ⇒ Object



70
71
72
# File 'lib/octopi/resource.rb', line 70

def path_for(type)
  @path_spec[type]
end

#resource_name(key) ⇒ Object



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

def resource_name(key)
  @resource_name[key]
end

#resource_path(path) ⇒ Object



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

def resource_path(path)
  (@path_spec||={})[:resource] = path
end

#set_resource_name(singular, plural = "#{singular}s") ⇒ Object



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

def set_resource_name(singular, plural = "#{singular}s")
  @resource_name = {:singular => declassify(singular), :plural => declassify(plural)}
end