Class: Factbase::Fact
- Inherits:
-
Object
- Object
- Factbase::Fact
- Defined in:
- lib/factbase/fact.rb
Overview
Fact.
- Author
-
Yegor Bugayenko ([email protected])
- Copyright
-
Copyright © 2024 Yegor Bugayenko
- License
-
MIT
Instance Method Summary collapse
-
#initialize(mutex, map) ⇒ Fact
constructor
A new instance of Fact.
-
#method_missing(*args) ⇒ Object
When a method is missing, this method is called.
-
#respond_to?(_method, _include_private = false) ⇒ Boolean
rubocop:disable Style/OptionalBooleanParameter.
- #respond_to_missing?(_method, _include_private = false) ⇒ Boolean
-
#to_s ⇒ String
Convert it to a string.
Constructor Details
#initialize(mutex, map) ⇒ Fact
Returns a new instance of Fact.
32 33 34 35 |
# File 'lib/factbase/fact.rb', line 32 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
When a method is missing, this method is called.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/factbase/fact.rb', line 44 def method_missing(*args) k = args[0].to_s if k.end_with?('=') kk = k[0..-2] raise "Invalid prop name '#{kk}'" unless kk.match?(/^[a-z][_a-zA-Z0-9]*$/) v = args[1] raise "Prop value can't be nil" if v.nil? raise "Prop value can't be empty" if v == '' raise "Prop type '#{v.class}' is not allowed" unless [String, Integer, Float, Time].include?(v.class) v = v.utc if v.is_a?(Time) @mutex.synchronize do before = @map[kk] return if before == v if before.nil? @map[kk] = v return end @map[kk] = [@map[kk]] unless @map[kk].is_a?(Array) @map[kk] << v end nil elsif k == '[]' @map[args[1].to_s] else v = @map[k] if v.nil? raise "Can't get '#{k}', the fact is empty" if @map.empty? raise "Can't find '#{k}' attribute in [#{@map.keys.join(', ')}]" end v.is_a?(Array) ? v[0] : v end end |
Instance Method Details
#respond_to?(_method, _include_private = false) ⇒ Boolean
rubocop:disable Style/OptionalBooleanParameter
78 79 80 81 |
# File 'lib/factbase/fact.rb', line 78 def respond_to?(_method, _include_private = false) # rubocop:enable Style/OptionalBooleanParameter true end |
#respond_to_missing?(_method, _include_private = false) ⇒ Boolean
83 84 85 |
# File 'lib/factbase/fact.rb', line 83 def respond_to_missing?(_method, _include_private = false) true end |
#to_s ⇒ String
Convert it to a string.
39 40 41 |
# File 'lib/factbase/fact.rb', line 39 def to_s @map.to_json end |