module Snapi::Capability::ClassMethods
Attributes
Public Instance Methods
DSL setter to add a function to the @functions hash
@params name, Name of function being defined @params Block to modify function @returns Hash
of function data
# File lib/snapi/capability.rb, line 47 def function(name) raise InvalidFunctionNameError unless Validator.valid_input?(:snapi_function_name, name) fn = Function.new if block_given? yield(fn) end @functions ||= {} @functions[name] = fn end
DSL Setter to define the ruby class (or module) which contains methods that should map to the function names in the @functions hash
@params klass, Class which default to self @returns Class
# File lib/snapi/capability.rb, line 63 def library(klass=self) @library_class = klass end
Getter to query the class which is responsible for having methods which map to the names of the functions.
@returns Class, @library_class || self
# File lib/snapi/capability.rb, line 38 def library_class @library_class || self end
Convert the class name to a snake-cased symbol namespace representation of class name for use in namespacing
@returns Symbol, snake_cased
# File lib/snapi/capability.rb, line 72 def namespace self.name .split('::').last .scan(/([A-Z]+[a-z0-9]+)/) .flatten.map(&:downcase) .join("_") .to_sym end
Accepts arguments and sends the args to the library class version of the function assuming that the function has been declared and the library class also includes that function
@param function Symbolic name to reference the function out of the @functions hash @param args hash of arguments @returns God only knows, my friend, God only know
# File lib/snapi/capability.rb, line 109 def run_function(function,args) raise InvalidFunctionCallError unless valid_function_call?(function, args) raise LibraryClassMissingFunctionError unless valid_library_class? library_class.send(function,args) end
Helper to conver the class itself to a hash representation including the functions hash keyed off the namespace
@returns Hash
# File lib/snapi/capability.rb, line 85 def to_hash fn_hash = {} functions.each {|k,v| fn_hash[k] = v.to_hash } if functions fn_hash end
Helper to check if a function call to the capability would be valid with the given arguments
@param function Symbolic name to reference the function out of the @functions hash @param args hash of arguments @returns Boolean
# File lib/snapi/capability.rb, line 97 def valid_function_call?(function, args) return false unless functions[function] functions[function].valid_args?(args) end
Test if the set library class is valid based on mapping the keys of the @functions hash against the methods available to the @library_class
@returns Boolean, true if @library_class offers all the methods needed
# File lib/snapi/capability.rb, line 120 def valid_library_class? self.functions.keys.each do |function_name| return false unless library_class.methods.include?(function_name) end true end