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
502 503 504 505 506 507 508 509 |
# File 'app/models/theme_field.rb', line 502 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
523 524 525 526 527 528 529 530 531 |
# File 'app/models/theme_field.rb', line 523 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
511 512 513 514 515 516 517 518 519 520 521 |
# File 'app/models/theme_field.rb', line 511 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 |