Class: Factbase::Fact

Inherits:
Object
  • Object
show all
Defined in:
lib/factbase/fact.rb

Overview

Fact.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2024 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(mutex, map) ⇒ Fact

Returns a new instance of Fact.



30
31
32
33
# File 'lib/factbase/fact.rb', line 30

def initialize(mutex, map)
  @mutex = mutex
  @map = map
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/factbase/fact.rb', line 35

def method_missing(*args)
  k = args[0].to_s
  if k.end_with?('=')
    k = k[0..-2]
    @mutex.synchronize do
      @map[k] = [] if @map[k].nil?
      @map[k] << args[1]
    end
    nil
  elsif k == '[]'
    kk = args[1].to_s
    @mutex.synchronize do
      @map[kk] = [] if @map[kk].nil?
    end
    @map[kk]
  else
    v = @map[k]
    raise "Can't find '#{k}'" if v.nil?
    v[0]
  end
end

Instance Method Details

#respond_to?(_method, _include_private = false) ⇒ Boolean

rubocop:disable Style/OptionalBooleanParameter

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/factbase/fact.rb', line 58

def respond_to?(_method, _include_private = false)
  # rubocop:enable Style/OptionalBooleanParameter
  true
end

#respond_to_missing?(_method, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/factbase/fact.rb', line 63

def respond_to_missing?(_method, _include_private = false)
  true
end