Skip to content

Instantly share code, notes, and snippets.

@jmmastey
Last active June 27, 2018 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmmastey/66e688cb9b3733a3c8218e013fcef12a to your computer and use it in GitHub Desktop.
Save jmmastey/66e688cb9b3733a3c8218e013fcef12a to your computer and use it in GitHub Desktop.
opts = {}
str = '{{ options.foo__required }}{{ options.bar__required }}{{ options.baz_9 }}'
# I'm still flattening right now... though just once in this case.
keys = str.scan(/\{\{ options.(\w+) \}\}/).flatten
=> ["foo__required", "bar__required", "baz_9"]
opts.merge!(Hash[keys.product([nil])])
=> {"foo__required"=>nil, "bar__required"=>nil, "baz_9"=>nil}
# if liquid tags are unlikely to change, maybe we quarantine their shittiness to one place?
# also includes uniq
# also uses match + named capture, but I don't know if that's an improvement. no more flattens!
def extract_opts(optstring)
tag_bodies(optstring).map { |option| option_name(option) }.compact.uniq
end
def option_name(option)
option.match(/options.(?<name>\w+)/)[:name]
end
def tag_bodies(str)
str.scan(/\{[\{%] ([^%\}]*) [%\}]\}/).flatten
end
str = '{{ options.foo__required }}{{ options.bar__required | upcase }}{% options.baz_9 %}{{ options.quux__integer | downcase }}'
extract_opts(str)
# liquid expressions may contain multiple variables.
# tons of flattens now, but compaction is automatic!
def extract_opts(optstring)
tag_bodies(optstring).inject([]) do |result, option|
result + option_names(option)
end.uniq
end
def option_names(option)
option.scan(/options.(\w+)/).flatten || []
end
def tag_bodies(str)
str.scan(/\{[\{%] ([^%\}]*) [%\}]\}/).flatten
end
str = '{{ options.foo__required }}
{{ options.bar__required | upcase }}
{% options.baz_9 %}
{{ nuffin }}
{{ options.quux__number | downcase }}
{{ options.boop__number | times: options.donk__number }}'
extract_opts(str)
=> ["foo__required", "bar__required", "baz_9", "quux__number", "boop__number", "donk__number"]
def extract_opts(optstring)
proper_flags(optstring) + classic_flags(optstring)
end
def proper_flags(str)
str.scan(/\{\{ options.(\w+) \}\}/).flatten
end
# looks like this is an oldschool liquid thing? trying adding a name for it.
# also prevents some of the worst of the unnesting of scan results
def classic_flags(str)
str.scan(/\{% options.(\w+) %\}/).flatten
end
str = '{{ options.foo__required }}{{ options.bar__required }}{% options.baz_9 %}'
extract_opts(str)
=> ["foo__required", "bar__required", "baz_9"]
# properly disregards transforms, but I'd like to template the regexes to be a bit more readable...
def extract_opts(optstring)
proper_flags(optstring) + classic_flags(optstring)
end
def proper_flags(str)
str.scan(/\{\{ options.(\w+)[^}]*\}\}/).flatten
end
def classic_flags(str)
str.scan(/\{% options.(\w+)[^%]*%\}/).flatten
end
str = '{{ options.foo__required }}{{ options.bar__required | upcase }}{% options.baz_9 %}{{ options.quux__integer | downcase }}'
extract_opts(str)
=> ["foo__required", "bar__required", "quux__integer", "baz_9"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment