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.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +3 -3
- data/.gitignore +1 -0
- data/.handlebars_helpers.json +901 -0
- data/.handlebars_string_formatters.json +137 -0
- data/.rubocop.yml +4 -2
- data/.vscode/settings.json +6 -0
- data/Gemfile +3 -0
- data/Rakefile +1 -0
- data/STORIES.md +44 -6
- data/handlebars-helpers.gemspec +2 -0
- data/lib/handlebars/helpers.rb +2 -0
- data/lib/handlebars/helpers/base_helper.rb +36 -0
- data/lib/handlebars/helpers/base_safe_string_helper.rb +17 -0
- data/lib/handlebars/helpers/code_ruby/classify.rb +40 -0
- data/lib/handlebars/helpers/code_ruby/deconstantize.rb +48 -0
- data/lib/handlebars/helpers/code_ruby/demodulize.rb +58 -0
- data/lib/handlebars/helpers/code_ruby/foreign_key.rb +49 -0
- data/lib/handlebars/helpers/code_ruby/tableize.rb +35 -0
- data/lib/handlebars/helpers/comparison/and.rb +44 -0
- data/lib/handlebars/helpers/comparison/default.rb +66 -0
- data/lib/handlebars/helpers/comparison/eq.rb +35 -0
- data/lib/handlebars/helpers/comparison/gt.rb +35 -0
- data/lib/handlebars/helpers/comparison/gte.rb +43 -0
- data/lib/handlebars/helpers/comparison/lt.rb +35 -0
- data/lib/handlebars/helpers/comparison/lte.rb +43 -0
- data/lib/handlebars/helpers/comparison/ne.rb +35 -0
- data/lib/handlebars/helpers/comparison/or.rb +56 -0
- data/lib/handlebars/helpers/configuration.rb +72 -0
- data/lib/handlebars/helpers/inflection/ordinal.rb +58 -0
- data/lib/handlebars/helpers/inflection/ordinalize.rb +59 -0
- data/lib/handlebars/helpers/inflection/pluralize.rb +36 -0
- data/lib/handlebars/helpers/inflection/pluralize_by_number.rb +60 -0
- data/lib/handlebars/helpers/inflection/singularize.rb +35 -0
- data/lib/handlebars/helpers/register_helpers.rb +73 -0
- data/lib/handlebars/helpers/string_formatting/append_if.rb +42 -0
- data/lib/handlebars/helpers/string_formatting/back_slash.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/camel.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/constantize.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/dasherize.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/dotirize.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/double_colon.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/downcase.rb +31 -0
- data/lib/handlebars/helpers/string_formatting/format_as.rb +57 -0
- data/lib/handlebars/helpers/string_formatting/humanize.rb +39 -0
- data/lib/handlebars/helpers/string_formatting/lamel.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/padl.rb +58 -0
- data/lib/handlebars/helpers/string_formatting/padr.rb +59 -0
- data/lib/handlebars/helpers/string_formatting/pluserize.rb +29 -0
- data/lib/handlebars/helpers/string_formatting/prepend_if.rb +42 -0
- data/lib/handlebars/helpers/string_formatting/singularize.rb +35 -0
- data/lib/handlebars/helpers/string_formatting/slash.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/snake.rb +33 -0
- data/lib/handlebars/helpers/string_formatting/string.js +511 -0
- data/lib/handlebars/helpers/string_formatting/surround.rb +49 -0
- data/lib/handlebars/helpers/string_formatting/surround_if.rb +43 -0
- data/lib/handlebars/helpers/string_formatting/titleize.rb +39 -0
- data/lib/handlebars/helpers/string_formatting/upcase.rb +31 -0
- data/lib/handlebars/helpers/string_tokenizer.rb +43 -0
- data/lib/handlebars/helpers/template.rb +68 -0
- data/lib/handlebars/helpers/version.rb +1 -1
- metadata +82 -3
@@ -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
|
@@ -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
|