Class: PromptManager::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_manager/prompt.rb

Constant Summary collapse

COMMENT_SIGNAL =

lines beginning with this are a comment

'#'
DIRECTIVE_SIGNAL =

Like the old IBM JCL

'//'
DEFAULT_PARAMETER_REGEX =
/(\[[A-Z _|]+\])/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, context: [], directives_processor: PromptManager::DirectiveProcessor.new, external_binding: binding, erb_flag: false, envar_flag: false) ⇒ Prompt

Returns a new instance of Prompt.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/prompt_manager/prompt.rb', line 65

def initialize(
    id:       nil,    # A String name for the prompt
    context:  [],     # TODO: Array of Strings or Pathname?
    directives_processor: PromptManager::DirectiveProcessor.new,
    external_binding:     binding,
    erb_flag:             false,    # replace $ENVAR and ${ENVAR} when true
    envar_flag:           false     # process ERB against the external_binding when true
  )

  @id  = id
  @directives_processor = directives_processor

  validate_arguments(@id)

  @record           = db.get(id: id)
  @text             = @record[:text]        || ""
  @parameters       = @record[:parameters]  || {}
  @directives       = {}
  @external_binding = external_binding
  @erb_flag         = erb_flag
  @envar_flag       = envar_flag
end

Class Attribute Details

.parameter_regexObject

Returns the value of attribute parameter_regex.



15
16
17
# File 'lib/prompt_manager/prompt.rb', line 15

def parameter_regex
  @parameter_regex
end

.storage_adapterObject

Returns the value of attribute storage_adapter.



15
16
17
# File 'lib/prompt_manager/prompt.rb', line 15

def storage_adapter
  @storage_adapter
end

Instance Attribute Details

#idObject

Public Instance Methods



60
61
62
# File 'lib/prompt_manager/prompt.rb', line 60

def id
  @id
end

#parametersObject

Public Instance Methods



60
61
62
# File 'lib/prompt_manager/prompt.rb', line 60

def parameters
  @parameters
end

#textObject

Public Instance Methods



60
61
62
# File 'lib/prompt_manager/prompt.rb', line 60

def text
  @text
end

Class Method Details

.create(id:, text: "", parameters: {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/prompt_manager/prompt.rb', line 21

def create(id:, text: "", parameters: {})
  storage_adapter.save(
    id:         id,
    text:       text,
    parameters: parameters
  )

  ::PromptManager::Prompt.new(id: id, context: [], directives_processor: PromptManager::DirectiveProcessor.new)
end

.destroy(id:) ⇒ Object



35
36
37
38
# File 'lib/prompt_manager/prompt.rb', line 35

def destroy(id:)
  prompt = find(id: id)
  prompt.delete
end

.find(id:) ⇒ Object



31
32
33
# File 'lib/prompt_manager/prompt.rb', line 31

def find(id:)
  ::PromptManager::Prompt.new(id: id, context: [], directives_processor: PromptManager::DirectiveProcessor.new)
end

.get(id:) ⇒ Object



17
18
19
# File 'lib/prompt_manager/prompt.rb', line 17

def get(id:)
  storage_adapter.get(id: id)  # Return the hash directly from storage
end

.method_missing(method_name, *args, &block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/prompt_manager/prompt.rb', line 44

def method_missing(method_name, *args, &block)
  if storage_adapter.respond_to?(method_name)
    storage_adapter.send(method_name, *args, &block)
  else
    super
  end
end

.respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/prompt_manager/prompt.rb', line 52

def respond_to_missing?(method_name, include_private = false)
  storage_adapter.respond_to?(method_name, include_private) || super
end

.search(for_what) ⇒ Object



40
41
42
# File 'lib/prompt_manager/prompt.rb', line 40

def search(for_what)
  storage_adapter.search(for_what)
end

Instance Method Details

#deleteObject



109
# File 'lib/prompt_manager/prompt.rb', line 109

def delete = db.delete(id: id)

#saveObject



101
102
103
104
105
106
107
# File 'lib/prompt_manager/prompt.rb', line 101

def save
  db.save(
    id:         id,
    text:       text,       # Save the original text
    parameters: parameters
  )
end

#to_sObject



93
94
95
96
97
98
99
# File 'lib/prompt_manager/prompt.rb', line 93

def to_s
  processed_text = remove_comments
  processed_text = substitute_values(processed_text, @parameters)
  processed_text = substitute_env_vars(processed_text)
  processed_text = process_directives(processed_text)
  process_erb(processed_text)
end

#validate_arguments(prompt_id, prompts_db = db) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
# File 'lib/prompt_manager/prompt.rb', line 88

def validate_arguments(prompt_id, prompts_db=db)
  raise ArgumentError, 'id cannot be blank'           if prompt_id.nil? || prompt_id.strip.empty?
  raise(ArgumentError, 'storage_adapter is not set')  if prompts_db.nil?
end