Module: FunctionalLightService
- Defined in:
- lib/functional-light-service.rb,
lib/functional-light-service/action.rb,
lib/functional-light-service/errors.rb,
lib/functional-light-service/context.rb,
lib/functional-light-service/version.rb,
lib/functional-light-service/organizer.rb,
lib/functional-light-service/deprecations.rb,
lib/functional-light-service/configuration.rb,
lib/functional-light-service/functional/enum.rb,
lib/functional-light-service/functional/monad.rb,
lib/functional-light-service/functional/option.rb,
lib/functional-light-service/functional/result.rb,
lib/functional-light-service/functional/result.rb,
lib/functional-light-service/organizer/execute.rb,
lib/functional-light-service/organizer/iterate.rb,
lib/functional-light-service/organizer/reduce_if.rb,
lib/functional-light-service/context/key_verifier.rb,
lib/functional-light-service/localization_adapter.rb,
lib/functional-light-service/organizer/reduce_until.rb,
lib/functional-light-service/organizer/with_reducer.rb,
lib/functional-light-service/organizer/with_callback.rb,
lib/functional-light-service/testing/context_factory.rb,
lib/functional-light-service/organizer/scoped_reducable.rb,
lib/functional-light-service/organizer/with_reducer_factory.rb,
lib/functional-light-service/organizer/with_reducer_log_decorator.rb
Defined Under Namespace
Modules: Action, Deprecations, Enum, Monad, Organizer, Prelude, Testing Classes: Configuration, Context, EnumBuilder, ExpectedKeysNotInContextError, FailWithRollbackError, LocalizationAdapter, Option, PromisedKeysNotInContextError, ReservedKeysInContextError, Result
Constant Summary collapse
- VERSION =
"6.0.0"
Class Method Summary collapse
-
.enum(&block) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity.
-
.impl(enum_type, &block) ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength rubocop:enable Metrics/PerceivedComplexity.
Class Method Details
.enum(&block) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/MethodLength rubocop:disable Metrics/PerceivedComplexity
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/functional-light-service/functional/enum.rb', line 158 def enum(&block) mod = Class.new do # the enum to be built private_class_method :new def self.match(obj, &block) # Binding#receiver: stesso risultato di binding.eval('self') senza eval caller_ctx = block.binding.receiver matcher = self::Matcher.new(obj) matcher.instance_eval(&block) # exhaustiveness check su classi memoizzate: niente split/sort di # stringhe per chiamata covered = matcher.matches.map { |e| e[1] } missing = variant_classes.reject { |klass| covered.include?(klass) } unless missing.empty? missing_names = missing.map { |klass| klass.name.split('::')[-1].to_sym } raise Enum::MatchError, "Match is non-exhaustive, #{missing_names} not covered" end type_matches = matcher.matches.select { |r| r[0].is_a?(r[1]) } type_matches.each do |match| obj, _type, block, args, guard = match return caller_ctx.instance_eval(&block) if args.empty? if args.count != obj.args.count msg = "Pattern (#{args.join(', ')}) must match (#{obj.args.join(', ')})" raise Enum::MatchError, msg end guard_ctx = guard_context(obj, args) return caller_ctx.instance_exec(* obj.wrapped_values, &block) unless guard if guard && guard_ctx.instance_exec(obj, &guard) return caller_ctx.instance_exec(* obj.wrapped_values, &block) end end raise Enum::MatchError, "No match could be made" end def self.variants constants - %i[Matcher MatchError] end def self.variant_classes @variant_classes ||= variants.map { |v| const_get(v) }.freeze end def self.guard_context(obj, args) # Struct.new definisce una classe: va fatto una volta per firma, # non a ogni match con guard @guard_structs ||= {} struct = @guard_structs[args] ||= Struct.new(*args) if obj.is_a?(FunctionalLightService::EnumBuilder::DataType::Binary) struct.new(*obj.value.values) else struct.new(obj.value) end end end enum = EnumBuilder.new(mod) enum.instance_eval(&block) type_variants = mod.constants matcher = Class.new do def initialize(obj) @obj = obj @matches = [] @vars = [] end attr_reader :matches, :vars def where(&guard) guard end type_variants.each do |m| define_method(m) do |guard = nil, &inner_block| raise ArgumentError, "No block given to `#{m}`" if inner_block.nil? params_spec = inner_block.parameters if params_spec.any? { |spec| spec.size < 2 } msg = "Unnamed param found in block parameters: #{params_spec.inspect}" raise ArgumentError, msg end if params_spec.any? { |spec| spec[0] != :req && spec[0] != :opt } msg = "Only :req & :opt params allowed; parameters=#{params_spec.inspect}" raise ArgumentError, msg end args = params_spec.map { |spec| spec[1] } type = mod.const_get(m) guard = nil if guard && !guard.is_a?(Proc) @matches << [@obj, type, inner_block, args, guard] end end end mod.const_set(:Matcher, matcher) type_variants.each do |variant| mod.singleton_class.class_exec do define_method(variant) do |*args| const_get(variant).new(*args) end end end mod end |
.impl(enum_type, &block) ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/MethodLength rubocop:enable Metrics/PerceivedComplexity
281 282 283 284 285 |
# File 'lib/functional-light-service/functional/enum.rb', line 281 def impl(enum_type, &block) enum_type.variants.each do |v| enum_type.const_get(v).class_eval(&block) end end |