Class: LocoMotion::Migrations::LeadingTrailing

Inherits:
Object
  • Object
show all
Defined in:
lib/loco_motion/migrations/leading_trailing.rb

Overview

Rewrites the start / end component API (removed in v0.7.0) to its leading / trailing replacement — with_start / with_end slot calls (and the Modal's with_start_actions / with_end_actions), start: / end: keyword arguments, and the generated part options (start_css:, end_html:, start_actions_css:, ...) on every renamed call: the labelable helpers (daisy_text_input, daisy_input, daisy_select, daisy_checkbox, daisy_toggle, daisy_radio, daisy_cally_input), the ThemeController's build_radio_input, daisy_navbar, daisy_modal, and the timeline's with_event.

This is intentionally NOT a blind find-and-replace: an app's own components may define start / end slots of their own. So keyword arguments are renamed only inside a recognized call, and with_start / with_end only when called on the block variable of an enclosing recognized block. Anything the scan cannot confidently attribute is collected in #leftovers for manual review instead of rewritten.

Like the icons Scanner, this is a plain line-based rewrite with no Ruby evaluation, so the same source always produces the same result. It runs as a dry run unless apply: is true.

Constant Summary collapse

DEFAULT_PATHS =

The file set scanned by default, relative to root.

["app/**/*.{rb,erb,haml,slim}"].freeze
HELPERS =

Helpers and builders whose calls take the renamed options and whose blocks yield a component with leading / trailing slots.

%w[
  daisy_text_input daisy_input daisy_select daisy_checkbox
  daisy_toggle daisy_radio daisy_cally_input build_radio_input
  daisy_navbar daisy_modal with_event
].freeze
HELPER_CALL =

Matches a call to any of the helpers above.

/\b(?:#{HELPERS.join('|')})\b/
RENAMES =

Maps each removed option/slot name to its replacement.

{ "start" => "leading", "end" => "trailing" }.freeze
KWARG =

start: / end: (plus the Modal's _actions pair and the generated part options) as keyword arguments. The leading (, ,, {, or start-of-line guard keeps words inside strings ("checkbox_end") and other symbols (:start_date) from matching.

/(^\s*|[(,{]\s*)(start|end)(_actions)?(_css|_html|_aria|_data)?:(?!:)/
KWARG_ROCKET =

The same keys written with a hash rocket (:end => "...").

/(^\s*|[(,{]\s*):(start|end)(_actions)?(_css|_html|_aria|_data)?(\s*=>)/
BLOCK_PARAM =

A do |var| / { |var| block parameter on the final line of a call.

/(?:\bdo|\{)\s*\|\s*(\w+)\s*\|/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root:, paths: DEFAULT_PATHS, apply: false) ⇒ LeadingTrailing

Returns a new instance of LeadingTrailing.

Parameters:

  • root (String)

    The directory the path globs resolve against (usually Rails.root).

  • paths (Array<String>) (defaults to: DEFAULT_PATHS)

    Glob patterns to scan, relative to root.

  • apply (Boolean) (defaults to: false)

    When true, write the rewritten files back to disk. When false (the default), only compute #changes.



67
68
69
70
71
72
73
# File 'lib/loco_motion/migrations/leading_trailing.rb', line 67

def initialize(root:, paths: DEFAULT_PATHS, apply: false)
  @root = root.to_s
  @paths = Array(paths)
  @apply = apply
  @changes = []
  @leftovers = []
end

Instance Attribute Details

#changesArray<Hash> (readonly)

Returns One entry per rewritten line: { file:, line:, before:, after: } (line numbers are 1-based and files are relative to root).

Returns:

  • (Array<Hash>)

    One entry per rewritten line: { file:, line:, before:, after: } (line numbers are 1-based and files are relative to root).



79
80
81
# File 'lib/loco_motion/migrations/leading_trailing.rb', line 79

def changes
  @changes
end

#leftoversArray<Hash> (readonly)

Returns Occurrences that were left untouched for manual review: { file:, line:, text:, reason: }.

Returns:

  • (Array<Hash>)

    Occurrences that were left untouched for manual review: { file:, line:, text:, reason: }.



84
85
86
# File 'lib/loco_motion/migrations/leading_trailing.rb', line 84

def leftovers
  @leftovers
end

Instance Method Details

#apply?Boolean

Returns Whether #run writes files or only reports.

Returns:

  • (Boolean)

    Whether #run writes files or only reports.



88
89
90
# File 'lib/loco_motion/migrations/leading_trailing.rb', line 88

def apply?
  @apply
end

#runLocoMotion::Migrations::LeadingTrailing

Scans (and, in apply mode, rewrites) every matched file.

Returns:



98
99
100
101
102
# File 'lib/loco_motion/migrations/leading_trailing.rb', line 98

def run
  files.each { |file| process_file(file) }

  self
end