Class: Handlebars::Helpers::BaseHelper
- Inherits:
-
Object
- Object
- Handlebars::Helpers::BaseHelper
- Defined in:
- lib/handlebars/helpers/base_helper.rb
Overview
base helper provides an interface to wrap your parsing logic in a Handlebars aware context
Direct Known Subclasses
BaseSafeStringHelper, CodeRuby::Classify, CodeRuby::Deconstantize, CodeRuby::Demodulize, CodeRuby::ForeignKey, CodeRuby::Tableize, Comparison::And, Comparison::Eq, Comparison::Gt, Comparison::Gte, Comparison::Lt, Comparison::Lte, Comparison::Ne, Comparison::Or, Inflection::Ordinal, Inflection::Ordinalize, Inflection::Pluralize, Inflection::PluralizeByNumber, Inflection::Singularize, Misc::Noop, StringFormatting::BackSlash, StringFormatting::Camel, StringFormatting::Constantize, StringFormatting::Dasherize, StringFormatting::Dotirize, StringFormatting::DoubleColon, StringFormatting::Downcase, StringFormatting::FormatAs, StringFormatting::Humanize, StringFormatting::Lamel, StringFormatting::Pluserize, StringFormatting::Singularize, StringFormatting::Slash, StringFormatting::Snake, StringFormatting::Titleize, StringFormatting::Upcase
Instance Method Summary collapse
-
#handlebars_helper ⇒ Object
Wrap the parse method in a handlebars context aware block that is used during registration.
-
#parse(value) ⇒ Object
All child classes will generally implement this method.
-
#parse_json(value, hash = {}) ⇒ Object
This needs to be in a data_helper rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Style/HashEachMethods.
-
#struct_to_hash(data) ⇒ Object
Needs to move into a GEM and make sure I have consistency.
-
#tokenizer ⇒ Object
String tokenizer will clean up a string so that all sorts of case formatted strings can be represented in a consistent fashion.
-
#wrapper(value) ⇒ Object
If you need to wrap the return value in a specific Handlebars Type, eg.
Instance Method Details
#handlebars_helper ⇒ Object
Wrap the parse method in a handlebars context aware block that is used during registration
12 13 14 |
# File 'lib/handlebars/helpers/base_helper.rb', line 12 def proc { |_context, value| wrapper(parse(value)) } end |
#parse(value) ⇒ Object
All child classes will generally implement this method
17 18 19 |
# File 'lib/handlebars/helpers/base_helper.rb', line 17 def parse(value) value end |
#parse_json(value, hash = {}) ⇒ Object
This needs to be in a data_helper rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Style/HashEachMethods
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/handlebars/helpers/base_helper.rb', line 37 def parse_json(value, hash = {}) return value.map { |item| parse_json(item) } if value.is_a?(V8::Array) return struct_to_hash(value) if value.is_a?(OpenStruct) return value unless value.is_a?(V8::Object) value.keys.each do |key| hash[key] = case value[key] when V8::Object parse_json(value[key]) when V8::Array value[key].map do |item| case item when V8::Object, V8::Array parse_json(item) when String item else item.values end end else value[key] end end hash end |
#struct_to_hash(data) ⇒ Object
Needs to move into a GEM and make sure I have consistency
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/handlebars/helpers/base_helper.rb', line 69 def struct_to_hash(data) # No test yet if data.is_a?(Array) return data.map { |v| v.is_a?(OpenStruct) ? struct_to_hash(v) : v } end data.each_pair.with_object({}) do |(key, value), hash| case value when OpenStruct hash[key] = struct_to_hash(value) when Array # No test yet values = value.map { |v| v.is_a?(OpenStruct) ? struct_to_hash(v) : v } hash[key] = values else hash[key] = value end end end |
#tokenizer ⇒ Object
String tokenizer will clean up a string so that all sorts of case formatted strings can be represented in a consistent fashion
31 32 33 |
# File 'lib/handlebars/helpers/base_helper.rb', line 31 def tokenizer @_tokenizer ||= ::Helpers.configuration.tokenizer end |
#wrapper(value) ⇒ Object
If you need to wrap the return value in a specific Handlebars Type, eg. SafeString, then you can override this method
24 25 26 |
# File 'lib/handlebars/helpers/base_helper.rb', line 24 def wrapper(value) value end |