Skip to content

Commit

Permalink
add helper to scrub through yml files, add helper for view_component …
Browse files Browse the repository at this point in the history
…and sidecar assets
  • Loading branch information
Tonksthebear committed Nov 11, 2021
1 parent 9dd1554 commit 535fd7b
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
5 changes: 3 additions & 2 deletions lib/classy/yaml.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "classy/yaml/version"
require "classy/yaml/railtie"
require "classy/yaml/engine"

module Classy
module Yaml
# Your code goes here...
autoload :Helpers, "classy/yaml/helpers"
autoload :ComponentHelpers, "classy/yaml/component_helpers"
end
end
13 changes: 13 additions & 0 deletions lib/classy/yaml/component_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Classy
module Yaml
module ComponentHelpers
def yass(*args)
component_name = caller.first.split("/").last.split(".").first
calling_path = caller.first.split("/")[0...-1].join("/")
classy_file = calling_path + "/" + component_name + ".yml"

helpers.yass(args, classy_files: [classy_file] )
end
end
end
end
10 changes: 10 additions & 0 deletions lib/classy/yaml/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Classy
module Yaml
class Engine < ::Rails::Engine
paths["lib/classy/yaml"]
config.to_prepare do
ApplicationController.helper(Classy::Yaml::Helpers)
end
end
end
end
84 changes: 84 additions & 0 deletions lib/classy/yaml/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module Classy
module Yaml
module Helpers
def yass(*args)
classy_yamls = []
classy_yamls << YAML.load_file(Rails.root.join("config/utility_classes.yml")) if File.exists?(Rails.root.join("config/utility_classes.yml"))

classy_files_hash = args.find { |arg| arg.is_a?(Hash) && arg.keys.include?(:classy_files) }
if classy_files_hash.present?
classy_files_hash[:classy_files].each do |file|
if File.exists?(file) && YAML.load_file(file)
file = YAML.load_file(file)
classy_yamls << file if file
end
end
end

return if classy_yamls.blank?

keys, classes = flatten_args(values: args)
classes += fetch_classes(keys, classy_yamls: classy_yamls)

return classes.flatten.join(" ")
end

private

def flatten_args(root: [], values: [], keys: [], added_classes: [])
values.each do|value|
if value.is_a?(Hash)
if value.has_key? :add
added_classes << value[:add]
value.except! :add
end

value.keys.each do |key|
values << (root + [key.to_s])
flatten_args(root: root + [key.to_s], values: [value[key]], keys: keys, added_classes: added_classes)
end
else
if value.is_a?(Array)
flatten_args(root: root, values: value, keys: keys, added_classes: added_classes)
else
keys << (root + [value.to_s])
end
end
end

return keys, added_classes
end

def fetch_classes(keys, classy_yamls: [])
classes = []

keys.map do |key|
base_classes = nil
fetched_classes = nil

classy_yamls.reverse_each do |classy_yaml|
base_classes ||= if classy_yaml.send(:dig, *key).is_a?(Hash)
classy_yaml.send(:dig, *(key + ['base'])).try(:split, " ")
else
classy_yaml.send(:dig, *(key[0...-1] + ['base'])).try(:split, " ")
end

fetched_classes ||= unless classy_yaml.send(:dig, *key).is_a?(Hash)
classy_yaml.send(:dig, *key).try(:split, " ")
end

base_classes = nil if base_classes.blank?
fetched_classes = nil if fetched_classes.blank?
end

classes << base_classes
classes << fetched_classes
end

classes.reject!(&:blank?)
return classes.flatten.uniq
end

end
end
end
6 changes: 0 additions & 6 deletions lib/classy/yaml/railtie.rb

This file was deleted.

0 comments on commit 535fd7b

Please sign in to comment.