module Polyseerio::Helper
General helper functions for SDK
Constants
- DEFAULT_REQUIRE_DIRECTORY_OPTIONS
Public Class Methods
add_to_proc_map(*args)
click to toggle source
Adds a proc method to an accumulator by its name. TODO: this is really a dir func map not always a proc map.
# File lib/helper.rb, line 27 def self.add_to_proc_map(*args) proc do |mod, (name, _), acc| if mod.respond_to? name result = mod.send name acc[name] = result unless result.nil? end acc end.curry.call(*args) end
attach_to_instance!(*args)
click to toggle source
Used to attach a method to an instance
# File lib/helper.rb, line 132 def self.attach_to_instance!(*args) lambda do |instance, (key, value)| instance.instance_eval do define_singleton_method(key, proc { value }) end end.curry.call(*args) end
defaults(options, *defaults)
click to toggle source
Generates default options.
# File lib/helper.rb, line 121 def self.defaults(options, *defaults) result = {} defaults.reverse_each do |opts| result.merge!(opts) end result.merge!(options) end
dir_proc_map(dir, mod)
click to toggle source
Given directory and module a map of module procs will be created. TODO: this is really a dir func map not a proc map
# File lib/helper.rb, line 17 def self.dir_proc_map(dir, mod) map = Polyseerio::Helper.dir_to_path_map dir map.each { |(_, path)| require path } map.each_with_object({}, &Helper.add_to_proc_map(mod)) end
dir_to_path_map(directory, options = {})
click to toggle source
Maps filename to file path
# File lib/helper.rb, line 71 def self.dir_to_path_map(directory, options = {}) directory = "#{directory}/*#{options[:extension]}" options = defaults(options, DEFAULT_REQUIRE_DIRECTORY_OPTIONS) paths = Dir[directory].select do |file| name = File.basename(file, '.*') !options[:exclude].include? name end paths.each_with_object({}) do |file, acc| name = File.basename(file, '.*') acc[name.to_sym] = file acc end end
format_payload(payload)
click to toggle source
Format a resouce payload for API consumption
# File lib/helper.rb, line 103 def self.format_payload(payload) { data: { attributes: payload.clone } } end
identity(value)
click to toggle source
Simple identity function.
# File lib/helper.rb, line 66 def self.identity(value) proc { |x| x }.call(value) end
memoize_function(func, get_key)
click to toggle source
A memoize function that has an optional cache key.
# File lib/helper.rb, line 45 def self.memoize_function(func, get_key) lambda do results = {} proc do |*args| key = get_key.call(*args) # TODO: assert key is sym? if results.key? key results.fetch key else result = func.call(*args) results[key] = result result end end end.call end
ms_to_seconds(ms)
click to toggle source
Simple conversion to seconds from ms.
# File lib/helper.rb, line 11 def self.ms_to_seconds(ms) ms / 1000.0 end
rekey(hash, map)
click to toggle source
Rekey a hash using a remapping hash.
# File lib/helper.rb, line 141 def self.rekey(hash, map) hash.each_with_object({}) do |(key, value), acc| if map.key? key acc[map[key]] = value else acc[key] = value end acc end end
require_directory(directory, options = {})
click to toggle source
Load an include an entire directory.
# File lib/helper.rb, line 91 def self.require_directory(directory, options = {}) directory = "#{directory}/*#{options[:extension]}" options = defaults(options, DEFAULT_REQUIRE_DIRECTORY_OPTIONS) Dir[directory].select do |file| pathname = Pathname.new(file) !options[:exclude].include? pathname.basename end end
resolve_token(options)
click to toggle source
Attempt to geta token.
# File lib/helper.rb, line 108 def self.resolve_token(options) return options[:token] unless options[:token].nil? if ENV.key? options[:token_env] value = ENV.fetch(options[:token_env], nil) return value unless value.nil? end nil end
resource_to_type(resource)
click to toggle source
Convert resource string to resource type.
# File lib/helper.rb, line 40 def self.resource_to_type(resource) Inflection.singular(resource) end