Module: Modulator
- Defined in:
- lib/modulator.rb,
lib/modulator/version.rb
Constant Summary collapse
- LAMBDAS =
{}
- VERSION =
"0.3.0"
Class Attribute Summary collapse
-
.registering_module ⇒ Object
Returns the value of attribute registering_module.
-
.stack ⇒ Object
Returns the value of attribute stack.
Class Method Summary collapse
-
.calculate_registry_key(mod, module_method) ⇒ Object
lambda definition reference.
-
.generate_endoints ⇒ Object
delegate to stack instance.
-
.init_stack(app_name: Pathname.getwd.basename.to_s, s3_bucket:, **stack_opts) ⇒ Object
init humidifier stack instance.
-
.register(lambda_def, **opts) ⇒ Object
opts are for overrides.
- .register_from_hash(hash) ⇒ Object
- .register_from_module(mod, **opts) ⇒ Object
-
.set_env_values(lambda_def) ⇒ Object
local gateway helper.
- .wrap_with(wrapper_mod, only: nil, except: nil) ⇒ Object
Class Attribute Details
.registering_module ⇒ Object
Returns the value of attribute registering_module.
12 13 14 |
# File 'lib/modulator.rb', line 12 def registering_module @registering_module end |
.stack ⇒ Object
Returns the value of attribute stack.
12 13 14 |
# File 'lib/modulator.rb', line 12 def stack @stack end |
Class Method Details
.calculate_registry_key(mod, module_method) ⇒ Object
lambda definition reference
98 99 100 101 |
# File 'lib/modulator.rb', line 98 def calculate_registry_key(mod, module_method) module_names = mod.to_s.split('::').map(&:downcase) "#{module_names.join('-')}-#{module_method}" end |
.generate_endoints ⇒ Object
delegate to stack instance
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/modulator.rb', line 136 def generate_endoints # add lambdas to stack puts 'Generating endpoints' LAMBDAS.each do |name, config| puts "- adding #{config.dig(:module, :name)}.#{config.dig(:module, :method)} to #{config.dig(:gateway, :path)}" stack.add_lambda_endpoint( gateway: config[:gateway], mod: config[:module], wrapper: config[:wrapper] || {}, env: config[:env] || {}, settings: config[:settings] || {} ) end end |
.init_stack(app_name: Pathname.getwd.basename.to_s, s3_bucket:, **stack_opts) ⇒ Object
init humidifier stack instance
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/modulator.rb', line 118 def init_stack(app_name: Pathname.getwd.basename.to_s, s3_bucket:, **stack_opts) stack = StackBuilder.init({ app_name: app_name.camelize, s3_bucket: s3_bucket, }.merge(stack_opts)) # validate stack # puts 'Validating stack' # puts '- it is valid' if stack.valid? self.stack = stack generate_endoints if LAMBDAS.any? # return instance stack end |
.register(lambda_def, **opts) ⇒ Object
opts are for overrides
15 16 17 18 19 20 21 22 23 |
# File 'lib/modulator.rb', line 15 def register(lambda_def, **opts) # opts are for overrides if lambda_def.is_a?(Hash) register_from_hash(lambda_def) else register_from_module(lambda_def, **opts) self.registering_module = lambda_def self end end |
.register_from_hash(hash) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/modulator.rb', line 37 def register_from_hash(hash) LAMBDAS[hash[:name]] = { name: hash[:name], gateway: hash[:gateway], module: hash[:module], wrapper: hash[:wrapper] || {}, env: hash[:env] || {}, settings: hash[:settings] || {} } end |
.register_from_module(mod, **opts) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/modulator.rb', line 48 def register_from_module(mod, **opts) mod.singleton_methods.sort.each do |module_method| module_name = mod.to_s module_names = module_name.split('::').map(&:downcase) verb = 'GET' path_fragments = module_names.dup # process parameters # method(a, b, c = 1, *args, d:, e: 2, **opts) # [[:req, :a], [:req, :b], [:opt, :c], [:rest, :args], [:keyreq, :d], [:key, :e], [:keyrest, :opts]] mod.method(module_method).parameters.each do |param| param_type = param[0] param_name = param[1] # collect required params path_fragments << ":#{param_name}" if param_type == :req # post if we have optional key param, ie. pet: {} verb = 'POST' if param_type == :key end # delete is a special case based on method name verb = 'DELETE' if %w[destroy delete remove implode].include? module_method.to_s # finalize path path_fragments << module_method path = path_fragments.join('/') registry_key = calculate_registry_key(mod, module_method) register_from_hash( { name: registry_key, gateway: { verb: opts.dig(module_method, :gateway, :verb) || verb, path: opts.dig(module_method, :gateway, :path) || path }, module: { name: module_name, method: module_method.to_s, path: module_names.join('/') # file name }, wrapper: opts.dig(module_method, :wrapper) || opts[:wrapper], env: opts.dig(module_method, :env), settings: opts.dig(module_method, :settings) } ) end end |
.set_env_values(lambda_def) ⇒ Object
local gateway helper
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/modulator.rb', line 104 def set_env_values(lambda_def) # remove wrapper if already set %i(name method path).each{|key| ENV.delete("wrapper_#{key}")} # set env for each group %i(module gateway wrapper env).each do |group_key| lambda_def[group_key]&.each do |key, value| key = "#{group_key}_#{key}" if group_key != :env ENV[key.to_s] = value.to_s end end end |
.wrap_with(wrapper_mod, only: nil, except: nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/modulator.rb', line 25 def wrap_with(wrapper_mod, only: nil, except: nil) registering_module.singleton_methods.sort.each do |module_method| next if only && !Array(only).include?(module_method) next if except && Array(except).include?(module_method) LAMBDAS[calculate_registry_key(registering_module, module_method)][:wrapper] = { name: wrapper_mod.to_s, path: wrapper_mod.to_s.split('::').map(&:downcase).join('/'), # file name method: 'call' } end end |