Class: RubyRest::AbstractApplication

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyrest/application.rb

Overview

Base Application class for all applications deployed with Ruby-on-Rest

Direct Known Subclasses

SimpleApplication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ AbstractApplication

Returns a new instance of AbstractApplication.



16
17
18
19
20
21
22
23
24
25
# File 'lib/rubyrest/application.rb', line 16

def initialize( config )
  @config = config
  setup
  @resources_by_path = Hash.new
  @resources_by_name = Hash.new
  @resources_by_domain = Hash.new
  register_domain
  register_formatters
  init_database
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/rubyrest/application.rb', line 8

def config
  @config
end

Instance Method Details

#formatter(params) ⇒ Object

Resolves the formatter to use in the returned representation



105
106
107
# File 'lib/rubyrest/application.rb', line 105

def formatter( params )
  @formatters[params[:format]]||@formatters[:atom]
end

#init_databaseObject

To be overriden by applications that use databases



12
13
14
# File 'lib/rubyrest/application.rb', line 12

def init_database

end

#is_a_collection(model) ⇒ Object

Tells whether the specified model will be rendered as a feed or entry. To be subclassed



115
116
117
# File 'lib/rubyrest/application.rb', line 115

def is_a_collection( model )
  model == nil || model.is_a?( Array )
end

#is_a_service_doc(model) ⇒ Object

Implement me in subclasses



120
121
122
# File 'lib/rubyrest/application.rb', line 120

def is_a_service_doc( model )
  
end

#register_domainObject

Loads all the domain files provided by the developer



28
29
30
31
32
33
34
35
36
# File 'lib/rubyrest/application.rb', line 28

def register_domain
  Dir.foreach( @doc_base ) { |filename|
    filename = File.basename( filename, ".rb" )
    if !(filename[0] == ?.) && !FileTest.directory?( filename ) && !SYS_FILES.include?( filename )
      require @doc_base + "/" + filename
      register_resource( filename.to_s.intern  )
    end
  }
end

#register_formattersObject

Register all the formatters supported by the application For the moment, only the Atom representation of resources is supported



99
100
101
# File 'lib/rubyrest/application.rb', line 99

def register_formatters
  @formatters = { :atom => RubyRest::Atom::Formatter.new( self ) }
end

#register_resource(name) ⇒ Object

Registers a new resource for the given name



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rubyrest/application.rb', line 74

def register_resource( name )
  puts "mounting resource: #{name} ..."
  resource_klass = to_resource_class( name )
  domain_klass = to_domain_class( name )
  resource_klass.set_domain domain_klass
  resource = resource_klass.new( self )
  @resources_by_path[resource_klass.mount_point] = resource
  @resources_by_domain[domain_klass]=resource
  @resources_by_name[name]=resource
  puts "resource mounted: #{resource} [OK]" 
end

#render_model(params, model) ⇒ Object



109
110
111
# File 'lib/rubyrest/application.rb', line 109

def render_model( params, model )
  formatter( params ).format( model, params )
end

#resource_by_domain(model) ⇒ Object

Retrieves the Ruby-on-Rest resource descriptor for the specified model object



61
62
63
64
65
# File 'lib/rubyrest/application.rb', line 61

def resource_by_domain( model )
  resource = @resources_by_domain[model.class]
  raise "no resource mounted for domain #{model.class}" if !resource
  return resource
end

#resource_by_name(name) ⇒ Object



67
68
69
70
71
# File 'lib/rubyrest/application.rb', line 67

def resource_by_name( name )
  resource = @resources_by_name[name]
  raise "no resource mounted for name #{name}" if !resource
  return resource
end

#resource_by_path(path) ⇒ Object

Resolves the Ruby-on-Rest resource descriptor for the specified request path



50
51
52
53
54
55
56
57
# File 'lib/rubyrest/application.rb', line 50

def resource_by_path( path )
  mount_point = "/"
  path_tokens = path.split( "/" )
  mount_point = "/"+path_tokens[1] if path_tokens.length > 0
  resource = @resources_by_path[mount_point]
  raise "no resource mounted at #{mount_point}" if !resource
  return resource
end

#setupObject

Please override me if needed



39
40
41
# File 'lib/rubyrest/application.rb', line 39

def setup
  
end

#to_domain_class(name) ⇒ Object

Returns the domain class name for the given name



92
93
94
# File 'lib/rubyrest/application.rb', line 92

def to_domain_class( name )
  Class.by_name( @config[:module].capitalize + "::" + @config[:service].capitalize + "::Domain::" + name.to_s.capitalize )
end

#to_resource_class(name) ⇒ Object

Returns the resource class name for the given name



87
88
89
# File 'lib/rubyrest/application.rb', line 87

def to_resource_class( name )
  Class.by_name( @config[:module].capitalize + "::" + @config[:service].capitalize + "::Resource::" + name.to_s.capitalize )
end

#to_sObject

Returns the description and version



44
45
46
# File 'lib/rubyrest/application.rb', line 44

def to_s
  @config[:service] + " " + @config[:version]
end