Class: Rails::Resource

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

Direct Known Subclasses

SingletonResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, scope, options) ⇒ Resource

Returns a new instance of Resource.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rails/resource.rb', line 7

def initialize(entity, scope, options)
  self.name_prefix = scope[:as]
  self.name = entity.to_s
  self.singular_name = name.singularize
  self.id = [name_prefix, name].compact.join('_')
  self.home_route = Rails.application.routes.named_routes[id]
  self.path_prefix = home_route.path.sub("/#{name}(.:format)", '')
  self.path_prefix = nil if path_prefix.blank?
  self.prefix_parameters = home_route.segment_keys.find_all{|sk| sk.to_s.match(/_id\Z/)}.to_set
  self.controller_path = home_route.requirements[:controller]
end

Instance Attribute Details

#controller_pathObject

Returns the value of attribute controller_path.



5
6
7
# File 'lib/rails/resource.rb', line 5

def controller_path
  @controller_path
end

#home_routeObject

Returns the value of attribute home_route.



5
6
7
# File 'lib/rails/resource.rb', line 5

def home_route
  @home_route
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/rails/resource.rb', line 5

def id
  @id
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/rails/resource.rb', line 5

def name
  @name
end

#name_prefixObject

Returns the value of attribute name_prefix.



5
6
7
# File 'lib/rails/resource.rb', line 5

def name_prefix
  @name_prefix
end

#path_prefixObject

Returns the value of attribute path_prefix.



5
6
7
# File 'lib/rails/resource.rb', line 5

def path_prefix
  @path_prefix
end

#prefix_parametersObject

Returns the value of attribute prefix_parameters.



5
6
7
# File 'lib/rails/resource.rb', line 5

def prefix_parameters
  @prefix_parameters
end

#singular_nameObject

Returns the value of attribute singular_name.



5
6
7
# File 'lib/rails/resource.rb', line 5

def singular_name
  @singular_name
end

Instance Method Details

#controllerObject



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

def controller
  @controller ||= (controller_path + '_controller').classify.constantize
end

#pathObject



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

def path
  path_prefix ? "#{path_prefix}/#{name}" : "/#{name}"
end

#singleton?Boolean

Returns:

  • (Boolean)


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

def singleton?
  false
end

#to_xml(options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rails/resource.rb', line 31

def to_xml(options = {})
  options[:indent] ||= 2
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[:skip_instruct]
  xml.resource do
    xml.id id
    xml.type self.class
    xml.name name
    xml.singular_name singular_name
    xml.path_prefix path_prefix
    xml.path path
    xml.controller_path controller_path
  end
end