Class: Tzispa::Application

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Engine

#rig_routes

Constructor Details

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

Returns a new instance of Application.



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

def initialize(appid, engine:, on: nil, &block)
  @domain = Domain.new(appid)
  @map_path = on
  @engine = engine
  instance_eval(&block) if block
end

Instance Attribute Details

#domainObject (readonly)

Returns the value of attribute domain.



19
20
21
# File 'lib/tzispa/app.rb', line 19

def domain
  @domain
end

#engineObject (readonly)

Returns the value of attribute engine.



19
20
21
# File 'lib/tzispa/app.rb', line 19

def engine
  @engine
end

#loggerObject (readonly)

Returns the value of attribute logger.



19
20
21
# File 'lib/tzispa/app.rb', line 19

def logger
  @logger
end

#map_pathObject (readonly)

Returns the value of attribute map_path.



19
20
21
# File 'lib/tzispa/app.rb', line 19

def map_path
  @map_path
end

#routesObject (readonly)

Returns the value of attribute routes.



19
20
21
# File 'lib/tzispa/app.rb', line 19

def routes
  @routes
end

Class Method Details

.[](name) ⇒ Object



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

def [](name)
  applications[name]
end

.__new__Object



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

alias __new__ :new

.add(app) ⇒ Object



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

def add(app)
  synchronize do
    raise DuplicateDomain.new(app.name) if applications.key?(app.name)
    applications[app.name] = app
  end
end

.applicationsObject

rubocop:disable Style/ClassVars



30
31
32
33
34
# File 'lib/tzispa/app.rb', line 30

def applications
  synchronize do
    @@applications ||= Hash.new { |_, key| raise UnknownApplication(key.to_s) }
  end
end

.new(*args, &block) ⇒ Object



25
26
27
# File 'lib/tzispa/app.rb', line 25

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

.synchronizeObject



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

def synchronize
  Mutex.new.synchronize { yield }
end

Instance Method Details

#[](domain) ⇒ Object



75
76
77
# File 'lib/tzispa/app.rb', line 75

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

#call(env) ⇒ Object



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

def call(env)
  routes.call env
end

#configObject



83
84
85
# File 'lib/tzispa/app.rb', line 83

def config
  @config ||= Config::AppConfig.new(@domain).load!
end

#envObject



79
80
81
# File 'lib/tzispa/app.rb', line 79

def env
  Tzispa::Environment.instance
end

#load!Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tzispa/app.rb', line 63

def load!
  tap do |app|
    app.class.synchronize do
      logging_setup
      locales_setup
      repository&.load!(domain)
      domain.setup
      routes_setup
    end
  end
end

#repositoryObject



87
88
89
90
91
92
# File 'lib/tzispa/app.rb', line 87

def repository
  @repository ||= begin
    dbcfg = Config::DbConfig.new(env.environment)&.to_h
    Data::Repository.new(dbcfg) if dbcfg&.count&.positive?
  end
end