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/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/verify_call_method_exists.rb,
lib/functional-light-service/organizer/with_reducer_log_decorator.rb
Defined Under Namespace
Modules: Action, Enum, Monad, Organizer, Prelude, Testing Classes: Configuration, Context, EnumBuilder, ExpectedKeysNotInContextError, FailWithRollbackError, LocalizationAdapter, Option, PromisedKeysNotInContextError, ReservedKeysInContextError, Result
Constant Summary collapse
- VERSION =
"0.4.4".freeze
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
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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 |
# File 'lib/functional-light-service/functional/enum.rb', line 131 def enum(&block) mod = Class.new do # the enum to be built private_class_method :new def self.match(obj, &block) caller_ctx = block.binding.eval 'self' matcher = self::Matcher.new(obj) matcher.instance_eval(&block) variants_in_match = matcher.matches.collect do |e| e[1].name.split('::')[-1].to_sym end.uniq.sort variants_not_covered = variants - variants_in_match unless variants_not_covered.empty? raise Enum::MatchError, "Match is non-exhaustive, #{variants_not_covered} 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.count.zero? 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.guard_context(obj, args) if obj.is_a?(FunctionalLightService::EnumBuilder::DataType::Binary) Struct.new(*args).new(*obj.value.values) else Struct.new(*args).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
243 244 245 246 247 248 249 |
# File 'lib/functional-light-service/functional/enum.rb', line 243 def impl(enum_type, &block) enum_type.variants.each do |v| name = "#{enum_type.name}::#{v}" type = Kernel.eval(name) type.class_eval(&block) end end |