Class: Sxn::Templates::TemplateVariables
- Inherits:
-
Object
- Object
- Sxn::Templates::TemplateVariables
- Defined in:
- lib/sxn/templates/template_variables.rb
Overview
TemplateVariables collects and prepares variables for template processing. It gathers context from sessions, git repositories, projects, and environment.
Variable Categories:
-
session: Current session information (name, path, created_at, etc.)
-
git: Git repository information (branch, author, last_commit, etc.)
-
project: Project details (name, type, path, etc.)
-
environment: Runtime environment (ruby version, rails version, etc.)
-
user: User preferences and git configuration
Example:
collector = TemplateVariables.new(session, project)
variables = collector.collect
# => {
# session: { name: "ATL-1234", path: "/path/to/session" },
# git: { branch: "feature/cart", author: "John Doe" },
# project: { name: "atlas-core", type: "rails" }
# }
Constant Summary collapse
- GIT_TIMEOUT =
Git command timeout in seconds
5
Instance Method Summary collapse
-
#add_custom_variables(custom_vars) ⇒ Object
Add custom variables that will be merged with collected variables.
-
#collect ⇒ Hash
(also: #build_variables, #collect_all_variables)
Collect all template variables.
- #collect_environment_variables ⇒ Object
- #collect_git_variables ⇒ Object
- #collect_project_variables ⇒ Object
-
#collect_session_variables ⇒ Object
Public aliases for collection methods (expected by tests).
- #collect_timestamp_variables ⇒ Object
- #collect_user_variables ⇒ Object
-
#detect_node_version ⇒ Object
Detect Node.js version if available.
-
#detect_rails_version ⇒ Object
Detect Rails version if available.
-
#detect_ruby_version ⇒ Object
Detect Ruby version.
-
#get_category(category) ⇒ Hash
Get variables for a specific category.
-
#initialize(session = nil, project = nil, config = nil) ⇒ TemplateVariables
constructor
A new instance of TemplateVariables.
-
#refresh! ⇒ Object
Refresh cached variables (useful for long-running processes).
Constructor Details
#initialize(session = nil, project = nil, config = nil) ⇒ TemplateVariables
Returns a new instance of TemplateVariables.
34 35 36 37 38 39 |
# File 'lib/sxn/templates/template_variables.rb', line 34 def initialize(session = nil, project = nil, config = nil) @session = session @project = project @config = config @cached_variables = {} end |
Instance Method Details
#add_custom_variables(custom_vars) ⇒ Object
Add custom variables that will be merged with collected variables
85 86 87 88 89 90 |
# File 'lib/sxn/templates/template_variables.rb', line 85 def add_custom_variables(custom_vars) return unless custom_vars.is_a?(Hash) # Merge custom variables, with custom taking precedence @cached_variables = collect.deep_merge(custom_vars) end |
#collect ⇒ Hash Also known as: build_variables, collect_all_variables
Collect all template variables
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sxn/templates/template_variables.rb', line 44 def collect return @cached_variables unless @cached_variables.empty? # steep:ignore:start - Template variable collection uses metaprogramming # The template system uses dynamic method calls and variable resolution that # cannot be statically typed. Runtime validation ensures type safety. @cached_variables = { session: _collect_session_variables, git: _collect_git_variables, project: _collect_project_variables, environment: _collect_environment_variables, user: _collect_user_variables, timestamp: }.compact # Runtime validation of collected variables validate_collected_variables(@cached_variables) @cached_variables end |
#collect_environment_variables ⇒ Object
105 106 107 |
# File 'lib/sxn/templates/template_variables.rb', line 105 def collect_environment_variables get_category(:environment) end |
#collect_git_variables ⇒ Object
101 102 103 |
# File 'lib/sxn/templates/template_variables.rb', line 101 def collect_git_variables get_category(:git) end |
#collect_project_variables ⇒ Object
97 98 99 |
# File 'lib/sxn/templates/template_variables.rb', line 97 def collect_project_variables get_category(:project) end |
#collect_session_variables ⇒ Object
Public aliases for collection methods (expected by tests)
93 94 95 |
# File 'lib/sxn/templates/template_variables.rb', line 93 def collect_session_variables get_category(:session) end |
#collect_timestamp_variables ⇒ Object
113 114 115 |
# File 'lib/sxn/templates/template_variables.rb', line 113 def get_category(:timestamp) end |
#collect_user_variables ⇒ Object
109 110 111 |
# File 'lib/sxn/templates/template_variables.rb', line 109 def collect_user_variables get_category(:user) end |
#detect_node_version ⇒ Object
Detect Node.js version if available
137 138 139 140 141 142 143 144 |
# File 'lib/sxn/templates/template_variables.rb', line 137 def detect_node_version return nil unless node_available? result = collect_node_version result.is_a?(Hash) && result[:version] ? result[:version] : nil rescue StandardError nil end |
#detect_rails_version ⇒ Object
Detect Rails version if available
127 128 129 130 131 132 133 134 |
# File 'lib/sxn/templates/template_variables.rb', line 127 def detect_rails_version return nil unless rails_available? result = collect_rails_version result.is_a?(Hash) && result[:version] ? result[:version] : nil rescue StandardError nil end |
#detect_ruby_version ⇒ Object
Detect Ruby version
118 119 120 121 122 123 124 |
# File 'lib/sxn/templates/template_variables.rb', line 118 def detect_ruby_version return "Unknown Ruby version" if RUBY_VERSION.nil? RUBY_VERSION rescue StandardError => e "Unknown Ruby version: #{e.message}" end |
#get_category(category) ⇒ Hash
Get variables for a specific category
78 79 80 |
# File 'lib/sxn/templates/template_variables.rb', line 78 def get_category(category) collect[category] || {} end |
#refresh! ⇒ Object
Refresh cached variables (useful for long-running processes)
69 70 71 72 |
# File 'lib/sxn/templates/template_variables.rb', line 69 def refresh! @cached_variables = {} collect end |