Module: Strum::Cache
- Extended by:
- Dry::Configurable
- Defined in:
- lib/strum/cache.rb,
lib/strum/cache/version.rb
Overview
rubocop: disable Style/Documentation
Defined Under Namespace
Classes: Error
Constant Summary collapse
- VERSION =
"1.0.0"
Class Method Summary collapse
-
.const_missing(resource_name) ⇒ Object
rubocop: disable Metrics/AbcSize,Metrics/MethodLength.
Class Method Details
.const_missing(resource_name) ⇒ Object
rubocop: disable Metrics/AbcSize,Metrics/MethodLength
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/strum/cache.rb', line 24 def self.const_missing(resource_name) # Strum::Cache::Entity::Find.(id) # Strum::Cache::Entity::Search.(params) # Strum::Cache::Entity::Put.(params) Module.new do const_set(:Push, Class.new do include Strum::Service define_method :call do redis_connection.hset(id, input) redis_connection.expire(id, redis_resource_expire) end define_method :audit do required(:id) end private define_method :resource_code do resource_name.downcase.to_s end define_method :redis_connection do Strum::CacheUtils::RedisStorage.const_get(resource_code.capitalize, false).instance.redis end def redis_resource_expire ENV.fetch("#{resource_code.to_s.upcase}_CACHE_EXPIRE", ENV.fetch("CACHE_EXPIRE", 60 * 15)) end end) const_set(:Find, Class.new do include Strum::Service define_method :call do if (entity = from_cache_by_id(input) || find_by_id(input)) output(entity) else not_found end end define_method :audit do self.input = input.to_s.to_i add_error(:id, :invalid) unless input.positive? end private define_method :resource_code do resource_name.downcase end define_method :from_cache_by_id do |resource_id| Strum::CacheUtils::Redis.call(resource_code: resource_code, resource_id: resource_id) end define_method :find_by_id do |resource_id| Strum::CacheUtils::Find.call(resource_code: resource_code, resource_id: resource_id) do |m| m.success { |result| result } m.failure do |errors| add_errors(errors) nil end end end define_method :not_found do add_error(:entity, :not_found) end end) const_set(:Search, Class.new do include Strum::Service define_method :call do if (entity = from_cache_by_params || search_by_params) output(entity) else not_found end end define_method :audit do required end private define_method :resource_code do resource_name.downcase end define_method :from_cache_by_params do false end define_method :search_by_params do Strum::CacheUtils::Search.call(resource_code: resource_code, params: input) do |m| m.success { |result| result } m.failure do |errors| add_errors(errors) nil end end end define_method :not_found do add_error(:entity, :not_found) end end) end end |