Module: Fear::Extractor Private

Defined in:
lib/fear/extractor.rb,
lib/fear/extractor/grammar.rb,
lib/fear/extractor/matcher.rb,
lib/fear/extractor/pattern.rb,
lib/fear/extractor/any_matcher.rb,
lib/fear/extractor/matcher/and.rb,
lib/fear/extractor/array_matcher.rb,
lib/fear/extractor/value_matcher.rb,
lib/fear/extractor/extractor_matcher.rb,
lib/fear/extractor/array_head_matcher.rb,
lib/fear/extractor/empty_list_matcher.rb,
lib/fear/extractor/identifier_matcher.rb,
lib/fear/extractor/array_splat_matcher.rb,
lib/fear/extractor/typed_identifier_matcher.rb,
lib/fear/extractor/named_array_splat_matcher.rb,
lib/fear/extractor/anonymous_array_splat_matcher.rb

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defined Under Namespace

Modules: Grammar Classes: AnonymousArraySplatMatcher, AnyMatcher, ArrayHeadMatcher, ArrayMatcher, ArraySplatMatcher, EmptyListMatcher, ExtractorMatcher, IdentifierMatcher, Matcher, NamedArraySplatMatcher, Pattern, TypedIdentifierMatcher, ValueMatcher

Constant Summary collapse

ExtractorNotFound =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Class.new(Error)
EXTRACTOR_NOT_FOUND =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

proc do |klass|
  raise ExtractorNotFound, 'could not find extractor for ' + klass.inspect
end
BUILD_EXTRACTOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

proc do |key, extractor|
  Fear.matcher do |m|
    case key
    when String
      m.case(Module, ->(lookup) { lookup.to_s == key }) { extractor }
      m.case(String, key) { extractor }
    when Module
      m.case(Module, ->(lookup) { lookup <= key }) { extractor }
      m.case(String, key.to_s) { extractor }
    else
      m.case(key) { extractor } # may it be useful to register other types of keys? lambda?
    end
  end
end

Class Method Summary collapse

Class Method Details

.find_extractor(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • klass (Class, String)


35
36
37
# File 'lib/fear/extractor.rb', line 35

def find_extractor(klass)
  @registry.call_or_else(klass, &EXTRACTOR_NOT_FOUND)
end

.register_extractor(*names, extractor) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Register extractor for given class

Examples:

register_extractor(Fear::Some, Fear.case(Fear::Some) { |some| some.get }.lift)

register_extractor(User, Fear.case(User) { |user|} [user.id, user.email] , )

registering an alias. Alias should be CamelCased string

register_extractor(Fear::Some, 'Some', Fear.case(Fear::Some) { |some| some.get }.lift)

# no you can extract Fear::Some's using Some alias
m.case(Fear['Some(value : Integer)']) { |value:| value * 2 }


56
57
58
59
60
61
62
63
64
65
# File 'lib/fear/extractor.rb', line 56

def register_extractor(*args)
  *keys, extractor = *args

  @mutex.synchronize do
    keys.uniq.each do |key|
      @registry = BUILD_EXTRACTOR.call(key, extractor).or_else(@registry)
    end
  end
  self
end