Class: HakumiComponents::Progress::StrokeGradientValue

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/components/hakumi_components/progress/stroke_gradient_value.rb

Constant Summary collapse

StrokeGradient =
T.type_alias { T::Hash[String, String] }
StrokeColor =
T.type_alias { T.nilable(T.any(String, StrokeGradient)) }

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ StrokeGradientValue

Returns a new instance of StrokeGradientValue.



37
38
39
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 37

def initialize(source)
  @source = T.let(source, StrokeGradient)
end

Class Method Details

.default_svg_directionObject



27
28
29
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 27

def self.default_svg_direction
  { x1: "0%", y1: "0%", x2: "100%", y2: "0%" }
end

.normalize(source, compact_blank: true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 13

def self.normalize(source, compact_blank: true)
  gradient = T.let({}, StrokeGradient)
  source.each do |key, value|
    next if value.nil?

    string_value = compact_blank ? value.to_s.strip : value.to_s
    next if compact_blank && string_value.empty?

    gradient[key.to_s] = string_value
  end
  gradient.empty? ? nil : gradient
end

.stop_list(stops) ⇒ Object



32
33
34
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 32

def self.stop_list(stops)
  stops.map { |key, color| "#{color} #{key}" }.join(", ")
end

Instance Method Details

#cssObject



63
64
65
66
67
68
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 63

def css
  stops = stop_pairs
  return nil if stops.empty?

  "linear-gradient(#{direction}, #{stop_list(stops)})"
end

#directionObject



42
43
44
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 42

def direction
  @source["direction"] || "to right"
end

#stop_list(stops) ⇒ Object



85
86
87
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 85

def stop_list(stops)
  self.class.stop_list(stops)
end

#stop_pairsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 47

def stop_pairs
  entries = @source.reject { |key, value| key == "direction" || value.nil? }

  if entries.key?("from") || entries.key?("to")
    pairs = T.let([], T::Array[[ String, String ]])
    from = entries["from"]
    to = entries["to"]
    pairs << [ "0%", from ] if from
    pairs << [ "100%", to ] if to
    return pairs
  end

  entries.map { |key, value| [ key, value ] }
end

#svg_directionObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/components/hakumi_components/progress/stroke_gradient_value.rb', line 71

def svg_direction
  case direction
  when "to left"
    { x1: "100%", y1: "0%", x2: "0%", y2: "0%" }
  when "to bottom"
    { x1: "0%", y1: "0%", x2: "0%", y2: "100%" }
  when "to top"
    { x1: "0%", y1: "100%", x2: "0%", y2: "0%" }
  else
    self.class.default_svg_direction
  end
end