Class: Tzispa::Application

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/tzispa/app.rb

Defined Under Namespace

Classes: ApplicationError, DuplicateDomain, UnknownApplication

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(appid, on: nil, &block) ⇒ Application

Returns a new instance of Application.



70
71
72
73
74
75
76
# File 'lib/tzispa/app.rb', line 70

def initialize(appid, on: nil, &block)
  @domain = Domain.new(appid)
  @config = Config::AppConfig.new(@domain).load!
  @middleware = Middleware.new self
  @routes ||= Routes.new(self, on)
  instance_eval(&block) if block
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def config
  @config
end

#domainObject (readonly)

Returns the value of attribute domain.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def domain
  @domain
end

#engineObject (readonly)

Returns the value of attribute engine.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def engine
  @engine
end

#loggerObject (readonly)

Returns the value of attribute logger.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def logger
  @logger
end

#middlewareObject (readonly)

Returns the value of attribute middleware.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def middleware
  @middleware
end

#mount_pathObject (readonly)

Returns the value of attribute mount_path.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def mount_path
  @mount_path
end

#repositoryObject (readonly)

Returns the value of attribute repository.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def repository
  @repository
end

#routesObject (readonly)

Returns the value of attribute routes.



22
23
24
# File 'lib/tzispa/app.rb', line 22

def routes
  @routes
end

Class Method Details

.[](name) ⇒ Object



52
53
54
# File 'lib/tzispa/app.rb', line 52

def [](name)
  applications[name]
end

.__new__Object



32
# File 'lib/tzispa/app.rb', line 32

alias :__new__ :new

.add(app) ⇒ Object



56
57
58
59
60
61
# File 'lib/tzispa/app.rb', line 56

def add(app)
  synchronize do
    raise DuplicateDomain.new("You have try to add an app with a duplicate domain name #{app.name}") if applications.has_key? app.name
    applications[app.name] = app
  end
end

.applicationsObject



40
41
42
43
44
# File 'lib/tzispa/app.rb', line 40

def applications
  synchronize do
    @@applications ||= Hash.new{ |hash, key| raise UnknownApplication.new("#{key}") }
  end
end

.new(*args, &block) ⇒ Object



34
35
36
37
38
# File 'lib/tzispa/app.rb', line 34

def new(*args, &block)
  __new__(*args, &block).tap { |app|
    add app
  }
end

.run(appid, builder: nil, on: nil, &block) ⇒ Object



63
64
65
66
# File 'lib/tzispa/app.rb', line 63

def run(appid, builder: nil, on: nil, &block)
  theapp = self.new appid, on: on, &block
  theapp.run builder
end

.synchronizeObject



46
47
48
49
50
# File 'lib/tzispa/app.rb', line 46

def synchronize
  Mutex.new.synchronize {
    yield
  }
end

Instance Method Details

#[](domain) ⇒ Object



103
104
105
# File 'lib/tzispa/app.rb', line 103

def [](domain)
  self.class[domain]
end

#load!Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tzispa/app.rb', line 90

def load!
  self.class.synchronize {
    load_locales
    @logger = Logger.new("logs/#{domain.name}.log", config.logging.shift_age).tap { |log|
      log.level = config.developing ? Logger::DEBUG : Logger::INFO
    } if config.logging&.enabled
    domain_setup
    @repository = Data::Repository.new(config.repository.to_h).load!(domain) if config.respond_to? :repository
    @loaded = true
  }
  self
end

#run(builder = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tzispa/app.rb', line 78

def run(builder=nil)
  builder ||= ::Rack::Builder.new
  if routes.map_path
    this_app = self
    builder.map routes.map_path do
      run this_app.middleware.builder
    end
  else
    builder.run middleware.builder
  end
end