handlebars-helpers 0.0.5 → 0.0.73

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +2 -3
  3. data/.gitignore +1 -0
  4. data/.handlebars_helpers.json +956 -0
  5. data/.handlebars_string_formatters.json +137 -0
  6. data/.rubocop.yml +4 -2
  7. data/.vscode/settings.json +6 -0
  8. data/Gemfile +3 -0
  9. data/README.md +31 -3
  10. data/Rakefile +1 -0
  11. data/STORIES.md +55 -7
  12. data/USAGE.md +1 -1
  13. data/bin/console +1 -1
  14. data/handlebars-helpers.gemspec +10 -1
  15. data/lib/handlebars/helpers.rb +2 -0
  16. data/lib/handlebars/helpers/base_helper.rb +36 -0
  17. data/lib/handlebars/helpers/base_safe_string_helper.rb +17 -0
  18. data/lib/handlebars/helpers/code_ruby/classify.rb +40 -0
  19. data/lib/handlebars/helpers/code_ruby/deconstantize.rb +48 -0
  20. data/lib/handlebars/helpers/code_ruby/demodulize.rb +58 -0
  21. data/lib/handlebars/helpers/code_ruby/foreign_key.rb +49 -0
  22. data/lib/handlebars/helpers/code_ruby/tableize.rb +35 -0
  23. data/lib/handlebars/helpers/comparison/and.rb +44 -0
  24. data/lib/handlebars/helpers/comparison/default.rb +66 -0
  25. data/lib/handlebars/helpers/comparison/eq.rb +35 -0
  26. data/lib/handlebars/helpers/comparison/gt.rb +35 -0
  27. data/lib/handlebars/helpers/comparison/gte.rb +43 -0
  28. data/lib/handlebars/helpers/comparison/lt.rb +35 -0
  29. data/lib/handlebars/helpers/comparison/lte.rb +43 -0
  30. data/lib/handlebars/helpers/comparison/ne.rb +35 -0
  31. data/lib/handlebars/helpers/comparison/or.rb +56 -0
  32. data/lib/handlebars/helpers/configuration.rb +72 -0
  33. data/lib/handlebars/helpers/inflection/ordinal.rb +58 -0
  34. data/lib/handlebars/helpers/inflection/ordinalize.rb +59 -0
  35. data/lib/handlebars/helpers/inflection/pluralize.rb +36 -0
  36. data/lib/handlebars/helpers/inflection/pluralize_by_number.rb +60 -0
  37. data/lib/handlebars/helpers/inflection/singularize.rb +35 -0
  38. data/lib/handlebars/helpers/misc/noop.rb +36 -0
  39. data/lib/handlebars/helpers/misc/safe.rb +37 -0
  40. data/lib/handlebars/helpers/register_helpers.rb +73 -0
  41. data/lib/handlebars/helpers/string_formatting/append_if.rb +42 -0
  42. data/lib/handlebars/helpers/string_formatting/back_slash.rb +33 -0
  43. data/lib/handlebars/helpers/string_formatting/camel.rb +29 -0
  44. data/lib/handlebars/helpers/string_formatting/constantize.rb +29 -0
  45. data/lib/handlebars/helpers/string_formatting/dasherize.rb +33 -0
  46. data/lib/handlebars/helpers/string_formatting/dotirize.rb +33 -0
  47. data/lib/handlebars/helpers/string_formatting/double_colon.rb +33 -0
  48. data/lib/handlebars/helpers/string_formatting/downcase.rb +31 -0
  49. data/lib/handlebars/helpers/string_formatting/format_as.rb +57 -0
  50. data/lib/handlebars/helpers/string_formatting/humanize.rb +39 -0
  51. data/lib/handlebars/helpers/string_formatting/lamel.rb +29 -0
  52. data/lib/handlebars/helpers/string_formatting/padl.rb +58 -0
  53. data/lib/handlebars/helpers/string_formatting/padr.rb +59 -0
  54. data/lib/handlebars/helpers/string_formatting/pluserize.rb +29 -0
  55. data/lib/handlebars/helpers/string_formatting/prepend_if.rb +42 -0
  56. data/lib/handlebars/helpers/string_formatting/singularize.rb +35 -0
  57. data/lib/handlebars/helpers/string_formatting/slash.rb +33 -0
  58. data/lib/handlebars/helpers/string_formatting/snake.rb +33 -0
  59. data/lib/handlebars/helpers/string_formatting/string.js +511 -0
  60. data/lib/handlebars/helpers/string_formatting/surround.rb +49 -0
  61. data/lib/handlebars/helpers/string_formatting/surround_if.rb +43 -0
  62. data/lib/handlebars/helpers/string_formatting/titleize.rb +39 -0
  63. data/lib/handlebars/helpers/string_formatting/upcase.rb +31 -0
  64. data/lib/handlebars/helpers/string_tokenizer.rb +43 -0
  65. data/lib/handlebars/helpers/template.rb +68 -0
  66. data/lib/handlebars/helpers/version.rb +1 -1
  67. metadata +97 -6
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # Miscellaneous handling routines
11
+ module Misc
12
+ # NoOp is a wrapper for blocks, it does no additional processing.
13
+ # It is useful for embedding blocks into the output when using raw operators {{{{noop}}}}
14
+ class Noop < Handlebars::Helpers::BaseHelper
15
+ # # Parse will do nothing and return.
16
+ # # It is useful for embedding blocks into the output when using raw operators {{{{noop}}}}
17
+ # #
18
+ # # @example
19
+ # #
20
+ # # puts Noop.new.parse()
21
+ # #
22
+ # # @return does nothing, but will allow inner blocks to be processed
23
+ # def parse()
24
+ # end
25
+
26
+ def handlebars_helper
27
+ proc do |context, block|
28
+ # Handlebars::SafeString.new('<div class="bold">' + block.fn(context) + "</div>");
29
+
30
+ block.fn(context)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_safe_string_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # Miscellaneous handling routines
11
+ module Misc
12
+ # Safe will return the value with <> and single and double quotes left as is
13
+ class Safe < Handlebars::Helpers::BaseSafeStringHelper
14
+ # Parse will return the value with <> and single and double quotes left as is
15
+ #
16
+ # @example
17
+ #
18
+ # puts Safe.new.parse('<hello>')
19
+ #
20
+ # <hello>
21
+ #
22
+ # @param [String] value that is considered a safe string
23
+ # @return [String] value in safe string format
24
+ def parse(value)
25
+ value = '' if value.nil?
26
+ value
27
+ end
28
+
29
+ def handlebars_helper
30
+ proc do |_context, value|
31
+ wrapper(parse(value))
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'handlebars'
5
+ require 'handlebars/helpers/configuration'
6
+
7
+ module Handlebars
8
+ module Helpers
9
+ # Register helpers against handlebars context
10
+ class RegisterHelpers
11
+ attr_accessor :handlebars
12
+ attr_reader :helpers
13
+
14
+ # def initialize(handlebars = Handlebars::Context.new, &block)
15
+ def initialize(&block)
16
+ @handlebars = Handlebars::Context.new
17
+
18
+ if block_given?
19
+ block.call(self) # , handlebars)
20
+ else
21
+ register_from_config
22
+ end
23
+ end
24
+
25
+ def helper(*names, &helper)
26
+ names.each do |name|
27
+ handlebars.register_helper(name, &helper)
28
+ end
29
+ end
30
+
31
+ # Register from configuration file
32
+ #
33
+ # Sample configuration
34
+ # "groups": [
35
+ # {
36
+ # "name": "string_formatting",
37
+ # "base_require": "handlebars/helpers/string_formatting",
38
+ # "base_namespace": "Handlebars::Helpers::StringFormatting",
39
+ # "description": "Case modification string manipulation methods",
40
+ # "helpers": [
41
+ # {
42
+ # "name": "camel",
43
+ # "description": "convert to camel case with first word uppercase and following words uppercase",
44
+ # "aliases": ["camel", "camelUpper", "camelU"],
45
+ # "require_path": "handlebars/helpers/string_formatting/camel",
46
+ # "class_namespace": "Handlebars::Helpers::StringFormatting::Camel"
47
+ # }
48
+ # ]
49
+ # }
50
+ # ]
51
+ def register_from_config
52
+ file = File.read Handlebars::Helpers.configuration.helper_config_file
53
+ config = JSON.parse(file)
54
+
55
+ config['groups'].each do |group|
56
+ group['helpers'].each do |helper_config|
57
+ register_config_item(helper_config)
58
+ end
59
+ end
60
+ end
61
+
62
+ private
63
+
64
+ def register_config_item(config)
65
+ require config['require_path']
66
+
67
+ helper_instance = Object.const_get(config['class_namespace']).new
68
+
69
+ helper(*config['aliases'].map(&:to_sym), &helper_instance.handlebars_helper)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_safe_string_helper'
7
+ require 'handlebars/helpers/string_formatting/format_as'
8
+
9
+ module Handlebars
10
+ module Helpers
11
+ # String manipulation methods for case formatting
12
+ module StringFormatting
13
+ # Append If will append suffix to value, if value is not empty
14
+ class AppendIf < Handlebars::Helpers::BaseSafeStringHelper
15
+ # Parse will Append If will append suffix to value, if value is not empty
16
+ #
17
+ # @example
18
+ #
19
+ # puts AppendIf.new.parse('the quick brown', 'fox', 'dashify')
20
+ #
21
+ # the-quick-brown-fox
22
+ #
23
+ # @param [String] value - value to add suffix too
24
+ # @param [String] suffix - suffix to add to value
25
+ # @param [String] formats - list of formats to apply to value, defaults to none
26
+ # @return [String] value + suffix when value exists, otherwise ''
27
+ def parse(value, suffix, formats)
28
+ format_as = Handlebars::Helpers::StringFormatting::FormatAs.new
29
+ value.present? ? "#{format_as.parse(value, formats)}#{suffix}" : ''
30
+ end
31
+
32
+ def handlebars_helper
33
+ proc do |_context, value, suffix, formats|
34
+ # Handle optional: formats
35
+ formats = nil if formats.is_a?(V8::Object)
36
+ wrapper(parse(value, suffix, formats))
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # convert to back slash notation
13
+ class BackSlash < Handlebars::Helpers::BaseHelper
14
+ # Parse will convert to back slash notation
15
+ #
16
+ # @side effects
17
+ #
18
+ # Text casing is preserved.
19
+ #
20
+ # @example
21
+ #
22
+ # puts BackSlash.new.parse('the Quick brown Fox 99')
23
+ #
24
+ # the\quick\brown\fox99
25
+ #
26
+ # @return [String] value converted to back_slash case
27
+ def parse(value)
28
+ tokenizer.parse(value, preserve_case: true, separator: '\\')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # camel case the characters in the given 'string'.
13
+ class Camel < Handlebars::Helpers::BaseHelper
14
+ # Parse will camel case the characters in the given 'string'.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Camel.new.parse('the quick brown fox 99')
19
+ #
20
+ # TheQuickBrownFox99
21
+ #
22
+ # @return [String] value converted to camel case
23
+ def parse(value)
24
+ tokenizer.parse(value).underscore.camelize
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # CONSTANT case the characters in the given 'string'.
13
+ class Constantize < Handlebars::Helpers::BaseHelper
14
+ # Parse will CONSTANT case the characters in the given 'string'.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Constantize.new.parse('the quick brown fox 99')
19
+ #
20
+ # THE_QUICK_BROWN_FOX99
21
+ #
22
+ # @return [String] value converted to constant case
23
+ def parse(value)
24
+ tokenizer.parse(value, separator: '_').upcase
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # convert to dash notation
13
+ class Dasherize < Handlebars::Helpers::BaseHelper
14
+ # Parse will convert to dash notation
15
+ #
16
+ # @side effects
17
+ #
18
+ # All text is in lower case
19
+ #
20
+ # @example
21
+ #
22
+ # puts Dasherize.new.parse('the quick brown fox 99')
23
+ #
24
+ # the-quick-brown-fox99
25
+ #
26
+ # @return [String] value converted to dash notation
27
+ def parse(value)
28
+ tokenizer.parse(value)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # convert to dot notation
13
+ class Dotirize < Handlebars::Helpers::BaseHelper
14
+ # Parse will convert to dot notation
15
+ #
16
+ # @side effects
17
+ #
18
+ # All text is in lower case
19
+ #
20
+ # @example
21
+ #
22
+ # puts Dotirize.new.parse('the quick brown fox 99')
23
+ #
24
+ # the.quick.brown.fox99
25
+ #
26
+ # @return [String] value converted to dot notation
27
+ def parse(value)
28
+ tokenizer.parse(value, separator: '.')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # double_colon notation for the characters in the given 'string'. aka ruby namespace
13
+ class DoubleColon < Handlebars::Helpers::BaseHelper
14
+ # Parse will double_colon notation for the characters in the given 'string'. aka ruby namespace
15
+ #
16
+ # @side effects
17
+ #
18
+ # Text casing is preserved.
19
+ #
20
+ # @example
21
+ #
22
+ # puts DoubleColon.new.parse('the quick brown fox 99')
23
+ #
24
+ # The::Quick::Brown::Fox99
25
+ #
26
+ # @return [String] value converted to double_colon notation
27
+ def parse(value)
28
+ tokenizer.parse(value, preserve_case: true, separator: '::')
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # Downcase/Lowercase all of the characters in the given string.
13
+ class Downcase < Handlebars::Helpers::BaseHelper
14
+ # Parse will Downcase/Lowercase all of the characters in the given string.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Downcase.new.parse('The Quick Brown Fox 99')
19
+ #
20
+ # the quick brown fox 99
21
+ #
22
+ # @return [String] value in lowercase
23
+ def parse(value)
24
+ return '' if value.nil?
25
+
26
+ value.downcase
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ # reference: https://github.com/rails/rails/blob/master/activesupport/lib/active_support/inflector/methods.rb
4
+ require 'active_support/core_ext/string'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # String manipulation methods for case formatting
11
+ module StringFormatting
12
+ # Format As: Chain a list of string formatters to run sequentially
13
+ class FormatAs < Handlebars::Helpers::BaseHelper
14
+ # Parse will execute a chain of string formatters and run them sequentially
15
+ #
16
+ # @example
17
+ #
18
+ # puts FormatAs.new.parse('the quick brown fox', 'pluralize,dashify')
19
+ #
20
+ # the-quick-brown-foxes
21
+ #
22
+ # @param [String] value - value to format
23
+ # @param [String] formats - comma delimited list of formats
24
+ # @return [String] returns a value that has been processed by multiple formatters
25
+ def parse(value, formats)
26
+ return '' if value.nil?
27
+ return value if formats.nil? || formats.empty?
28
+
29
+ formats = formats.split(',') if formats.is_a?(String)
30
+ formats = formats.map(&:to_sym)
31
+ formats.each do |format|
32
+ value = format_value(value, format)
33
+ end
34
+ value
35
+ end
36
+
37
+ def handlebars_helper
38
+ proc { |_context, value, formats| wrapper(parse(value, formats)) }
39
+ end
40
+
41
+ private
42
+
43
+ def format_value(value, format)
44
+ return value if format == :none
45
+
46
+ formatter = Handlebars::Helpers.configuration.string_formatter_config[format]
47
+ unless formatter
48
+ puts 'Logger not found: format'
49
+ return value
50
+ end
51
+
52
+ formatter.parse(value)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end