Class: Classic::Manager
- Inherits:
-
Object
- Object
- Classic::Manager
- Defined in:
- lib/classic/manager.rb
Overview
Store and Proces styles.
Constant Summary collapse
- BASE_STYLE_KEY =
"base"- DEFAULT_STYLE_KEY =
"default"- SELF_STYLE_KEY =
"<self"- ALIASES_KEY =
"@aliases"- STYLE_EXP_REGX =
Matches: btn(varient.default,size.default) (varient.default,size.default)
/([\w.]*)\(([\w.,\s]*)\)/
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
-
#merged_css_entries ⇒ Object
readonly
Returns the value of attribute merged_css_entries.
Instance Method Summary collapse
- #build_aliases(aliases, prefix: nil) ⇒ Object
- #cls(name, classes = nil) ⇒ Object
- #collect_styles(style_fn) ⇒ Object
- #files ⇒ Object
-
#initialize(config:) ⇒ Manager
constructor
A new instance of Manager.
- #load ⇒ Object
- #lookup(name) ⇒ Object
- #parse(style) ⇒ Object
- #style_not_found_value(name) ⇒ Object
Constructor Details
#initialize(config:) ⇒ Manager
Returns a new instance of Manager.
16 17 18 19 |
# File 'lib/classic/manager.rb', line 16 def initialize(config:) @config = config @classes_merge_handler = config.classes_merge_handler end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
14 15 16 |
# File 'lib/classic/manager.rb', line 14 def config @config end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
14 15 16 |
# File 'lib/classic/manager.rb', line 14 def data @data end |
#entries ⇒ Object (readonly)
Returns the value of attribute entries.
14 15 16 |
# File 'lib/classic/manager.rb', line 14 def entries @entries end |
#merged_css_entries ⇒ Object (readonly)
Returns the value of attribute merged_css_entries.
14 15 16 |
# File 'lib/classic/manager.rb', line 14 def merged_css_entries @merged_css_entries end |
Instance Method Details
#build_aliases(aliases, prefix: nil) ⇒ Object
167 168 169 170 171 172 173 174 175 |
# File 'lib/classic/manager.rb', line 167 def build_aliases(aliases, prefix: nil) aliases&.each do |name, style| if prefix @entries["#{prefix}.#{name}"] = lookup("#{prefix}.#{style}") else @entries[name] = lookup(style) end end end |
#cls(name, classes = nil) ⇒ Object
52 53 54 55 56 |
# File 'lib/classic/manager.rb', line 52 def cls(name, classes = nil) return (@entries[name] ||= lookup(name)) unless classes @merged_css_entries[name][classes] ||= @classes_merge_handler.call([lookup(name), classes]) end |
#collect_styles(style_fn) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/classic/manager.rb', line 114 def collect_styles(style_fn) component = nil style_fn[:component].split(".").each do |c| c = c.strip component = component ? component[c] : @data[c] end # If component is direct css class. i.e "flex flex-col space-y-1.5 p-6" return style_not_found_value(style_fn[:name]) unless component # In case of i.e "card.header" component is end result css classes. return component if component.is_a?(String) if style_fn[:options].empty? # Handle "label" -> will lookup "label.<self" return component[SELF_STYLE_KEY] if component.key?(SELF_STYLE_KEY) # Handle "btn.size" -> will lookup "btn.size.default" return component[DEFAULT_STYLE_KEY] if component.key?(DEFAULT_STYLE_KEY) end styles = [] # If component is a Hash and has 'base' entry and has style options # i.e # btn: # base: rounded-md text-sm font-medium... styles << component[BASE_STYLE_KEY] if component.key?(BASE_STYLE_KEY) style_fn[:options].each do |o| option = nil o.split(".").each do |c| c = c.strip option = option ? option[c] : component[c] end if option styles << option else style_not_found_value(style_fn[:name]) end end styles end |
#files ⇒ Object
21 22 23 |
# File 'lib/classic/manager.rb', line 21 def files config.files end |
#load ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/classic/manager.rb', line 25 def load @data = {} @entries = {} @merged_css_entries = Hash.new({}) files.each do |file| extname = File.extname(file) file_data = (extname == "json") ? JSON.load_file(file) : YAML.load_file(file) next unless file_data.is_a?(Hash) aliases = file_data.delete(ALIASES_KEY) if file_data.key?(ALIASES_KEY) if config.file_prefix prefix = File.basename(file).gsub(extname, "") @data[prefix] = file_data build_aliases(aliases, prefix: prefix) else @data.merge!(file_data) do |key, old_value, new_value| raise Classic::Error, "dublicate value found for #{key} - values #{old_value}, #{new_value}" end build_aliases(aliases) end end end |
#lookup(name) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/classic/manager.rb', line 58 def lookup(name) component = @data[name] || name component_path = if component.is_a?(String) name elsif component.is_a?(Hash) # default value is style expression(i.e varient.default,size.default)) then add componet name # (i.e btn(varient.default,size.default))) # else add default to component name(i.e btn) with "." i.e(btn.default) if component.key?(DEFAULT_STYLE_KEY) if component[DEFAULT_STYLE_KEY].match?(STYLE_EXP_REGX) "#{name}#{component[DEFAULT_STYLE_KEY]}" else "#{name}.#{DEFAULT_STYLE_KEY}" end elsif component.key?(SELF_STYLE_KEY) "#{name}.#{SELF_STYLE_KEY}" end end if component_path style_fn = parse(component_path) classes = collect_styles(style_fn) return style_not_found_value(name) if classes.nil? || classes.empty? classes = classes.join(" ") if classes.is_a?(Array) classes.strip.tr("\n", " ") else style_not_found_value(name) end end |
#parse(style) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/classic/manager.rb', line 96 def parse(style) matches = style.scan(STYLE_EXP_REGX) if matches.any? component, = matches.first if component.empty? component = = "" end else component = style = "" end { component: component, options: .split(",").map(&:strip), name: style } end |
#style_not_found_value(name) ⇒ Object
161 162 163 164 165 |
# File 'lib/classic/manager.rb', line 161 def style_not_found_value(name) raise NotFoundError, "'#{name}' style not found." if config.raise_error_on_not_found "#{name}-StyleNotFound" end |