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,43 @@
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
+ # Comparison helpers, eg. or, and, equal, not equal, less than, greater than etc.
11
+ module Comparison
12
+ # Lte: (less than or equal to) Block helper that renders a block if `a` is **less than or equal to** `b`. If an inverse block is specified it will be rendered when falsy.
13
+ class Lte < Handlebars::Helpers::BaseHelper
14
+ # Parse will Lte: (less than or equal to) Block helper that renders a block if `a` is **less than or equal to** `b`. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Lte.new.parse(1, 2)
19
+ #
20
+ # Truthy
21
+ #
22
+ # puts Lte.new.parse(1, 1)
23
+ #
24
+ # Truthy
25
+ #
26
+ # puts Lte.new.parse(2, 1)
27
+ #
28
+ # Falsey
29
+ #
30
+ # @param [String] lhs - left hand side value
31
+ # @param [String] rhs - right hand side value
32
+ # @return [String] truthy value if left hand side LESS THAN or EQUAL to right hand side
33
+ def parse(lhs, rhs)
34
+ lhs <= rhs
35
+ end
36
+
37
+ def handlebars_helper
38
+ proc { |_context, lhs, rhs| wrapper(parse(lhs, rhs)) }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,35 @@
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
+ # Comparison helpers, eg. or, and, equal, not equal, less than, greater than etc.
11
+ module Comparison
12
+ # Ne: (not equal) Block helper that renders a block if `a` is **not equal to** `b`. If an inverse block is specified it will be rendered when falsy.
13
+ class Ne < Handlebars::Helpers::BaseHelper
14
+ # Parse will Ne: (not equal) Block helper that renders a block if `a` is **not equal to** `b`. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Ne.new.parse('aaa', 'bbb')
19
+ #
20
+ # Truthy
21
+ #
22
+ # @param [String] lhs - left hand side value
23
+ # @param [String] rhs - right hand side value
24
+ # @return [String] truthy value if left hand side is NOT equal to right hand side
25
+ def parse(lhs, rhs)
26
+ lhs != rhs
27
+ end
28
+
29
+ def handlebars_helper
30
+ proc { |_context, lhs, rhs| wrapper(parse(lhs, rhs)) }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,56 @@
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
+ # Comparison helpers, eg. or, and, equal, not equal, less than, greater than etc.
11
+ module Comparison
12
+ # Or: Block helper that renders a block if **any of** the given values is truthy. If an inverse block is specified it will be rendered when falsy.
13
+ #
14
+ # @example
15
+ #
16
+ # {{#if (or p1 p2 p3 p4 p5)}}
17
+ # found
18
+ # {{/if}}
19
+ #
20
+ # @example
21
+ #
22
+ # {{#if (or
23
+ # (eq section1 "foo")
24
+ # (ne section2 "bar"))}}
25
+ # .. content
26
+ # {{/if}}
27
+ #
28
+ # @example
29
+ # {{#if (or name age)}}
30
+ # {{name}}-{{age}}
31
+ # {{else}}
32
+ # no name or age
33
+ # {{/if}}
34
+ class Or < Handlebars::Helpers::BaseHelper
35
+ # Parse will Or: Block helper that renders a block if **any of** the given values is truthy. If an inverse block is specified it will be rendered when falsy.
36
+ #
37
+ # @example
38
+ #
39
+ # puts Or.new.parse(var1, var2)
40
+ #
41
+ # truthy block
42
+ #
43
+ # @param values list of values (via *splat) to be checked via OR condition
44
+ # @return [String] return block when first value is truthy
45
+ def parse(values)
46
+ values.any? { |value| value }
47
+ end
48
+
49
+ def handlebars_helper
50
+ # Exclude last paramater which is the context V8::Object
51
+ proc { |_context, *values| wrapper(parse(values[0..-2])) }
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'handlebars/helpers/string_tokenizer'
4
+
5
+ module Handlebars
6
+ # Configuration access for Handlebars/Helpers
7
+ module Helpers
8
+ # Configuration access for handlebars/helpers
9
+ class << self
10
+ attr_writer :configuration
11
+ end
12
+
13
+ def self.configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def self.reset
18
+ @configuration = Configuration.new
19
+ end
20
+
21
+ def self.configure
22
+ yield(configuration)
23
+ end
24
+
25
+ # Configuration class
26
+ class Configuration
27
+ attr_accessor :tokenizer,
28
+ :helper_config_file,
29
+ :padl_count,
30
+ :padl_char,
31
+ :padr_count,
32
+ :padr_char
33
+ attr_reader :string_formatter_config_file
34
+ attr_writer :string_formatter_config
35
+
36
+ def initialize
37
+ @tokenizer = Handlebars::Helpers::StringTokenizer.new
38
+ @helper_config_file = '.handlebars_helpers.json'
39
+ @string_formatter_config_file = '.handlebars_string_formatters.json'
40
+ @padr_count = 30
41
+ @padr_char = ' '
42
+ @padl_count = 30
43
+ @padl_char = ' '
44
+ end
45
+
46
+ def string_formatter_config_file=(value)
47
+ @string_formatter_config_file = value
48
+ # updating the file will clear the config object,
49
+ # which will be reloaded on next call to string_formatter_config
50
+ self.string_formatter_config = nil
51
+ end
52
+
53
+ def string_formatter_config
54
+ @string_formatter_config ||= build_string_formatter_config
55
+ end
56
+
57
+ def build_string_formatter_config
58
+ config_content = File.read(string_formatter_config_file)
59
+ config = JSON.parse(config_content)
60
+ configured_formatters = config['formatters']
61
+ configured_formatters.each_with_object({}) do |formatter, result|
62
+ require formatter['require_path']
63
+ helper_instance = Object.const_get(formatter['class_namespace']).new
64
+
65
+ formatter['aliases'].each do |name|
66
+ result[name.to_sym] = helper_instance
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,58 @@
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/integer/inflections'
5
+
6
+ require 'handlebars/helpers/base_helper'
7
+
8
+ module Handlebars
9
+ module Helpers
10
+ # Inflection handling routines, eg. pluralize, singular, ordinalize
11
+ module Inflection
12
+ # Ordinal: The suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th
13
+ class Ordinal < Handlebars::Helpers::BaseHelper
14
+ # Parse will Ordinal: The suffix that should be added to a number to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th
15
+ #
16
+ # @example
17
+ #
18
+ # puts Ordinal.new.parse('1')
19
+ #
20
+ # st
21
+ #
22
+ # @example
23
+ #
24
+ # puts Ordinal.new.parse('2')
25
+ #
26
+ # nd
27
+ #
28
+ # @example
29
+ #
30
+ # puts Ordinal.new.parse('3')
31
+ #
32
+ # rd
33
+ #
34
+ # @example
35
+ #
36
+ # puts Ordinal.new.parse('4')
37
+ #
38
+ # th
39
+ #
40
+ # @param [String] value - numeric value
41
+ # @return [String] ordinal suffix that would be required for a number
42
+ def parse(value)
43
+ return '' if value.nil?
44
+
45
+ value = value.to_i if value.is_a? String
46
+
47
+ value.ordinal
48
+ end
49
+
50
+ def handlebars_helper
51
+ proc do |_context, value|
52
+ wrapper(parse(value))
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,59 @@
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/inflector/inflections'
5
+ require 'active_support/core_ext/integer/inflections'
6
+
7
+ require 'handlebars/helpers/base_helper'
8
+
9
+ module Handlebars
10
+ module Helpers
11
+ # Inflection handling routines, eg. pluralize, singular, ordinalize
12
+ module Inflection
13
+ # Ordinalize: Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
14
+ class Ordinalize < Handlebars::Helpers::BaseHelper
15
+ # Parse will Ordinalize: Turns a number into an ordinal string used to denote the position in an ordered sequence such as 1st, 2nd, 3rd, 4th.
16
+ #
17
+ # @example
18
+ #
19
+ # puts Ordinalize.new.parse('1')
20
+ #
21
+ # 1st
22
+ #
23
+ # @example
24
+ #
25
+ # puts Ordinalize.new.parse('2')
26
+ #
27
+ # 2nd
28
+ #
29
+ # @example
30
+ #
31
+ # puts Ordinalize.new.parse('3')
32
+ #
33
+ # 3rd
34
+ #
35
+ # @example
36
+ #
37
+ # puts Ordinalize.new.parse('4')
38
+ #
39
+ # 4th
40
+ #
41
+ # @param [String] value - numeric value
42
+ # @return [String] number value turned to 1st, 2nd, 3rd, 4th etc.
43
+ def parse(value)
44
+ return '' if value.nil?
45
+
46
+ value = value.to_i if value.is_a? String
47
+
48
+ value.ordinalize
49
+ end
50
+
51
+ def handlebars_helper
52
+ proc do |_context, value|
53
+ wrapper(parse(value))
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -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
+ # Inflection handling routines, eg. pluralize, singular, ordinalize
11
+ module Inflection
12
+ # Returns the plural form of the word in the string
13
+ class Pluralize < Handlebars::Helpers::BaseHelper
14
+ # Parse will Returns the plural form of the word in the string
15
+ #
16
+ # @example
17
+ #
18
+ # puts Pluralize.new.parse('name')
19
+ #
20
+ # names
21
+ #
22
+ # puts Pluralize.new.parse('octopus')
23
+ #
24
+ # octopi
25
+ #
26
+ #
27
+ # @return [String] value in plural form
28
+ def parse(value)
29
+ return '' if value.nil?
30
+
31
+ value.pluralize
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,60 @@
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
+ # Inflection handling routines, eg. pluralize, singular, ordinalize
11
+ module Inflection
12
+ # Pluralize By Number: uses both a word and number to decide if the plural or singular form should be used.
13
+ class PluralizeByNumber < Handlebars::Helpers::BaseHelper
14
+ # Parse will Pluralize By Number: uses both a word and number to decide if the plural or singular form should be used.
15
+ #
16
+ # @example
17
+ #
18
+ # puts PluralizeByNumber.new.parse('category', '3', 'number_word')
19
+ #
20
+ # 3 categories
21
+ #
22
+ # @example
23
+ #
24
+ # puts PluralizeByNumber.new.parse('category', '3', 'word' )
25
+ #
26
+ # categories
27
+ #
28
+ # puts PluralizeByNumber.new.parse('category', '1' )
29
+ #
30
+ # category
31
+ #
32
+ # @param [String] value - value to pluralize
33
+ # @param [Integer] count - count used to determine pluralization
34
+ # @param [String] format - (Optional) what format should output be. :word, :number_word
35
+ # @return [String] value and number are used to calculate plural/singular form
36
+ def parse(value, count, format)
37
+ return '' if value.nil?
38
+
39
+ count = count.to_i if count.is_a? String
40
+ format = :word if format.nil?
41
+
42
+ case format.to_sym
43
+ when :number_word, :number_and_word
44
+ "#{count} #{value.pluralize(count)}"
45
+ else # aka :word
46
+ value.pluralize(count)
47
+ end
48
+ end
49
+
50
+ def handlebars_helper
51
+ proc do |_context, value, count, format|
52
+ # Handle optional: format
53
+ format = nil if value.is_a?(V8::Object)
54
+ wrapper(parse(value, count, format))
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,35 @@
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
+ # Inflection handling routines, eg. pluralize, singular, ordinalize
11
+ module Inflection
12
+ # The reverse of #pluralize, returns the singular form of a word in a string
13
+ class Singularize < Handlebars::Helpers::BaseHelper
14
+ # Parse will reverse of #pluralize, returns the singular form of a word in a string
15
+ #
16
+ # @example
17
+ #
18
+ # puts Singularize.new.parse('names')
19
+ #
20
+ # name
21
+ #
22
+ # puts Singularize.new.parse('octopi')
23
+ #
24
+ # octopus
25
+ #
26
+ # @return [String] plural value turned to singular value
27
+ def parse(value)
28
+ return '' if value.nil?
29
+
30
+ value.singularize
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end