Class: OkonomiUiKit::Component

Inherits:
Object
  • Object
show all
Defined in:
app/helpers/okonomi_ui_kit/component.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ Component

Returns a new instance of Component.



5
6
7
# File 'app/helpers/okonomi_ui_kit/component.rb', line 5

def initialize(view)
  @view = view
end

Instance Attribute Details

#view ⇒ Object (readonly)

Returns the value of attribute view.



3
4
5
# File 'app/helpers/okonomi_ui_kit/component.rb', line 3

def view
  @view
end

Class Method Details

.config_class? ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/helpers/okonomi_ui_kit/component.rb', line 101

def self.config_class?
  Object.const_defined?(config_class_name)
end

.config_class_name ⇒ Object



93
94
95
# File 'app/helpers/okonomi_ui_kit/component.rb', line 93

def self.config_class_name
  "#{config_namespace.name}::#{name.demodulize}"
end

.config_classes ⇒ Object



81
82
83
# File 'app/helpers/okonomi_ui_kit/component.rb', line 81

def self.config_classes
  @config_classes ||= resolve_config_classes
end

.config_namespace ⇒ Object



97
98
99
# File 'app/helpers/okonomi_ui_kit/component.rb', line 97

def self.config_namespace
  OkonomiUiKit::Configs
end

.config_styles_registry ⇒ Object



75
76
77
78
79
# File 'app/helpers/okonomi_ui_kit/component.rb', line 75

def self.config_styles_registry
  @config_styles_registry ||= config_classes.reverse.reduce({}) do |hash, klass|
    deep_merge(hash, klass.styles_registry)
  end
end

.deep_merge(*hashes) ⇒ Object



105
106
107
# File 'app/helpers/okonomi_ui_kit/component.rb', line 105

def self.deep_merge(*hashes)
  OkonomiUiKit::TWMerge.deep_merge_all(*hashes)
end

.internal_styles_registry ⇒ Object



63
64
65
# File 'app/helpers/okonomi_ui_kit/component.rb', line 63

def self.internal_styles_registry
  @internal_styles_registry ||= deep_merge({}, parent_styles_registry)
end

.parent_styles_registry ⇒ Object



67
68
69
70
71
72
73
# File 'app/helpers/okonomi_ui_kit/component.rb', line 67

def self.parent_styles_registry
  if superclass.respond_to?(:internal_styles_registry)
    superclass.internal_styles_registry
  else
    {}
  end
end

.register_styles(theme = :default, &block) ⇒ Object

Raises:

  • (ArgumentError)


54
55
56
57
58
59
60
61
# File 'app/helpers/okonomi_ui_kit/component.rb', line 54

def self.register_styles(theme = :default, &block)
  styles = block.call if block_given?

  raise ArgumentError, "Styles must be a Hash" unless styles.is_a?(Hash)

  internal_styles_registry[theme] ||= {}
  internal_styles_registry[theme] = deep_merge(internal_styles_registry[theme], styles)
end

.resolve_config_classes ⇒ Object



85
86
87
88
89
90
91
# File 'app/helpers/okonomi_ui_kit/component.rb', line 85

def self.resolve_config_classes
  classes = []
  classes << Object.const_get(config_class_name) if config_class?
  classes += superclass.config_classes if superclass <= OkonomiUiKit::Component

  classes.compact
end

Instance Method Details

#combined_styles ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'app/helpers/okonomi_ui_kit/component.rb', line 32

def combined_styles
  internal_name = internal_styles_registry.has_key?(theme_name) ? theme_name : :default
  config_name = config_styles_registry.has_key?(theme_name) ? theme_name : :default

  internal_styles = internal_styles_registry[internal_name] || {}
  config_styles = config_styles_registry[config_name] || {}

  deep_merge({}, internal_styles, config_styles)
end

#config_styles_registry ⇒ Object



46
47
48
# File 'app/helpers/okonomi_ui_kit/component.rb', line 46

def config_styles_registry
  self.class.config_styles_registry
end

#internal_styles_registry ⇒ Object



42
43
44
# File 'app/helpers/okonomi_ui_kit/component.rb', line 42

def internal_styles_registry
  self.class.internal_styles_registry
end

#name ⇒ Object



13
14
15
# File 'app/helpers/okonomi_ui_kit/component.rb', line 13

def name
  self.class.name.demodulize.underscore
end

#style(*args) ⇒ Object



24
25
26
# File 'app/helpers/okonomi_ui_kit/component.rb', line 24

def style(*args)
  styles.dig(*args)
end

#styles ⇒ Object



28
29
30
# File 'app/helpers/okonomi_ui_kit/component.rb', line 28

def styles
  @combined_styles ||= combined_styles
end

#template_path ⇒ Object



9
10
11
# File 'app/helpers/okonomi_ui_kit/component.rb', line 9

def template_path
  [ self.class.name.underscore.gsub("okonomi_ui_kit/", "okonomi/"), name ].join("/")
end

#theme_name ⇒ Object



50
51
52
# File 'app/helpers/okonomi_ui_kit/component.rb', line 50

def theme_name
  :default
end

#tw_merge(*classes) ⇒ Object



110
111
112
# File 'app/helpers/okonomi_ui_kit/component.rb', line 110

def tw_merge(*classes)
  OkonomiUiKit::TWMerge.merge_all(*classes)
end

#variant_style(*args) ⇒ Object



17
18
19
20
21
22
# File 'app/helpers/okonomi_ui_kit/component.rb', line 17

def variant_style(*args)
  tw_merge(
    style(*args[1..-1]),
    style(*(([:variants] + args).compact))
  )
end