Class: Uinit::Type::Composition
- Defined in:
- lib/uinit/type/composition.rb
Constant Summary collapse
- INTERSECTION =
'&'
- UNION =
'|'
Instance Attribute Summary collapse
-
#compositions ⇒ Object
readonly
Returns the value of attribute compositions.
-
#operand ⇒ Object
readonly
Returns the value of attribute operand.
Instance Method Summary collapse
- #&(other) ⇒ Object
- #add_intersection_composition(operand) ⇒ Object
- #add_union_composition(operand) ⇒ Object
-
#check!(value, depth) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity.
-
#initialize(operand, compositions = []) ⇒ Composition
constructor
A new instance of Composition.
-
#inspect ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity.
- #is?(value) ⇒ Boolean
- #|(other) ⇒ Object
Methods inherited from Base
#==, [], from, from?, #is!, #to_s, #trace!
Constructor Details
#initialize(operand, compositions = []) ⇒ Composition
Returns a new instance of Composition.
9 10 11 12 13 14 |
# File 'lib/uinit/type/composition.rb', line 9 def initialize(operand, compositions = []) super() @operand = ::Uinit::Type.from(operand) @compositions = compositions end |
Instance Attribute Details
#compositions ⇒ Object (readonly)
Returns the value of attribute compositions.
16 17 18 |
# File 'lib/uinit/type/composition.rb', line 16 def compositions @compositions end |
#operand ⇒ Object (readonly)
Returns the value of attribute operand.
16 17 18 |
# File 'lib/uinit/type/composition.rb', line 16 def operand @operand end |
Instance Method Details
#&(other) ⇒ Object
18 19 20 |
# File 'lib/uinit/type/composition.rb', line 18 def &(other) clone.add_intersection_composition(other) end |
#add_intersection_composition(operand) ⇒ Object
26 27 28 |
# File 'lib/uinit/type/composition.rb', line 26 def add_intersection_composition(operand) add_composition(INTERSECTION, operand) end |
#add_union_composition(operand) ⇒ Object
30 31 32 |
# File 'lib/uinit/type/composition.rb', line 30 def add_union_composition(operand) add_composition(UNION, operand) end |
#check!(value, depth) ⇒ Object
rubocop:disable Metrics/AbcSize rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity
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 76 77 |
# File 'lib/uinit/type/composition.rb', line 50 def check!(value, depth) errors = [] is = true begin operand.check!(value, depth + 1) rescue Error => e errors << e is = false end compositions.each do |operator, operand| break if is && operator == UNION next if !is && operator == INTERSECTION begin operand.check!(value, depth + 1) is = true rescue Error => e errors << e is = false end end return value if is type_error!(value, errors.max_by(&:depth)) end |
#inspect ⇒ Object
rubocop:enable Metrics/AbcSize rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity
82 83 84 |
# File 'lib/uinit/type/composition.rb', line 82 def inspect "(#{[operand, *compositions.flatten].join(' ')})" end |
#is?(value) ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/uinit/type/composition.rb', line 34 def is?(value) is = operand.is?(value) compositions.each do |operator, operand| break if is && operator == UNION next if !is && operator == INTERSECTION is = operand.is?(value) end is end |
#|(other) ⇒ Object
22 23 24 |
# File 'lib/uinit/type/composition.rb', line 22 def |(other) clone.add_union_composition(other) end |