Class: PatternRuby::EntityTypes::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/pattern_ruby/entity_types.rb

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



38
39
40
41
# File 'lib/pattern_ruby/entity_types.rb', line 38

def initialize
  @types = BUILT_IN.dup
  @mutex = Mutex.new
end

Instance Method Details

#get(name) ⇒ Object



49
50
51
# File 'lib/pattern_ruby/entity_types.rb', line 49

def get(name)
  @mutex.synchronize { @types[name.to_sym] }
end

#parse(name, raw_value) ⇒ Object



58
59
60
61
62
# File 'lib/pattern_ruby/entity_types.rb', line 58

def parse(name, raw_value)
  type = get(name)
  return raw_value unless type && type[:parser]
  type[:parser].call(raw_value)
end

#pattern_for(name) ⇒ Object



53
54
55
56
# File 'lib/pattern_ruby/entity_types.rb', line 53

def pattern_for(name)
  type = get(name)
  type && type[:pattern]
end

#register(name, pattern: nil, parser: nil) ⇒ Object



43
44
45
46
47
# File 'lib/pattern_ruby/entity_types.rb', line 43

def register(name, pattern: nil, parser: nil)
  @mutex.synchronize do
    @types[name.to_sym] = { pattern: pattern, parser: parser }
  end
end