Module: Polyseerio::Helper
- Defined in:
- lib/helper.rb
Overview
General helper functions for SDK
Constant Summary collapse
- DEFAULT_REQUIRE_DIRECTORY_OPTIONS =
{ exclude: ['index'].freeze }.freeze
Class Method Summary collapse
-
.add_to_proc_map(*args) ⇒ Object
Adds a proc method to an accumulator by its name.
-
.attach_to_instance!(*args) ⇒ Object
Used to attach a method to an instance.
-
.defaults(options, *defaults) ⇒ Object
Generates default options.
-
.dir_proc_map(dir, mod) ⇒ Object
Given directory and module a map of module procs will be created.
-
.dir_to_path_map(directory, options = {}) ⇒ Object
Maps filename to file path.
-
.format_payload(payload) ⇒ Object
Format a resouce payload for API consumption.
-
.identity(value) ⇒ Object
Simple identity function.
-
.memoize_function(func, get_key) ⇒ Object
A memoize function that has an optional cache key.
-
.ms_to_seconds(ms) ⇒ Object
Simple conversion to seconds from ms.
-
.rekey(hash, map) ⇒ Object
Rekey a hash using a remapping hash.
-
.require_directory(directory, options = {}) ⇒ Object
Load an include an entire directory.
-
.resolve_token(options) ⇒ Object
Attempt to geta token.
-
.resource_to_type(resource) ⇒ Object
Convert resource string to resource type.
Class Method Details
.add_to_proc_map(*args) ⇒ Object
Adds a proc method to an accumulator by its name. TODO: this is really a dir func map not always a proc map.
27 28 29 30 31 32 33 34 35 36 37 |
# 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) ⇒ Object
Used to attach a method to an instance
132 133 134 135 136 137 138 |
# 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) ⇒ Object
Generates default options.
121 122 123 124 125 126 127 128 129 |
# File 'lib/helper.rb', line 121 def self.defaults(, *defaults) result = {} defaults.reverse_each do |opts| result.merge!(opts) end result.merge!() end |
.dir_proc_map(dir, mod) ⇒ Object
Given directory and module a map of module procs will be created. TODO: this is really a dir func map not a proc map
17 18 19 20 21 22 23 |
# 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 = {}) ⇒ Object
Maps filename to file path
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/helper.rb', line 71 def self.dir_to_path_map(directory, = {}) directory = "#{directory}/*#{[:extension]}" = defaults(, DEFAULT_REQUIRE_DIRECTORY_OPTIONS) paths = Dir[directory].select do |file| name = File.basename(file, '.*') ![: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) ⇒ Object
Format a resouce payload for API consumption
103 104 105 |
# File 'lib/helper.rb', line 103 def self.format_payload(payload) { data: { attributes: payload.clone } } end |
.identity(value) ⇒ Object
Simple identity function.
66 67 68 |
# File 'lib/helper.rb', line 66 def self.identity(value) proc { |x| x }.call(value) end |
.memoize_function(func, get_key) ⇒ Object
A memoize function that has an optional cache key.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# 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) ⇒ Object
Simple conversion to seconds from ms.
11 12 13 |
# File 'lib/helper.rb', line 11 def self.ms_to_seconds(ms) ms / 1000.0 end |
.rekey(hash, map) ⇒ Object
Rekey a hash using a remapping hash.
141 142 143 144 145 146 147 148 149 150 151 |
# 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 = {}) ⇒ Object
Load an include an entire directory.
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/helper.rb', line 91 def self.require_directory(directory, = {}) directory = "#{directory}/*#{[:extension]}" = defaults(, DEFAULT_REQUIRE_DIRECTORY_OPTIONS) Dir[directory].select do |file| pathname = Pathname.new(file) ![:exclude].include? pathname.basename end end |
.resolve_token(options) ⇒ Object
Attempt to geta token.
108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/helper.rb', line 108 def self.resolve_token() return [:token] unless [:token].nil? if ENV.key? [:token_env] value = ENV.fetch([:token_env], nil) return value unless value.nil? end nil end |
.resource_to_type(resource) ⇒ Object
Convert resource string to resource type.
40 41 42 |
# File 'lib/helper.rb', line 40 def self.resource_to_type(resource) Inflection.singular(resource) end |