Class: Pebbles::Uid::Conditions
- Inherits:
-
Object
- Object
- Pebbles::Uid::Conditions
- Defined in:
- lib/pebbles-uid/conditions.rb
Constant Summary collapse
- NO_MARKER =
Class.new
Instance Attribute Summary collapse
-
#max_depth ⇒ Object
readonly
Returns the value of attribute max_depth.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
-
#suffix ⇒ Object
readonly
Returns the value of attribute suffix.
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Instance Method Summary collapse
-
#initialize(values, options = {}) ⇒ Conditions
constructor
A new instance of Conditions.
- #label(i = nil) ⇒ Object
- #labelize ⇒ Object
- #next ⇒ Object
- #stop_label ⇒ Object
- #to_hash ⇒ Object
- #use_stop_marker? ⇒ Boolean
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, = {}) @values = values @max_depth = [:max_depth] @name = .fetch(:name) { 'label' } @suffix = .fetch(:suffix) { nil } @stop = .fetch(:stop) { NO_MARKER } if values.last == '*' @stop = NO_MARKER @values.pop end end |
Instance Attribute Details
#max_depth ⇒ Object (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 |
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/pebbles-uid/conditions.rb', line 10 def name @name end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop.
10 11 12 |
# File 'lib/pebbles-uid/conditions.rb', line 10 def stop @stop end |
#suffix ⇒ Object (readonly)
Returns the value of attribute suffix.
10 11 12 |
# File 'lib/pebbles-uid/conditions.rb', line 10 def suffix @suffix end |
#values ⇒ Object (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 |
#labelize ⇒ Object
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 |
#next ⇒ Object
24 25 26 |
# File 'lib/pebbles-uid/conditions.rb', line 24 def next label(values.length) end |
#stop_label ⇒ Object
59 60 61 |
# File 'lib/pebbles-uid/conditions.rb', line 59 def stop_label { label(values.length) => stop } end |
#to_hash ⇒ Object
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
63 64 65 |
# File 'lib/pebbles-uid/conditions.rb', line 63 def use_stop_marker? stop != NO_MARKER && !(values.size == max_depth) end |