Class: UtopiaData::Application

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeApplication

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.expand_path('app/resource', Rails.root)]
end

Instance Attribute Details

#load_pathsObject

A hash of all the registered resources



30
31
32
# File 'lib/utopia_data/application.rb', line 30

def load_paths
  @load_paths
end

#resourcesObject

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_pathObject

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, options = {}, &block) # :nodoc:
  return @resources[name] if resources[name]
  resource = Resource.new(name, options, &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

Returns:

  • (Boolean)


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, options = {}, &block)
  resource = find_or_create_resource(resource_name, options, &block)
end

#routerObject

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