bs5 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 54b572156c8e8680ba3937031736fe9cdb6e2aeaf2ea88cb49b6e26c18e02c35
4
- data.tar.gz: 4a4650ceee8e21d32f6a905f00c398e072bb407a4f118cd95488454f9ef61121
3
+ metadata.gz: ecb9af880ad8e465272d743689c0c470af8c322affb3e77be49bdb360f8b0022
4
+ data.tar.gz: 3b1517ef64015fa58510b9123cd625735d79e2e7fbbccda4eb161f1f4671e038
5
5
  SHA512:
6
- metadata.gz: c3be0fb1a7839cafbd9673ee974879fd2e95c13e25c5a0f73a27c278c0f2abb1d4305d7eaf965999e5c5fcd390079d116c9aea297e63c1ce5530fdc3e455deb8
7
- data.tar.gz: 73f164bfd36774c565a358e23e294d49b8f386db1450e92a9e73cefd81fa2951b7560705c6e098607a5005e50bf071c094a23d1eacfadf304be61c07995771df
6
+ metadata.gz: b638dd6760619921caaa399a585a53040501cdb796712c5bf118cc281f5ba65f16637dde70df784adff66839c2b71b91488c64bcd46a0045cc949e82cf8ba995
7
+ data.tar.gz: f5b36b7a285aad9a6eb2e803d182d5e4126398ae410c9cb2a6dd66674de9474da84ba1db4f6c2202730a6f0b255a3df71762074fb8315c74711f457d3a9331a2
@@ -3,17 +3,23 @@
3
3
  module Bs5
4
4
  class ButtonTagComponent < ViewComponent::Base
5
5
  STYLES = %i[primary secondary success danger warning info light dark link].freeze
6
- SIZES = %i[small large].freeze
6
+ DEFAULT_STYLE = :primary
7
+ SIZES = { small: :sm, large: :lg }.freeze
8
+ CLASS_PREFIX = 'btn'
7
9
 
8
- attr_reader :content_or_options, :options, :size
10
+ attr_reader :content_or_options, :size
9
11
 
10
12
  include ActiveModel::Validations
11
13
  validates :style, style: true
12
- validates :size, inclusion: { in: SIZES, valid_sizes: SIZES.to_sentence, allow_nil: true }
14
+ validates :size, inclusion: { in: SIZES.keys, valid_sizes: SIZES.keys.to_sentence, allow_nil: true }
13
15
 
14
16
  def initialize(content_or_options = nil, options = nil)
15
- @content_or_options = content_or_options.dup
16
- @options = options.dup
17
+ if content_or_options.is_a? Hash
18
+ self.options = content_or_options
19
+ else
20
+ @content_or_options = content_or_options
21
+ self.options = options
22
+ end
17
23
 
18
24
  extract_custom_options
19
25
  merge_default_options
@@ -33,6 +39,14 @@ module Bs5
33
39
 
34
40
  private
35
41
 
42
+ def options=(value)
43
+ @options = Hash(value).symbolize_keys!
44
+ end
45
+
46
+ def options
47
+ @options.empty? ? nil : @options
48
+ end
49
+
36
50
  def extract_custom_options
37
51
  extract_style
38
52
  extract_outline
@@ -40,68 +54,43 @@ module Bs5
40
54
  end
41
55
 
42
56
  def extract_style
43
- if @content_or_options.is_a? Hash
44
- @style = @content_or_options.delete(:style)
45
- @content_or_options = nil if @content_or_options.empty?
46
- elsif @options.is_a? Hash
47
- @style = @options.delete(:style)
48
- @options = nil if @options.empty?
49
- end
57
+ @style = @options.delete(:style)
50
58
  end
51
59
 
52
60
  def extract_outline
53
- if @content_or_options.is_a? Hash
54
- @outline = @content_or_options.delete(:outline)
55
- @content_or_options = nil if @content_or_options.empty?
56
- elsif @options.is_a? Hash
57
- @outline = @options.delete(:outline)
58
- @options = nil if @options.empty?
59
- end
61
+ @outline = @options.delete(:outline)
60
62
  end
61
63
 
62
64
  def extract_size
63
- if @content_or_options.is_a? Hash
64
- @size = @content_or_options.delete(:size)
65
- @content_or_options = nil if @content_or_options.empty?
66
- elsif @options.is_a? Hash
67
- @size = @options.delete(:size)
68
- @options = nil if @options.empty?
69
- end
65
+ @size = @options.delete(:size)
70
66
  end
71
67
 
72
68
  def merge_default_options
73
- if @content_or_options.is_a? Hash
74
- deep_merge_and_join @content_or_options, default_options
75
- else
76
- @options ||= {}
77
- deep_merge_and_join @options, default_options
69
+ @options.deep_merge!(default_options) do |_key, this_val, other_val|
70
+ [this_val, other_val].join(' ').strip
78
71
  end
79
72
  end
80
73
 
81
74
  def default_options
82
- {
83
- class: button_class
84
- }
75
+ { class: button_class }
85
76
  end
86
77
 
87
78
  def button_class
88
- ['btn', contextual_class, size_class].compact.join(' ')
79
+ [CLASS_PREFIX, contextual_class, size_class].compact.join(' ')
89
80
  end
90
81
 
91
82
  def contextual_class
92
- ['btn', outline? ? 'outline' : nil, style].compact.join('-')
83
+ [CLASS_PREFIX, outline? ? 'outline' : nil, style].compact.join('-')
93
84
  end
94
85
 
95
86
  def size_class
96
87
  return unless size?
97
88
 
98
- x = { large: 'lg', small: 'sm' }[@size]
99
-
100
- "btn-#{x}"
89
+ [CLASS_PREFIX, SIZES[size]].join('-')
101
90
  end
102
91
 
103
92
  def style
104
- (@style || 'primary').to_sym
93
+ (@style || DEFAULT_STYLE).to_sym
105
94
  end
106
95
 
107
96
  def outline?
@@ -109,13 +98,7 @@ module Bs5
109
98
  end
110
99
 
111
100
  def size?
112
- !!@size
113
- end
114
-
115
- def deep_merge_and_join(h1, h2)
116
- h1.deep_merge!(h2) do |key, this_val, other_val|
117
- [this_val, other_val].join(' ').strip
118
- end
101
+ !!size
119
102
  end
120
103
  end
121
104
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bs5
4
- VERSION = '0.0.9'
4
+ VERSION = '0.0.10'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bs5
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Patrick Baselier
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-26 00:00:00.000000000 Z
11
+ date: 2020-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails