Class: Charming::Application

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

Overview

Application is a lightweight, Rails-inspired application base for building terminal-based apps. It provides routing (via a DSL), session storage, and task execution for managing async operations.

Constant Summary collapse

THEME_READER =
Object.new.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

Initializes an empty session hash for per-request state storage.



72
73
74
# File 'lib/charming/application.rb', line 72

def initialize
  @session = {}
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



69
70
71
# File 'lib/charming/application.rb', line 69

def session
  @session
end

#task_executorObject

Returns the value of attribute task_executor.



68
69
70
# File 'lib/charming/application.rb', line 68

def task_executor
  @task_executor
end

Class Method Details

.default_theme(name = THEME_READER) ⇒ Object



46
47
48
49
50
# File 'lib/charming/application.rb', line 46

def default_theme(name = THEME_READER)
  return @default_theme || themes.keys.first if name == THEME_READER

  @default_theme = name.to_sym
end

.namespaceObject

Derives the module namespace from the class name — e.g., Admin::HomeController yields “Admin”. Mirrors Rails’ engine-style namespacing.



21
22
23
# File 'lib/charming/application.rb', line 21

def namespace
  name&.split("::")&.then { |parts| parts[0...-1].join("::") }
end

.root(path = THEME_READER) ⇒ Object



25
26
27
28
29
# File 'lib/charming/application.rb', line 25

def root(path = THEME_READER)
  return @root if path == THEME_READER

  @root = File.expand_path(path)
end

.routes(&block) ⇒ Object

Registers or returns the app’s Router. Accepts an optional block to define routes via DSL (screen, root). Lazily initializes a new Router per namespace.



13
14
15
16
17
# File 'lib/charming/application.rb', line 13

def routes(&block)
  @routes ||= Router.new(namespace: namespace)
  @routes.draw(&block) if block
  @routes
end

.theme(name, from: nil, built_in: nil) ⇒ Object

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
38
39
40
# File 'lib/charming/application.rb', line 31

def theme(name, from: nil, built_in: nil)
  raise ArgumentError, "theme expects from: or built_in:" unless from || built_in
  raise ArgumentError, "theme expects either from: or built_in:, not both" if from && built_in

  themes[name.to_sym] = if built_in
    UI::Theme.load_builtin(built_in)
  else
    UI::Theme.load_file(resolve_theme_path(from))
  end
end

.theme_for(name = nil) ⇒ Object



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

def theme_for(name = nil)
  theme_name = name || default_theme
  return UI::Theme.default unless theme_name

  themes.fetch(theme_name.to_sym)
end

.themesObject



42
43
44
# File 'lib/charming/application.rb', line 42

def themes
  @themes ||= superclass.respond_to?(:themes) ? superclass.themes.dup : {}
end

Instance Method Details

#routesObject

Delegates to the class-level Router, providing instance access to route definitions.



77
78
79
# File 'lib/charming/application.rb', line 77

def routes
  self.class.routes
end

#themeObject



81
82
83
# File 'lib/charming/application.rb', line 81

def theme
  self.class.theme_for(session[:theme])
end

#use_theme(name) ⇒ Object



85
86
87
88
# File 'lib/charming/application.rb', line 85

def use_theme(name)
  self.class.theme_for(name)
  session[:theme] = name.to_sym
end