module Snapi::Capability::ClassMethods

Attributes

functions[RW]
library_class[RW]

Public Instance Methods

function(name) { |fn| ... } click to toggle source

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
functions() click to toggle source

Getter to query the internally tracked list of supported functions.

@returns Hash, @functions or new Hash

# File lib/snapi/capability.rb, line 30
def functions
  @functions || {}
end
library(klass=self) click to toggle source

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
library_class() click to toggle source

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
namespace() click to toggle source

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
run_function(function,args) click to toggle source

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
to_hash() click to toggle source

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
valid_function_call?(function, args) click to toggle source

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
valid_library_class?() click to toggle source

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