Class: ThemeField::ThemeFileMatcher
- Inherits:
-
Object
- Object
- ThemeField::ThemeFileMatcher
- Defined in:
- app/models/theme_field.rb
Constant Summary collapse
- OPTIONS =
%i[name type target]
Instance Method Summary collapse
- #filename_from_opts(opts) ⇒ Object
-
#initialize(regex:, canonical:, targets:, names:, types:) ⇒ ThemeFileMatcher
constructor
regex: used to match file names to fields (import).
- #opts_from_filename(filename) ⇒ Object
Constructor Details
#initialize(regex:, canonical:, targets:, names:, types:) ⇒ ThemeFileMatcher
regex: used to match file names to fields (import).
can contain named capture groups for name/type/target
canonical: a lambda which converts name/type/target
to filename (export)
targets/names/types: can be nil if any value is allowed
single value
array of allowed values
548 549 550 551 552 553 554 555 |
# File 'app/models/theme_field.rb', line 548 def initialize(regex:, canonical:, targets:, names:, types:) @allowed_values = {} @allowed_values[:names] = Array(names) if names @allowed_values[:targets] = Array(targets) if targets @allowed_values[:types] = Array(types) if types @canonical = canonical @regex = regex end |
Instance Method Details
#filename_from_opts(opts) ⇒ Object
569 570 571 572 573 574 575 576 577 |
# File 'app/models/theme_field.rb', line 569 def filename_from_opts(opts) is_match = OPTIONS.all? do |option| plural = :"#{option}s" next true if @allowed_values[plural] == nil # Allows any value next true if @allowed_values[plural].include?(opts[option]) # Value is allowed end is_match ? @canonical.call(opts) : nil end |
#opts_from_filename(filename) ⇒ Object
557 558 559 560 561 562 563 564 565 566 567 |
# File 'app/models/theme_field.rb', line 557 def opts_from_filename(filename) match = @regex.match(filename) return false unless match hash = {} OPTIONS.each do |option| plural = :"#{option}s" hash[option] = @allowed_values[plural][0] if @allowed_values[plural]&.length == 1 hash[option] = match[option] if hash[option].nil? end hash end |