Class: Pairs::CleanRoom

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/pairs.rb', line 79

def method_missing(method_name, *args, &block)
  return super unless args.count == 1

  predicate_method_name = "#{method_name}?".intern
  unless methods.include?(predicate_method_name)
    define_singleton_method(predicate_method_name) do |item|
      __items__[method_name].include?(item)
    end
  end

  __items__[method_name] ||= []
  __items__[method_name] << args.first
end

Instance Method Details

#__constraints__Object



51
52
53
# File 'lib/pairs.rb', line 51

def __constraints__
  @__constraints__ ||= []
end

#__items__Object



55
56
57
# File 'lib/pairs.rb', line 55

def __items__
  @__items__ ||= {}
end

#accompanied(a) ⇒ Object



75
76
77
# File 'lib/pairs.rb', line 75

def accompanied(a)
  constraint { |both| both.include?(a) ? both.count == 2 : true }
end

#alone(a) ⇒ Object



71
72
73
# File 'lib/pairs.rb', line 71

def alone(a)
  constraint { |both| both.include?(a) ? both == [a] : true }
end

#constraint(&block) ⇒ Object



59
60
61
# File 'lib/pairs.rb', line 59

def constraint(&block)
  __constraints__ << block
end

#separate(a, b) ⇒ Object



67
68
69
# File 'lib/pairs.rb', line 67

def separate(a, b)
  constraint { |both| both.include?(a) ? !both.include?(b) : true }
end

#together(a, b) ⇒ Object



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

def together(a, b)
  constraint { |both| both.include?(a) ? both.include?(b) : true }
end