Module: LocoMotion::Helpers

Defined in:
lib/loco_motion/helpers.rb

Class Method Summary collapse

Class Method Details

.component_example_path(component_name) ⇒ String

The demo app's example route for a component, keyed by its full class name.

Parameters:

  • component_name (String)

    The full component class name, e.g. "Daisy::Layout::AuraComponent".

Returns:

  • (String)

    The path to the component's example page.



198
199
200
# File 'lib/loco_motion/helpers.rb', line 198

def component_example_path(component_name)
  "/examples/#{component_name}"
end

.component_partial_path(component_name) ⇒ String

The demo app's partial path for a component's example view, derived from its full class name and COMPONENTS entry. The class name's first segment (Daisy/Loco) becomes the framework directory; for 3-segment names (e.g. Daisy::Layout::AuraComponent) the middle segment becomes an additional section directory (e.g. layout/), while 2-segment names (e.g. Loco::IconComponent) have no section directory.

Parameters:

  • component_name (String)

    The full component class name, e.g. "Daisy::Layout::AuraComponent".

Returns:

  • (String)

    The partial path to the component's example view.



216
217
218
219
220
221
222
223
224
225
226
# File 'lib/loco_motion/helpers.rb', line 216

def component_partial_path(component_name)
  comp = COMPONENTS[component_name]

  comp_split = component_name.split("::")
  framework = comp_split.first.underscore
  section = comp_split.length == 3 ? comp_split[1] : nil
  example = comp[:example]
  section_path = section ? "#{section.underscore}/" : ""

  "/examples/#{framework}/#{section_path}#{example}"
end

.new_component?(component_name) ⇒ Boolean

Whether a component was added in the current (or upcoming) minor release. Powers the "New" badges in the demo nav: registry entries opt in with an added: "MAJOR.MINOR" key, and the badge expires on its own once LocoMotion::VERSION moves past that series.

Parameters:

  • component_name (String)

    The full component class name, e.g. "Daisy::Layout::AuraComponent".

Returns:

  • (Boolean)

    Whether the component is new as of this release.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/loco_motion/helpers.rb', line 174

def new_component?(component_name)
  added = COMPONENTS.dig(component_name, :added)
  return false if added.nil?

  version = Gem::Version.new(LocoMotion::VERSION)
  major, minor = version.segments[0..1]

  # A pre-release on main (e.g. `0.8.0.pre`) sits *between* series: 0.8
  # hasn't shipped, so badges must match what the published 0.7 demo
  # shows. Compare against the last released series instead.
  minor -= 1 if version.prerelease? && minor.positive?

  Gem::Version.new(added) >= Gem::Version.new("#{major}.#{minor}")
end