Class: UtopiaData::Application
- Inherits:
-
Object
- Object
- UtopiaData::Application
- Defined in:
- lib/utopia_data/application.rb
Overview
In UtopiaData 0.0.1, UtopiaData::Application object is to manipulate the utopia system, here there are the recources, loads, start routes, etc
Constant Summary collapse
- @@loaded =
false
Instance Attribute Summary collapse
-
#load_paths ⇒ Object
A hash of all the registered resources.
-
#resources ⇒ Object
A hash of all the registered resources.
Instance Method Summary collapse
-
#files_in_load_path ⇒ Object
get files to (re)load.
-
#find_or_create_resource(name, options = {}, &block) ⇒ Object
:nodoc:.
-
#initialize ⇒ Application
constructor
create a default value to resources and Load paths for utopia configurations.
-
#load! ⇒ Object
Load files in files_in_load_path and set @loaded to true.
-
#loaded? ⇒ Boolean
verify if the application is loaded or not.
-
#register(resource_name, options = {}, &block) ⇒ Object
Register a resource You can register a resource using the generetor resource, only run: rails g utopia:resource lei.
-
#router ⇒ Object
The Router to this application.
-
#routes(rails_router) ⇒ Object
Create the routes to resource on your application use:.
Constructor Details
#initialize ⇒ Application
create a default value to resources and Load paths for utopia configurations. Add folders to this load path to load up other resources for utopia.
35 36 37 38 |
# File 'lib/utopia_data/application.rb', line 35 def initialize self.resources ||= {} self.load_paths = [File.('app/resource', Rails.root)] end |
Instance Attribute Details
#load_paths ⇒ Object
A hash of all the registered resources
30 31 32 |
# File 'lib/utopia_data/application.rb', line 30 def load_paths @load_paths end |
#resources ⇒ Object
A hash of all the registered resources
30 31 32 |
# File 'lib/utopia_data/application.rb', line 30 def resources @resources end |
Instance Method Details
#files_in_load_path ⇒ Object
get files to (re)load
99 100 101 |
# File 'lib/utopia_data/application.rb', line 99 def files_in_load_path load_paths.flatten.compact.uniq.collect{|path| Dir["#{path}/**/*.rb"] }.flatten end |
#find_or_create_resource(name, options = {}, &block) ⇒ Object
:nodoc:
77 78 79 80 81 |
# File 'lib/utopia_data/application.rb', line 77 def find_or_create_resource(name, = {}, &block) # :nodoc: return @resources[name] if resources[name] resource = Resource.new(name, , &block) @resources[name] = resource end |
#load! ⇒ Object
Load files in files_in_load_path and set @loaded to true
91 92 93 94 95 96 |
# File 'lib/utopia_data/application.rb', line 91 def load! return false if loaded? files_in_load_path.each{|file| load file } @@loaded = true end |
#loaded? ⇒ Boolean
verify if the application is loaded or not
86 87 88 |
# File 'lib/utopia_data/application.rb', line 86 def loaded? @@loaded end |
#register(resource_name, options = {}, &block) ⇒ Object
Register a resource
You can register a resource using the generetor resource, only run:
rails g utopia:resource lei
This command you be create a lei.rb path on app/resource with the content
UtopiaData.register :lei do
configs...
end
or
UtopiaData.regsiter :lei
After, you can enter on /leis.json for example and see your resource expose
55 56 57 |
# File 'lib/utopia_data/application.rb', line 55 def register(resource_name, = {}, &block) resource = find_or_create_resource(resource_name, , &block) end |
#router ⇒ Object
The Router to this application
61 62 63 |
# File 'lib/utopia_data/application.rb', line 61 def router @router ||= Router.new(self) end |
#routes(rails_router) ⇒ Object
Create the routes to resource on your application use:
YourApplication::Application.routes.draw do
UtopiaData.routes(self)
end
72 73 74 75 |
# File 'lib/utopia_data/application.rb', line 72 def routes(rails_router) load! router.apply(rails_router) end |