handlebars-helpers 0.0.5 → 0.0.65

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ruby.yml +3 -3
  3. data/.gitignore +1 -0
  4. data/.handlebars_helpers.json +901 -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/Rakefile +1 -0
  10. data/STORIES.md +44 -6
  11. data/handlebars-helpers.gemspec +2 -0
  12. data/lib/handlebars/helpers.rb +2 -0
  13. data/lib/handlebars/helpers/base_helper.rb +36 -0
  14. data/lib/handlebars/helpers/base_safe_string_helper.rb +17 -0
  15. data/lib/handlebars/helpers/code_ruby/classify.rb +40 -0
  16. data/lib/handlebars/helpers/code_ruby/deconstantize.rb +48 -0
  17. data/lib/handlebars/helpers/code_ruby/demodulize.rb +58 -0
  18. data/lib/handlebars/helpers/code_ruby/foreign_key.rb +49 -0
  19. data/lib/handlebars/helpers/code_ruby/tableize.rb +35 -0
  20. data/lib/handlebars/helpers/comparison/and.rb +44 -0
  21. data/lib/handlebars/helpers/comparison/default.rb +66 -0
  22. data/lib/handlebars/helpers/comparison/eq.rb +35 -0
  23. data/lib/handlebars/helpers/comparison/gt.rb +35 -0
  24. data/lib/handlebars/helpers/comparison/gte.rb +43 -0
  25. data/lib/handlebars/helpers/comparison/lt.rb +35 -0
  26. data/lib/handlebars/helpers/comparison/lte.rb +43 -0
  27. data/lib/handlebars/helpers/comparison/ne.rb +35 -0
  28. data/lib/handlebars/helpers/comparison/or.rb +56 -0
  29. data/lib/handlebars/helpers/configuration.rb +72 -0
  30. data/lib/handlebars/helpers/inflection/ordinal.rb +58 -0
  31. data/lib/handlebars/helpers/inflection/ordinalize.rb +59 -0
  32. data/lib/handlebars/helpers/inflection/pluralize.rb +36 -0
  33. data/lib/handlebars/helpers/inflection/pluralize_by_number.rb +60 -0
  34. data/lib/handlebars/helpers/inflection/singularize.rb +35 -0
  35. data/lib/handlebars/helpers/register_helpers.rb +73 -0
  36. data/lib/handlebars/helpers/string_formatting/append_if.rb +42 -0
  37. data/lib/handlebars/helpers/string_formatting/back_slash.rb +33 -0
  38. data/lib/handlebars/helpers/string_formatting/camel.rb +29 -0
  39. data/lib/handlebars/helpers/string_formatting/constantize.rb +29 -0
  40. data/lib/handlebars/helpers/string_formatting/dasherize.rb +33 -0
  41. data/lib/handlebars/helpers/string_formatting/dotirize.rb +33 -0
  42. data/lib/handlebars/helpers/string_formatting/double_colon.rb +33 -0
  43. data/lib/handlebars/helpers/string_formatting/downcase.rb +31 -0
  44. data/lib/handlebars/helpers/string_formatting/format_as.rb +57 -0
  45. data/lib/handlebars/helpers/string_formatting/humanize.rb +39 -0
  46. data/lib/handlebars/helpers/string_formatting/lamel.rb +29 -0
  47. data/lib/handlebars/helpers/string_formatting/padl.rb +58 -0
  48. data/lib/handlebars/helpers/string_formatting/padr.rb +59 -0
  49. data/lib/handlebars/helpers/string_formatting/pluserize.rb +29 -0
  50. data/lib/handlebars/helpers/string_formatting/prepend_if.rb +42 -0
  51. data/lib/handlebars/helpers/string_formatting/singularize.rb +35 -0
  52. data/lib/handlebars/helpers/string_formatting/slash.rb +33 -0
  53. data/lib/handlebars/helpers/string_formatting/snake.rb +33 -0
  54. data/lib/handlebars/helpers/string_formatting/string.js +511 -0
  55. data/lib/handlebars/helpers/string_formatting/surround.rb +49 -0
  56. data/lib/handlebars/helpers/string_formatting/surround_if.rb +43 -0
  57. data/lib/handlebars/helpers/string_formatting/titleize.rb +39 -0
  58. data/lib/handlebars/helpers/string_formatting/upcase.rb +31 -0
  59. data/lib/handlebars/helpers/string_tokenizer.rb +43 -0
  60. data/lib/handlebars/helpers/template.rb +68 -0
  61. data/lib/handlebars/helpers/version.rb +1 -1
  62. metadata +82 -3
@@ -0,0 +1,49 @@
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
+ # Ruby code handling routines
11
+ module CodeRuby
12
+ # Foreign Key: Creates a foreign key name from a class name
13
+ # +separate_class_name_and_id_with_underscore+ sets whether
14
+ # the method should put '_' between the name and 'id'.
15
+ class ForeignKey < Handlebars::Helpers::BaseHelper
16
+ # Parse will Creates a foreign key name from a class name
17
+ #
18
+ # @example
19
+ #
20
+ # puts ForeignKey.new.parse('Message')
21
+ #
22
+ # message_id
23
+ #
24
+ # @example
25
+ #
26
+ # puts ForeignKey.new.parse('Message', class_id_underscored: false)
27
+ #
28
+ # messageid
29
+ #
30
+ # @example
31
+ #
32
+ # puts ForeignKey.new.parse('Admin::Post')
33
+ #
34
+ # post_id
35
+ #
36
+ # @param [String] class_name - name of class
37
+ # @param [Boolean] class_id_underscored: - optional named value (defaults to true) - is the class and ID to be separated by underscore?
38
+ # @return [String] value converted to separate_class_name_and_id_with_underscore
39
+ def parse(class_name, class_id_underscored: true)
40
+ class_name.foreign_key(class_id_underscored)
41
+ end
42
+
43
+ def handlebars_helper
44
+ proc { |_context, class_name, class_id_underscored| wrapper(parse(class_name, class_id_underscored: class_id_underscored)) }
45
+ end
46
+ end
47
+ end
48
+ end
49
+ 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
+ # Ruby code handling routines
11
+ module CodeRuby
12
+ # Tableize: Creates the name of a table like Rails does when converting models to table names
13
+ class Tableize < Handlebars::Helpers::BaseHelper
14
+ # Parse will creates the name of a table like Rails does when converting models to table names
15
+ #
16
+ # @example
17
+ #
18
+ # puts Tableize.new.parse('product category')
19
+ #
20
+ # product_categories
21
+ #
22
+ # @param [String] value - name of the table
23
+ # @return [String] value converted to table case with pluralization
24
+ def parse(value)
25
+ # tokenizer.parse(value, separator: '_', forced_separator: true).classify
26
+ tokenizer.parse(value).tableize
27
+ end
28
+
29
+ def handlebars_helper
30
+ proc { |_context, value| wrapper(parse(value)) }
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,44 @@
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
+ # And: Block helper that renders a block if **all of** the given values are truthy. If an inverse block is specified it will be rendered when falsy.
13
+ class And < Handlebars::Helpers::BaseHelper
14
+ # Parse will And: Block helper that renders a block if **all of** the given values are truthy. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # {{#if (and p1 p2 p3 p4 p5)}}
19
+ # found
20
+ # {{/if}}
21
+ #
22
+ # @example
23
+ #
24
+ # @example
25
+ # {{#if (and name age)}}
26
+ # {{name}}-{{age}}
27
+ # {{else}}
28
+ # no name or age
29
+ # {{/if}}
30
+ #
31
+ # @param values list of values (via *splat) to be checked via AND condition
32
+ # @return [String] return block when every value is truthy
33
+ def parse(values)
34
+ values.all? { |value| value }
35
+ end
36
+
37
+ def handlebars_helper
38
+ # Exclude last paramater which is the context V8::Object
39
+ proc { |_context, *values| wrapper(parse(values[0..-2])) }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,66 @@
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
+ # Comparison helpers, eg. or, and, equal, not equal, less than, greater than etc.
11
+ module Comparison
12
+ # Default: Returns the first value that is not nil or undefined, otherwise the 'default' value is returned.
13
+ class Default < Handlebars::Helpers::BaseSafeStringHelper
14
+ # Parse will Default: Returns the first value that is not nil or undefined, otherwise the 'default' value is returned.
15
+ #
16
+ # @example
17
+ #
18
+ # emotion = nil
19
+ # puts Default.new.parse(emotion, 'happy')
20
+ #
21
+ # happy
22
+ #
23
+ # @example
24
+ #
25
+ # emotion = 'sad'
26
+ # puts Default.new.parse(emotion, 'happy')
27
+ #
28
+ # sad
29
+ #
30
+ # @example
31
+ #
32
+ # david = nil
33
+ # lisa = nil
34
+ # ben = nil
35
+ # puts Default.new.parse(david, lisa, ben, 'happy')
36
+ #
37
+ # happy
38
+ #
39
+ # @example
40
+ #
41
+ # david = nil
42
+ # lisa = sad
43
+ # ben = mad
44
+ # puts Default.new.parse(david, lisa, ben, 'happy')
45
+ #
46
+ # sad
47
+ #
48
+ # @param [Object] *values - one or more paramaters that may or may not contain nil
49
+ # @param [String] default_value - the last paramater will be the default value
50
+ # @return [String] value or default value
51
+ def parse(values)
52
+ default_value = values[-1]
53
+
54
+ find_value = values[0..-2].find { |value| !value.nil? }
55
+
56
+ find_value || default_value
57
+ end
58
+
59
+ def handlebars_helper
60
+ # Exclude last paramater which is the context V8::Object
61
+ proc { |_context, *values| wrapper(parse(values[0..-2])) }
62
+ end
63
+ end
64
+ end
65
+ end
66
+ 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
+ # Eq: Block helper that renders a block if `a` is **equal to** `b`. If an inverse block is specified it will be rendered when falsy.
13
+ class Eq < Handlebars::Helpers::BaseHelper
14
+ # Parse will Eq: Block helper that renders a block if `a` is **equal to** `b`. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Eq.new.parse('aaa', 'aaa')
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 equals 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,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
+ # Gt: (greater than) Block helper that renders a block if `a` is **greater than** `b`. If an inverse block is specified it will be rendered when falsy.
13
+ class Gt < Handlebars::Helpers::BaseHelper
14
+ # Parse will Gt: (greater than) Block helper that renders a block if `a` is **greater than** `b`. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Gt.new.parse(2, 1)
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 GREATER THAN 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,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
+ # Gte: (greater than or equal to) Block helper that renders a block if `a` is **greater than** `b`. If an inverse block is specified it will be rendered when falsy.
13
+ class Gte < Handlebars::Helpers::BaseHelper
14
+ # Parse will Gte: (greater than or equal to) Block helper that renders a block if `a` is **greater than** `b`. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Gte.new.parse(1, 2)
19
+ #
20
+ # Falsey
21
+ #
22
+ # puts Gte.new.parse(1, 1)
23
+ #
24
+ # Truthy
25
+ #
26
+ # puts Gte.new.parse(2, 1)
27
+ #
28
+ # Truthy
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 GREATER 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
+ # Lt: (less than) Block helper that renders a block if `a` is **less than** `b`. If an inverse block is specified it will be rendered when falsy.
13
+ class Lt < Handlebars::Helpers::BaseHelper
14
+ # Parse will Lt: (less than) Block helper that renders a block if `a` is **less than** `b`. If an inverse block is specified it will be rendered when falsy.
15
+ #
16
+ # @example
17
+ #
18
+ # puts Lt.new.parse(1, 2)
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 LESS THAN 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,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