Class: Runt::Collection

Inherits:
Object
  • Object
show all
Includes:
TExpr
Defined in:
lib/runt/temporalexpression.rb

Overview

Base class for TExpr classes that can be composed of other TExpr objects imlpemented using the Composite(GoF) pattern.

Direct Known Subclasses

Intersect, Union

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TExpr

#&, #-, #and, #dates, #include?, #minus, #or, #|

Constructor Details

#initialize(expressions = []) ⇒ Collection

Returns a new instance of Collection.



90
91
92
# File 'lib/runt/temporalexpression.rb', line 90

def initialize(expressions = [])
  @expressions = expressions
end

Instance Attribute Details

#expressionsObject (readonly)

Returns the value of attribute expressions.



88
89
90
# File 'lib/runt/temporalexpression.rb', line 88

def expressions
  @expressions
end

Instance Method Details

#==(other) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/runt/temporalexpression.rb', line 94

def ==(other)
  if other.is_a?(Collection)
    o_exprs = other.expressions.dup
    expressions.each do |e|
      return false unless i = o_exprs.index(e)
      o_exprs.delete_at(i)
    end
    o_exprs.each {|e| return false unless i == expressions.index(e)}
    return true
  else
    super(other)
  end
end

#add(anExpression) ⇒ Object



108
109
110
111
# File 'lib/runt/temporalexpression.rb', line 108

def add(anExpression)
  @expressions.push anExpression
  self
end

#displayObject



139
140
141
142
143
144
# File 'lib/runt/temporalexpression.rb', line 139

def display
  puts "I am a #{self.class} containing:"
  @expressions.each do |ex| 
    pp "#{ex.class}"
  end
end

#overlap?(date_expr) ⇒ Boolean

Will return true if the supplied object overlaps with the range used to create this instance

Returns:

  • (Boolean)


115
116
117
118
119
120
# File 'lib/runt/temporalexpression.rb', line 115

def overlap?(date_expr)
  @expressions.each do | interval |
    return true if date_expr.overlap?(interval)      
  end
  false    
end

#to_sObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/runt/temporalexpression.rb', line 122

def to_s
  if !@expressions.empty? && block_given?
    first_expr, next_exprs = yield
    result = '' 
    @expressions.map do |expr|
      if @expressions.first===expr
        result = first_expr + expr.to_s
      else
        result = result + next_exprs + expr.to_s
      end 
    end
    result
  else
    'empty'
  end
end