Class: Shatter::Service::Function
- Inherits:
-
Object
- Object
- Shatter::Service::Function
- Defined in:
- lib/shatter/service/function.rb
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
- .define_param(name, type:, nullable: true) ⇒ Object
- .invoke ⇒ Object
- .meta ⇒ Object
- .to_typescript ⇒ Object
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(function_params) ⇒ Function
constructor
A new instance of Function.
- #valid_param?(arg) ⇒ Boolean
- #valid_params? ⇒ Boolean
- #value_for_arg(arg) ⇒ Object
Constructor Details
#initialize(function_params) ⇒ Function
Returns a new instance of Function.
20 21 22 |
# File 'lib/shatter/service/function.rb', line 20 def initialize(function_params) @params = function_params end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
18 19 20 |
# File 'lib/shatter/service/function.rb', line 18 def params @params end |
Class Method Details
.define_param(name, type:, nullable: true) ⇒ Object
8 9 10 11 |
# File 'lib/shatter/service/function.rb', line 8 def self.define_param(name, type:, nullable: true) @param_meta ||= {} @param_meta[name] = { name:, type:, nullable: } end |
.invoke ⇒ Object
58 59 60 |
# File 'lib/shatter/service/function.rb', line 58 def self.invoke raise "cant invoke for base function" end |
.meta ⇒ Object
13 14 15 |
# File 'lib/shatter/service/function.rb', line 13 def self. @param_meta || {} end |
.to_typescript ⇒ Object
62 63 64 65 66 67 |
# File 'lib/shatter/service/function.rb', line 62 def self.to_typescript function_nm = Shatter::Service::Base.service_definition.function_collection.to_a.detect do |fn_def| fn_def[1] == self end[0] ERB.new(File.read("#{__dir__}/../../../templates/function_definition.ts.erb")).result(binding) end |
Instance Method Details
#call ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/shatter/service/function.rb', line 24 def call { result: nil, error: "Invalid Parameters" } unless valid_params? { result: nil, error: nil }.merge(invoke.merge(uuid: params[:uuid])) rescue StandardError => e Shatter.logger.error e. Shatter.logger.error e.backtrace { result: nil, error: "Something Went Wrong", uuid: params[:uuid] } end |
#valid_param?(arg) ⇒ Boolean
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/shatter/service/function.rb', line 40 def valid_param?(arg) = self.class.[arg] return false if .nil? => type:, nullable: val = value_for_arg(arg) return nullable if val.nil? return false if type == "string" && !val.is_a?(String) return false if type == "integer" && !val.is_a?(Integer) true end |
#valid_params? ⇒ Boolean
33 34 35 36 37 38 |
# File 'lib/shatter/service/function.rb', line 33 def valid_params? self.class..each_key do |arg| return false unless valid_param?(arg) end true end |
#value_for_arg(arg) ⇒ Object
54 55 56 |
# File 'lib/shatter/service/function.rb', line 54 def value_for_arg(arg) @params[arg.to_s] || @params[arg.to_sym] end |