Add missing documentation for classes
This commit is contained in:
parent
a653d99fe8
commit
a67f3468fe
@ -15,5 +15,4 @@ require_relative "liquid_dynamic_context/variable_finder"
|
||||
|
||||
module LiquidDynamicContext
|
||||
class Error < StandardError; end
|
||||
# Your code goes here...
|
||||
end
|
||||
|
@ -1,6 +1,20 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module LiquidDynamicContext
|
||||
# Abstract base class with convenience methods to
|
||||
# implement a resolver for bindings
|
||||
#
|
||||
# @example How to implement your own resolver
|
||||
# class MyResolver < LiquidDynamicContext
|
||||
# register_binding :username
|
||||
#
|
||||
# protected
|
||||
#
|
||||
# # Method that needs to be implemented by custom implementation
|
||||
# def resolve(models, context)
|
||||
# context.username = User.find(models[:user_id]).username
|
||||
# end
|
||||
# end
|
||||
class BindingResolver
|
||||
class_attribute :bindings
|
||||
self.bindings = Set.new
|
||||
@ -15,12 +29,23 @@ module LiquidDynamicContext
|
||||
context.to_h.deep_stringify_keys
|
||||
end
|
||||
|
||||
# Given a list of bindings in the template this method determines
|
||||
# whether the resolver needs to run
|
||||
#
|
||||
# @param used_bindings [Enumerable<Symbol>] a list of bindings that were used in
|
||||
# the template
|
||||
#
|
||||
# @return [Boolean] true or false depending on whether this resolver provides
|
||||
# any of the used bindings
|
||||
def needs_to_run?(used_bindings)
|
||||
!(bindings & used_bindings.to_set).empty?
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Method to implemented by subclasses
|
||||
#
|
||||
# @param models [Any] An object that can be used to provide dynamic content
|
||||
# @param context [Struct] A struct to assign the output of your resolver to
|
||||
def resolve(models, context)
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
@ -1,6 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module LiquidDynamicContext
|
||||
# Main entry point to resolve variables to bindings
|
||||
class TemplateContext
|
||||
attr_reader :binding_resolvers,
|
||||
:variable_finder
|
||||
@ -21,7 +22,8 @@ module LiquidDynamicContext
|
||||
# @param models [Any] an object with dynamic data you want to
|
||||
# pass to your resolvers, can be nil
|
||||
#
|
||||
# @return a hash with string keys that contains the bindings for the template
|
||||
# @return [Hash<String,Any>] a hash with string keys that contains the bindings
|
||||
# for the template
|
||||
def resolve(template_string, models)
|
||||
binding_resolvers.select { |resolver| resolver.needs_to_run?(variables(template_string)) }
|
||||
.map { |resolver| resolver.call(models) }
|
||||
|
@ -1,13 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module LiquidDynamicContext
|
||||
# Provides access to variables in a liquid template
|
||||
class VariableFinder
|
||||
|
||||
# Extracts all used variables from a liquid template.
|
||||
#
|
||||
# @param template_string [String] a string with a valid liquid template
|
||||
#
|
||||
# @return an enumerable with the variables found in the template, the variables
|
||||
# @return [Enumerable<Symbol>] an enumerable with the variables found in the template, the variables
|
||||
# are represented as symbols
|
||||
def find_variables(template_string)
|
||||
parsed_template = Liquid::Template.parse(template_string)
|
||||
|
Loading…
Reference in New Issue
Block a user