Class: Squared::Common::JoinSet

Inherits:
Set
  • Object
show all
Defined in:
lib/squared/common/class.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = [], delim: ' ') ⇒ JoinSet

Returns a new instance of JoinSet.



43
44
45
46
# File 'lib/squared/common/class.rb', line 43

def initialize(data = [], delim: ' ')
  super(data.compact)
  @delim = delim
end

Instance Attribute Details

#delimObject (readonly)

Returns the value of attribute delim.



41
42
43
# File 'lib/squared/common/class.rb', line 41

def delim
  @delim
end

Class Method Details

.to_sObject



37
38
39
# File 'lib/squared/common/class.rb', line 37

def self.to_s
  super[/[^:]+\z/, 0]
end

Instance Method Details

#and(*args) ⇒ Object



78
79
80
81
# File 'lib/squared/common/class.rb', line 78

def and(*args)
  self << '&&'
  merge(args)
end

#doneObject



99
100
101
102
103
# File 'lib/squared/common/class.rb', line 99

def done
  ret = to_s
  clear
  ret
end

#last(val, pat) ⇒ Object



48
49
50
51
# File 'lib/squared/common/class.rb', line 48

def last(val, pat)
  (@last ||= []).push([val, pat, $1]) if val =~ pat
  self << val
end

#or(*args) ⇒ Object



83
84
85
86
# File 'lib/squared/common/class.rb', line 83

def or(*args)
  self << '||'
  merge(args)
end

#pass(&blk) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/squared/common/class.rb', line 53

def pass(&blk)
  ret = to_a.map!(&:to_s).reject(&:empty?)
  @last&.each do |val, pat, key|
    i = []
    j = nil
    ret.each_with_index do |opt, index|
      if opt == val
        j = index
      elsif j && opt[pat, 1] == key
        i << index
      end
    end
    next unless j && !i.empty?

    val = ret[j]
    cur = j
    i.each do |k|
      ret[cur] = ret[k]
      cur = k
    end
    ret[i.last] = val
  end
  block_given? ? ret.reject(&blk) : ret
end

#temp(*args, &blk) ⇒ Object



92
93
94
95
96
97
# File 'lib/squared/common/class.rb', line 92

def temp(*args, &blk)
  args.compact!
  ret = pass(&blk)
  ret = Set.new(ret.concat(args)).to_a unless args.empty?
  ret.join(@delim)
end

#to_sObject



105
106
107
# File 'lib/squared/common/class.rb', line 105

def to_s
  pass.join(@delim)
end

#with(*args, &blk) ⇒ Object



88
89
90
# File 'lib/squared/common/class.rb', line 88

def with(*args, &blk)
  temp('&&', *args, &blk)
end