class Silva::System::Base
Provides basic utility functions.
Constants
- DEFAULT_PARAMS
Default parameters can be specified for each system.
- REQUIRED_PARAMS
Some parameters must be given
Public Class Methods
new(options)
click to toggle source
Create the given system with relevant options.
@param [Hash] options Parameters relevant to the given system.
# File lib/silva/system/base.rb, line 17 def initialize(options) @system_name = self.class.name.split('::').last.downcase.to_sym options = self.class::DEFAULT_PARAMS.merge(options) params_satisfied?(options) options.each {|param, val| set_param(param, val) } end
Public Instance Methods
to(target_system_name, options = nil)
click to toggle source
Transforms the base system to a different system.
@param [Symbol] target_system_name The system to convert to. @param [Hash] options Parameters relevant to the target system. @return [Silva::System] A new location system of type target_system_name.
# File lib/silva/system/base.rb, line 30 def to(target_system_name, options = nil) return self if target_system_name == @system_name to_method = "to_#{target_system_name}".to_sym unless respond_to?(to_method, true) raise Silva::InvalidTransformError, "#{@system_name} cannot be transformed to #{target_system_name}." end send(to_method, options) end
Private Instance Methods
params_satisfied?(options)
click to toggle source
# File lib/silva/system/base.rb, line 49 def params_satisfied?(options) raise Silva::InsufficientParamsError unless self.class::REQUIRED_PARAMS & options.keys == self.class::REQUIRED_PARAMS end
set_param(param, val)
click to toggle source
Set the params given in options, if they pass validation.
# File lib/silva/system/base.rb, line 42 def set_param(param, val) val_method = "validate_#{param}".to_sym raise Silva::InvalidParamError, "Invalid param: #{param}." unless respond_to?(val_method, true) raise Silva::InvalidParamValueError, "Invalid #{param}: #{val}." unless (send(val_method, val)) instance_variable_set("@#{param}", val) end