module Patch::Config

Deal with config files, hashes

Public Instance Methods

to_hub(nodes_config, options = {}) click to toggle source

@param [Hash] nodes_config @param [Hash] options @option options [IO] :log @return [Hub]

# File lib/patch/config.rb, line 12
def to_hub(nodes_config, options = {})
  log = Log.new(options.fetch(:log, $>)) unless options[:log].nil?
  nodes = to_nodes(nodes_config, :log => log)
  patches = to_patches(nodes, options[:patches]) unless options[:patches].nil?
  Hub.new(:log => log, :patches => patches)
end
to_node_maps(nodes, config) click to toggle source

Instantiate Node::Map objects given a map config hash @param [NodeContainer] nodes @param [Hash] config @return [Array<Node::Map>]

# File lib/patch/config.rb, line 47
def to_node_maps(nodes, config)
  config.map { |from, to| get_node_map(nodes, from, to) }
end
to_nodes(config, options = {}) click to toggle source

Instantiate node objects from the given node config or config file @param [File, Hash, String] config @param [Hash] options @option options [Log] :log @return [Node::Container]

# File lib/patch/config.rb, line 37
def to_nodes(config, options = {})
  config = ensure_hash(config)
  node_array = config[:nodes].map { |node_config| to_node(node_config, options) }
  Node::Container.new(node_array)
end
to_patches(nodes, config) click to toggle source

Instantiate patch objects from the given patch config file, filename or hash @param [NodeContainer] nodes @param [File, Hash, String] config @return [Array<Patch>]

# File lib/patch/config.rb, line 23
def to_patches(nodes, config)
  config = ensure_hash(config)
  patches = []
  config[:patches].each do |name, patch_config|
    patches << to_patch(name, nodes, patch_config)
  end
  patches
end

Private Instance Methods

deep_freeze_config(container) click to toggle source

@param [Enumerable] container @return [Enumerable]

# File lib/patch/config.rb, line 115
def deep_freeze_config(container)
  container.freeze
  values = container.respond_to?(:values) ? container.values : container
  enums = values.select { |item| item.kind_of?(Array) || item.kind_of?(Hash) }
  enums.each { |item| deep_freeze_config(item) }
  container
end
ensure_hash(object) click to toggle source

Given a file name, file or hash, populate a config hash and freeze it @param [File, Hash, String] object @return [Hash]

# File lib/patch/config.rb, line 104
def ensure_hash(object)
  hash = if (config_file = get_config_file(object)).nil?
    object
  else
    YAML.load_file(config_file)
  end
  deep_freeze_config(hash) unless hash.nil?
end
get_config_file(object) click to toggle source

@param [File, Hash, String] object @return [File, String]

# File lib/patch/config.rb, line 95
def get_config_file(object)
  case object
  when File, String then object
  end
end
get_node_map(nodes, from, to) click to toggle source

@param [NodeContainer] nodes @param [Array<Object>, Object] from (id) @param [Array<Object>, Object] to (id) @return [Node::Map]

# File lib/patch/config.rb, line 57
def get_node_map(nodes, from, to)
  from_nodes = get_nodes(nodes, from)
  to_nodes = get_nodes(nodes, to)
  Node::Map.new(from_nodes, to_nodes)
end
get_nodes(nodes, ids) click to toggle source

@param [NodeContainer] nodes @param [Array<Object>, Object] from (id) @return [Array<Patch::IO::MIDI, Patch::IO::OSC, Patch::IO::Websocket>]

# File lib/patch/config.rb, line 66
def get_nodes(nodes, ids)
  ids = [ids].flatten
  ids.map { |id| nodes.find_by_id(id) }
end
to_node(node_config, options = {}) click to toggle source

Instantiate a node from the given node config @param [Hash] config @param [Hash] options @option options [Log] :log @return [Patch::IO::MIDI, Patch::IO::OSC, Patch::IO::Websocket]

# File lib/patch/config.rb, line 76
def to_node(node_config, options = {})
  module_key = node_config[:type].to_sym
  mod = IO::Module.find_by_key(module_key)
  mod.new_from_config(node_config, :log => options[:log])
end
to_patch(name, nodes, config) click to toggle source

Instantiate a patch object for the given config hash @param [Symbol, String] name @param [NodeContainer] nodes @param [Hash] config @return [Patch]

# File lib/patch/config.rb, line 87
def to_patch(name, nodes, config)
  actions = config[:actions] || config[:action]
  maps = to_node_maps(nodes, config[:node_map])
  Patch.new(name, maps, actions)
end