Module: Charty::DashPatternGenerator

Extended by:
Enumerable
Defined in:
lib/charty/dash_pattern_generator.rb

Constant Summary collapse

NAMED_PATTERNS =
{
        solid: "",
         dash: [4, 1.5],
          dot:  [1, 1],
      dashdot: [3, 1.25, 1.5, 1.25],
  longdashdot: [5, 1, 1, 1],
}.freeze

Class Method Summary collapse

Class Method Details

.eachObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/charty/dash_pattern_generator.rb', line 28

def self.each
  return enum_for(__method__) unless block_given?

  NAMED_PATTERNS.each_value do |pattern|
    yield pattern
  end

  m = 3
  while true
    # Long and short dash combinations
    a = [3, 1.25].repeated_combination(m).to_a[1..-2].reverse
    b = [4, 1].repeated_combination(m).to_a[1..-2]

    # Interleave these combinations
    segment_list = a.zip(b).flatten(1)

    # Insert the gaps
    segment_list.each do |segment|
      gap = segment.min
      pattern = segment.map {|seg| [seg, gap] }.flatten
      yield pattern
    end

    m += 1
  end
end

.pattern_to_name(pattern) ⇒ Object



21
22
23
24
25
26
# File 'lib/charty/dash_pattern_generator.rb', line 21

def self.pattern_to_name(pattern)
  NAMED_PATTERNS.each do |key, val|
    return key if pattern == val
  end
  nil
end

.valid_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
# File 'lib/charty/dash_pattern_generator.rb', line 11

def self.valid_name?(name)
  name = case name
         when Symbol, String
           name.to_sym
         else
           name.to_str.to_sym
         end
  NAMED_PATTERNS.key?(name)
end