Module: Snapi::Capability::ClassMethods
- Defined in:
- lib/snapi/capability.rb
Class Attribute Summary collapse
-
.functions ⇒ Object
Returns the value of attribute functions.
-
.library_class ⇒ Object
Returns the value of attribute library_class.
Instance Method Summary collapse
-
#function(name) ⇒ Object
DSL setter to add a function to the @functions hash.
-
#functions ⇒ Object
Getter to query the internally tracked list of supported functions.
-
#library(klass = self) ⇒ Object
DSL Setter to define the ruby class (or module) which contains methods that should map to the function names in the @functions hash.
-
#library_class ⇒ Object
Getter to query the class which is responsible for having methods which map to the names of the functions.
-
#namespace ⇒ Object
Convert the class name to a snake-cased symbol namespace representation of class name for use in namespacing.
-
#run_function(function, args) ⇒ Object
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.
-
#to_hash ⇒ Object
Helper to conver the class itself to a hash representation including the functions hash keyed off the namespace.
-
#valid_function_call?(function, args) ⇒ Boolean
Helper to check if a function call to the capability would be valid with the given arguments.
-
#valid_library_class? ⇒ Boolean
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.
Class Attribute Details
.functions ⇒ Object
Returns the value of attribute functions.
23 24 25 |
# File 'lib/snapi/capability.rb', line 23 def functions @functions end |
.library_class ⇒ Object
Returns the value of attribute library_class.
23 24 25 |
# File 'lib/snapi/capability.rb', line 23 def library_class @library_class end |
Instance Method Details
#function(name) ⇒ Object
DSL setter to add a function to the @functions hash
47 48 49 50 51 52 53 54 55 |
# 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 ⇒ Object
Getter to query the internally tracked list of supported functions.
30 31 32 |
# File 'lib/snapi/capability.rb', line 30 def functions @functions || {} end |
#library(klass = self) ⇒ Object
DSL Setter to define the ruby class (or module) which contains methods that should map to the function names in the @functions hash
63 64 65 |
# File 'lib/snapi/capability.rb', line 63 def library(klass=self) @library_class = klass end |
#library_class ⇒ Object
Getter to query the class which is responsible for having methods which map to the names of the functions.
38 39 40 |
# File 'lib/snapi/capability.rb', line 38 def library_class @library_class || self end |
#namespace ⇒ Object
Convert the class name to a snake-cased symbol namespace representation of class name for use in namespacing
72 73 74 75 76 77 78 79 |
# 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) ⇒ Object
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
111 112 113 114 115 |
# File 'lib/snapi/capability.rb', line 111 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 ⇒ Object
Helper to conver the class itself to a hash representation including the functions hash keyed off the namespace
85 86 87 88 89 90 91 |
# File 'lib/snapi/capability.rb', line 85 def to_hash fn_hash = {} functions.each {|k,v| fn_hash[k] = v.to_hash } if functions { self.namespace => fn_hash } end |
#valid_function_call?(function, args) ⇒ Boolean
Helper to check if a function call to the capability would be valid with the given arguments
99 100 101 102 |
# File 'lib/snapi/capability.rb', line 99 def valid_function_call?(function, args) return false unless functions[function] functions[function].valid_args?(args) end |
#valid_library_class? ⇒ Boolean
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
122 123 124 125 126 127 |
# File 'lib/snapi/capability.rb', line 122 def valid_library_class? self.functions.keys.each do |function_name| return false unless library_class.methods.include?(function_name) end true end |