Class: ShellContext
- Inherits:
-
Object
- Object
- ShellContext
- Defined in:
- lib/shell_context.rb
Overview
- Author
-
Eric Crane (eric.crane@mac.com)
- Copyright
-
Copyright © 2026 Eric Crane. All rights reserved.
A shell context. Data associated with the shell session.
Instance Attribute Summary collapse
-
#done ⇒ Object
Returns the value of attribute done.
Instance Method Summary collapse
-
#add_to_list(key, item) ⇒ Object
Add an item to a property list.
-
#get(key) ⇒ Object
Get a property value.
-
#has?(key) ⇒ Boolean
Check if a property exists.
-
#initialize ⇒ ShellContext
constructor
Initialize the shell context.
-
#keys ⇒ Array<Symbol>
Get all property keys.
-
#method_missing(method_name, *args, &block) ⇒ Object
Dynamic method access to properties.
-
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to missing methods for property access.
-
#set(key, value) ⇒ Object
Set a property value.
Constructor Details
#initialize ⇒ ShellContext
Initialize the shell context
15 16 17 18 |
# File 'lib/shell_context.rb', line 15 def initialize @done = false @properties = {} end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
Dynamic method access to properties
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/shell_context.rb', line 86 def method_missing( method_name, *args, &block ) if method_name.to_s.end_with?('=') # Setter method: projects=, tasks=, etc. key = method_name.to_s.chomp('=').to_sym set(key, args.first) else # Getter method: projects, tasks, etc. key = method_name.to_sym get(key) end end |
Instance Attribute Details
#done ⇒ Object
Returns the value of attribute done.
10 11 12 |
# File 'lib/shell_context.rb', line 10 def done @done end |
Instance Method Details
#add_to_list(key, item) ⇒ Object
Add an item to a property list
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/shell_context.rb', line 50 def add_to_list( key, item ) key = key.to_sym list = get(key) # Ensure we have an array to work with list = [] unless list.is_a?(Array) # Add the item and store the updated list list << item set(key, list) list end |
#get(key) ⇒ Object
Get a property value
26 27 28 29 30 31 32 |
# File 'lib/shell_context.rb', line 26 def get( key ) key = key.to_sym return @properties[key] if @properties.key?(key) # If the property doesn't exist, return an empty array return [] end |
#has?(key) ⇒ Boolean
Check if a property exists
70 71 72 |
# File 'lib/shell_context.rb', line 70 def has?( key ) @properties.key?( key.to_sym ) end |
#keys ⇒ Array<Symbol>
Get all property keys
79 80 81 |
# File 'lib/shell_context.rb', line 79 def keys @properties.keys end |
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
Respond to missing methods for property access
101 102 103 104 |
# File 'lib/shell_context.rb', line 101 def respond_to_missing?( method_name, include_private = false ) key = method_name.to_s.chomp('=').to_sym has?(key) || super end |
#set(key, value) ⇒ Object
Set a property value
40 41 42 |
# File 'lib/shell_context.rb', line 40 def set( key, value ) @properties[key.to_sym] = value end |