Class: Pebbles::Uid::Conditions

Inherits:
Object
  • Object
show all
Defined in:
lib/pebbles-uid/conditions.rb

Constant Summary collapse

NO_MARKER =
Class.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, options = {}) ⇒ Conditions

Returns a new instance of Conditions.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/pebbles-uid/conditions.rb', line 12

def initialize(values, options = {})
  @values = values
  @max_depth = options[:max_depth]
  @name = options.fetch(:name) { 'label' }
  @suffix = options.fetch(:suffix) { nil }
  @stop = options.fetch(:stop) { NO_MARKER }
  if values.last == '*'
    @stop = NO_MARKER
    @values.pop
  end
end

Instance Attribute Details

#max_depthObject (readonly)

Returns the value of attribute max_depth.



10
11
12
# File 'lib/pebbles-uid/conditions.rb', line 10

def max_depth
  @max_depth
end

#nameObject (readonly)

Returns the value of attribute name.



10
11
12
# File 'lib/pebbles-uid/conditions.rb', line 10

def name
  @name
end

#stopObject (readonly)

Returns the value of attribute stop.



10
11
12
# File 'lib/pebbles-uid/conditions.rb', line 10

def stop
  @stop
end

#suffixObject (readonly)

Returns the value of attribute suffix.



10
11
12
# File 'lib/pebbles-uid/conditions.rb', line 10

def suffix
  @suffix
end

#valuesObject (readonly)

Returns the value of attribute values.



10
11
12
# File 'lib/pebbles-uid/conditions.rb', line 10

def values
  @values
end

Instance Method Details

#label(i = nil) ⇒ Object



55
56
57
# File 'lib/pebbles-uid/conditions.rb', line 55

def label(i = nil)
  [name, i, suffix].compact.join('_').to_sym
end

#labelizeObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pebbles-uid/conditions.rb', line 34

def labelize

  optional_part = false
  labels = values.map do |label|
    if label =~ /^\^/
      label.gsub!(/^\^/, '')
      optional_part = true
    end

    result = label.include?('|') ? label.split('|') : label
    result = [result, nil].flatten if optional_part
    result
  end

  collection = {}
  labels.each_with_index do |value, i|
    collection[label(i)] = value
  end
  collection
end

#nextObject



24
25
26
# File 'lib/pebbles-uid/conditions.rb', line 24

def next
  label(values.length)
end

#stop_labelObject



59
60
61
# File 'lib/pebbles-uid/conditions.rb', line 59

def stop_label
  { label(values.length) => stop }
end

#to_hashObject



28
29
30
31
32
# File 'lib/pebbles-uid/conditions.rb', line 28

def to_hash
  collection = labelize
  collection.merge!(stop_label) if use_stop_marker?
  collection
end

#use_stop_marker?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/pebbles-uid/conditions.rb', line 63

def use_stop_marker?
  stop != NO_MARKER && !(values.size == max_depth)
end