Module: Snapi::Capability::ClassMethods

Defined in:
lib/snapi/capability.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.functionsObject

Returns the value of attribute functions.



23
24
25
# File 'lib/snapi/capability.rb', line 23

def functions
  @functions
end

.library_classObject

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

#functionsObject

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_classObject

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

#namespaceObject

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



109
110
111
112
113
# 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_hashObject

Helper to conver the class itself to a hash representation including the functions hash keyed off the namespace



85
86
87
88
89
# 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) ⇒ Boolean

Helper to check if a function call to the capability would be valid with the given arguments



97
98
99
100
# 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?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



120
121
122
123
124
125
# 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