Class: Mulang::Inspection

Inherits:
Object
  • Object
show all
Includes:
Compacted
Defined in:
lib/mulang/inspection.rb

Defined Under Namespace

Modules: Compacted Classes: Matcher, Target

Constant Summary collapse

REGEXP =
Regexp.new %q{
^(?<negation>Not:)?
(?<type>[^:]+)
(
  :(?<matcher>WithAnything|WithLiteral|WithNonliteral|WithAnyString|WithAnyNumber|WithLogic|WithMath|WithFalse|WithNil|WithTrue) |
  :(?<matcher>WithReference|WithChar|WithNumber|WithString|WithSymbol):(?<value>[^:]+) |
  :(?<target>[^:]+)(:(?<matcher>[^:]+)(:(?<value>[^:]+))?)?
)?$}.gsub(/\s/, '')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Compacted

#as_json

Constructor Details

#initialize(type, target, negated: false, matcher: nil, i18n_namespace: nil) ⇒ Inspection

Returns a new instance of Inspection.



23
24
25
26
27
28
29
# File 'lib/mulang/inspection.rb', line 23

def initialize(type, target, negated: false, matcher: nil, i18n_namespace: nil)
  @type = type
  @target = target
  @negated = negated
  @matcher = matcher
  @i18n_namespace = i18n_namespace
end

Instance Attribute Details

#matcherObject

Returns the value of attribute matcher.



20
21
22
# File 'lib/mulang/inspection.rb', line 20

def matcher
  @matcher
end

#negatedObject Also known as: negated?

Returns the value of attribute negated.



20
21
22
# File 'lib/mulang/inspection.rb', line 20

def negated
  @negated
end

#targetObject

Returns the value of attribute target.



20
21
22
# File 'lib/mulang/inspection.rb', line 20

def target
  @target
end

#typeObject

Returns the value of attribute type.



20
21
22
# File 'lib/mulang/inspection.rb', line 20

def type
  @type
end

Class Method Details

.extensionsObject



77
78
79
# File 'lib/mulang/inspection.rb', line 77

def self.extensions
  @extensions ||= []
end

.parse(inspection_s) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/mulang/inspection.rb', line 59

def self.parse(inspection_s)
  parse_extension(inspection_s).try { |it| return it }
  match = REGEXP.match inspection_s
  raise "Invalid inspection #{inspection_s}" unless match
  Mulang::Inspection.new(
    match['type'],
    Mulang::Inspection::Target.parse(match['target']),
    matcher: Mulang::Inspection::Matcher.parse(match['matcher'], match['value']),
    negated: match['negation'].present?)
end

.parse_binding_name(binding_s) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/mulang/inspection.rb', line 51

def self.parse_binding_name(binding_s)
  if binding_s.start_with? 'Intransitive:'
    binding_s[13..-1]
  else
    binding_s
  end
end

.parse_extension(inspection_s) ⇒ Object



70
71
72
73
74
75
# File 'lib/mulang/inspection.rb', line 70

def self.parse_extension(inspection_s)
  extensions.each do |extension|
    extension.parse(inspection_s).try { |it| return it }
  end
  nil
end

.register_extension!(extension) ⇒ Object



81
82
83
# File 'lib/mulang/inspection.rb', line 81

def self.register_extension!(extension)
  extensions << extension
end

.unregister_extension!(extension) ⇒ Object



85
86
87
# File 'lib/mulang/inspection.rb', line 85

def self.unregister_extension!(extension)
  extensions.delete extension
end

Instance Method Details

#i18n_namespaceObject



47
48
49
# File 'lib/mulang/inspection.rb', line 47

def i18n_namespace
  @i18n_namespace || 'mulang.inspection'
end

#matcher_to_sObject



43
44
45
# File 'lib/mulang/inspection.rb', line 43

def matcher_to_s
  matcher ? ":#{matcher.to_s}" : nil
end

#negated_to_sObject



35
36
37
# File 'lib/mulang/inspection.rb', line 35

def negated_to_s
  negated ? 'Not:' : nil
end

#target_to_sObject



39
40
41
# File 'lib/mulang/inspection.rb', line 39

def target_to_s
  target ? ":#{target.to_s}" : nil
end

#to_sObject



31
32
33
# File 'lib/mulang/inspection.rb', line 31

def to_s
  "#{negated_to_s}#{type}#{target_to_s}#{matcher_to_s}"
end