~hacktivista/leanweb

357a56a36a002348191fcd5ded246bd60ce251e6 — Felix Freeman 12 days ago 122ee2c main
Bugfix: Properly handle {'file': 'action'} Route@action initialization

- Adds helpers.rb for helpers
- String#camelize and String#pascalize
- CHANGELOG.md on yard
M .yardopts => .yardopts +1 -1
@@ 1,2 1,2 @@
--markup markdown
- README.md LICENSE
- README.md CHANGELOG.md LICENSE

M CHANGELOG.md => CHANGELOG.md +1 -0
@@ 3,3 3,4 @@
## 0.1.x

- Allow nested `Controller#render`, for layouts or partials.
- Add `String#camelize` and `String#pascalize` methods.

M lib/leanweb.rb => lib/leanweb.rb +2 -0
@@ 28,3 28,5 @@ module LeanWeb
  autoload :Controller, 'leanweb/controller.rb'
  autoload :App, 'leanweb/app.rb'
end

require_relative 'leanweb/helpers'

A lib/leanweb/helpers.rb => lib/leanweb/helpers.rb +24 -0
@@ 0,0 1,24 @@
# frozen_string_literal: true

# Copyright 2022 Felix Freeman <libsys@hacktivista.org>
#
# This file is part of "LeanWeb" and licensed under the terms of the Hacktivista
# General Public License version 0.1 or (at your option) any later version. You
# should have received a copy of this license along with the software. If not,
# see <https://hacktivista.org/licenses/>.

# String helpers
class String
  # String to PascalCase.
  def pascalize
    camelize(pascal: true)
  end

  # String to camelCase.
  # @param pascal [Boolean] If true first letter is uppercase.
  def camelize(pascal: false)
    str = gsub(/[-_\s]+(.?)/) { |match| match[1].upcase }
    str[0] = pascal ? str[0].upcase : str[0].downcase
    str
  end
end

M lib/leanweb/route.rb => lib/leanweb/route.rb +10 -6
@@ 106,18 106,22 @@ module LeanWeb
                end
    end

    # @param value [Hash, String, nil] Check {#initialize} action param for
    # @param src_value [Hash, String, nil] Check {#initialize} action param for
    #   valid input.
    # @return [Hash] valid hash for {Action}.
    def prepare_action_hash(value)
    def prepare_action_hash(src_value) # rubocop:disable Metrics/MethodLength
      begin
        value[:file], value[:action] = value.first \
          unless %i[file controller action].include?(value.keys.first)
        if %i[file controller action].include?(src_value.keys.first)
          value = src_value
        else
          value = {}
          value[:file], value[:action] = src_value.first
        end
      rescue NoMethodError
        value = { action: value }
        value = { action: src_value }
      end
      value[:file] ||= 'main'
      value[:controller] ||= "#{value[:file].capitalize}Controller"
      value[:controller] ||= "#{value[:file].pascalize}Controller"
      value[:action] ||= "#{path_basename}_#{@method.downcase}"
      value
    end

A test/helpers_test.rb => test/helpers_test.rb +25 -0
@@ 0,0 1,25 @@
# frozen_string_literal: true

# Copyright 2022 Felix Freeman <libsys@hacktivista.org>
#
# This file is part of "LeanWeb" and licensed under the terms of the Hacktivista
# General Public License version 0.1 or (at your option) any later version. You
# should have received a copy of this license along with the software. If not,
# see <https://hacktivista.org/licenses/>.

# Helpers tests.
class HelpersTest < Minitest::Test
  def test_string_camelize
    assert_equal(
      'iWantACamel',
      'i_want-a camel'.camelize
    )
  end

  def test_string_pascalize
    assert_equal(
      'IWantAPascal',
      'i_want-a pascal'.pascalize
    )
  end
end

M test/route_test.rb => test/route_test.rb +4 -2
@@ 29,9 29,11 @@ class RouteTest < Minitest::Test
  end

  def test_new__action_tuple
    route = LeanWeb::Route.new(path: '/', action: { 'controller' => 'action' })
    route = LeanWeb::Route.new(
      path: '/', action: { 'my_controller' => 'my_action' }
    )
    assert_equal(
      %w[controller ControllerController action],
      %w[my_controller MyControllerController my_action],
      Array(route.action)
    )
  end