Class: Coupdoeil::Popover::Option::Placement

Inherits:
Coupdoeil::Popover::Option show all
Defined in:
app/models/coupdoeil/popover/option/placement.rb

Constant Summary collapse

VALUES =
[
  "auto",
  ["top", "top-start", "top-end"],
  ["right", "right-start", "right-end"],
  ["bottom", "bottom-start", "bottom-end"],
  ["left", "left-start", "left-end"],
].flatten.freeze
INDEX_BY_VALUES =
VALUES.each_with_index.to_h.with_indifferent_access.freeze

Constants inherited from Coupdoeil::Popover::Option

InvalidOptionError

Instance Attribute Summary

Attributes inherited from Coupdoeil::Popover::Option

#value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Coupdoeil::Popover::Option

inherited, #initialize, into_bits

Constructor Details

This class inherits a constructor from Coupdoeil::Popover::Option

Class Method Details

.extract_values(value, validate: false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/models/coupdoeil/popover/option/placement.rb', line 29

def extract_values(value, validate: false)
  case value
  when Array
    raise_invalid_option("You can provide maximum 4 placement options.") if validate && value.length > 4
    raise_invalid_option("You must provide at least one option.") if validate && value.empty?
    value
  when Symbol
    [value.name]
  when String
    value.split(",").each(&:strip!).tap do |values|
      if values.many?
        locations = caller_locations
        start_index = (locations.find_index { |s| s.path.include?(Rails.root.to_s) } || 0) + 1
        Coupdoeil.deprecator.warn(
          "Use array of string instead to provide several placement options",
          caller_locations(start_index),
        )
      end
    end
  end
end

.parse(value) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/models/coupdoeil/popover/option/placement.rb', line 19

def parse(value)
  values = extract_values(value)
  4.times.sum do |index|
    next 0 unless (placement = values[index])

    placement_index = INDEX_BY_VALUES[placement]
    placement_index << (index * 4)
  end
end

Instance Method Details

#validate!Object



52
53
54
55
56
57
58
59
60
61
# File 'app/models/coupdoeil/popover/option/placement.rb', line 52

def validate!
  values = Placement.extract_values(value, validate: true)

  values.each do |placement_value|
    next if placement_value.in?(VALUES)

    values_sentence = VALUES.to_sentence(last_word_connector: " or ")
    raise_invalid_option("Value must be one of: #{values_sentence}")
  end
end