Class: Foobara::DomainMapper
- Inherits:
-
Object
- Object
- Foobara::DomainMapper
show all
- Includes:
- CommandPatternImplementation
- Defined in:
- foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb,
foobara-0.0.110/projects/domain_mapper/lib/foobara/domain_mapper.rb
Overview
NOTE: If you depend_on a domain mapper in a command, then you need to depend on all domain mappers that that command uses. Or you can just exclude all of them in which case Foobara won’t enforce explicit depends_on of domain mappers.
Direct Known Subclasses
Ai::AnswerBot::DomainMappers::AnthropicApi::MessageResultToAnswer, Ai::AnswerBot::DomainMappers::AnthropicApi::ModelToFoobaraModel, Ai::AnswerBot::DomainMappers::AnthropicApi::ModelToModelEnumString, Ai::AnswerBot::DomainMappers::AnthropicApi::QuestionToCreateMessage, Ai::AnswerBot::DomainMappers::ModelToAiService, Ai::AnswerBot::DomainMappers::OllamaApi::ChatCompletionToAnswer, Ai::AnswerBot::DomainMappers::OllamaApi::ModelToFoobaraModel, Ai::AnswerBot::DomainMappers::OllamaApi::ModelToModelEnumString, Ai::AnswerBot::DomainMappers::OllamaApi::QuestionToGenerateChatCompletion, Ai::AnswerBot::DomainMappers::OpenAiApi::ChatCompletionToAnswer, Ai::AnswerBot::DomainMappers::OpenAiApi::ModelToFoobaraModel, Ai::AnswerBot::DomainMappers::OpenAiApi::ModelToModelEnumString, Ai::AnswerBot::DomainMappers::OpenAiApi::QuestionToCreateChatCompletion, Ai::AnswerBot::DomainMappers::ServiceToChatCompletionCommand, Ai::AnswerBot::DomainMappers::ServiceToListModelsCommand
Constant Summary
TruncatedInspect::MAX_LENGTH
Instance Attribute Summary
#is_subcommand
#exception, #outcome
#error_collection
#inputs, #raw_inputs
Class Method Summary
collapse
Instance Method Summary
collapse
#initialize
Methods included from Concern
foobara_class_methods_module_for, foobara_concern?, included
#domain_map, #domain_map!, #run_mapped_subcommand!
#run_subcommand!, #subcommand?
#load_entities, #load_records
#auto_detect_current_transactions, #commit_transaction, #open_transaction, #opened_transactions, #relevant_entity_classes, #rollback_transaction, #transactions
#state_machine
#halt!, #run, #run!, #run_execute, #succeed, #success?, #validate, #validate_records
#initialize
#cast_and_validate_inputs, #initialize, #method_missing, #respond_to_missing?, #respond_to_missing_for_inputs?
#inspect, truncating
Class Method Details
.applicable?(from_value, to_value) ⇒ Boolean
43
44
45
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 43
def applicable?(from_value, to_value)
matches?(from_type, from_value) && matches?(to_type, to_value)
end
|
.applicable_score(from_value, to_value) ⇒ Object
51
52
53
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 51
def applicable_score(from_value, to_value)
[*match_score(from_type, from_value), *match_score(to_type, to_value)].sum
end
|
.args_to_type(*args, **opts, &block) ⇒ Object
TODO: should this be somewhere more general-purpose?
85
86
87
88
89
90
91
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 85
def args_to_type(*args, **opts, &block)
if args.size == 1 && opts.empty? && block.nil?
object_to_type(args.first)
else
domain.foobara_type_from_declaration(*args, **opts, &block)
end
end
|
.foobara_on_register ⇒ Object
9
10
11
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 9
def foobara_on_register
foobara_domain.new_mapper_registered!
end
|
.from ⇒ Object
A bit hacky because Command only supports attributes inputs at the moment, ugg.
22
23
24
25
26
27
28
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 22
def from(...)
from_type = args_to_type(...)
inputs do
from from_type, :required
end
end
|
.from_type ⇒ Object
35
36
37
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 35
def from_type
inputs_type.element_types[:from]
end
|
.map(value) ⇒ Object
13
14
15
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 13
def map(value)
new(from: value).run
end
|
.map!(value) ⇒ Object
17
18
19
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 17
def map!(value)
new(from: value).run!
end
|
.match_score(type_indicator, value) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 55
def match_score(type_indicator, value)
return 1 if type_indicator.nil? || value.nil?
return 20 if type_indicator == value
type = object_to_type(type_indicator)
return 0 if type.nil?
return 9 if type == value
return 5 if type.applicable?(value) && type.process_value(value).success?
if value.is_a?(Types::Type)
if !value.registered? && !type.registered?
if value.declaration_data == type.declaration_data
8
end
end
else
value_type = object_to_type(value)
if value_type
if matches?(type, value_type)
6
end
end
end
end
|
.matches?(type_indicator, value) ⇒ Boolean
47
48
49
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 47
def matches?(type_indicator, value)
match_score(type_indicator, value)&.>(0)
end
|
.object_to_type(object) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 93
def object_to_type(object)
if object
if object.is_a?(::Class)
if object < Foobara::Model
object.model_type
elsif object < Foobara::Command
object.inputs_type
else
domain.foobara_type_from_declaration(object)
end
else
case object
when Types::Type
object
when ::Symbol
domain.foobara_lookup_type(object)
end
end
end
end
|
.to ⇒ Object
30
31
32
33
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 30
def to(...)
result_type = args_to_type(...)
result(result_type)
end
|
.to_type ⇒ Object
39
40
41
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 39
def to_type
result_type
end
|
Instance Method Details
#execute ⇒ Object
115
116
117
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 115
def execute
map
end
|
#from ⇒ Object
119
120
121
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 119
def from
inputs[:from]
end
|
#map ⇒ Object
123
124
125
126
127
|
# File 'foobara-0.0.110/projects/domain_mapper/src/domain_mapper.rb', line 123
def map
raise "subclass responsibility"
end
|