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
|
module LiquidDynamicContext
|
||||||
class Error < StandardError; end
|
class Error < StandardError; end
|
||||||
# Your code goes here...
|
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,20 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module LiquidDynamicContext
|
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 BindingResolver
|
||||||
class_attribute :bindings
|
class_attribute :bindings
|
||||||
self.bindings = Set.new
|
self.bindings = Set.new
|
||||||
@ -15,12 +29,23 @@ module LiquidDynamicContext
|
|||||||
context.to_h.deep_stringify_keys
|
context.to_h.deep_stringify_keys
|
||||||
end
|
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)
|
def needs_to_run?(used_bindings)
|
||||||
!(bindings & used_bindings.to_set).empty?
|
!(bindings & used_bindings.to_set).empty?
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
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)
|
def resolve(models, context)
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module LiquidDynamicContext
|
module LiquidDynamicContext
|
||||||
|
# Main entry point to resolve variables to bindings
|
||||||
class TemplateContext
|
class TemplateContext
|
||||||
attr_reader :binding_resolvers,
|
attr_reader :binding_resolvers,
|
||||||
:variable_finder
|
:variable_finder
|
||||||
@ -21,7 +22,8 @@ module LiquidDynamicContext
|
|||||||
# @param models [Any] an object with dynamic data you want to
|
# @param models [Any] an object with dynamic data you want to
|
||||||
# pass to your resolvers, can be nil
|
# 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)
|
def resolve(template_string, models)
|
||||||
binding_resolvers.select { |resolver| resolver.needs_to_run?(variables(template_string)) }
|
binding_resolvers.select { |resolver| resolver.needs_to_run?(variables(template_string)) }
|
||||||
.map { |resolver| resolver.call(models) }
|
.map { |resolver| resolver.call(models) }
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module LiquidDynamicContext
|
module LiquidDynamicContext
|
||||||
|
# Provides access to variables in a liquid template
|
||||||
class VariableFinder
|
class VariableFinder
|
||||||
|
|
||||||
# Extracts all used variables from a liquid template.
|
# Extracts all used variables from a liquid template.
|
||||||
#
|
#
|
||||||
# @param template_string [String] a string with a valid 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
|
# are represented as symbols
|
||||||
def find_variables(template_string)
|
def find_variables(template_string)
|
||||||
parsed_template = Liquid::Template.parse(template_string)
|
parsed_template = Liquid::Template.parse(template_string)
|
||||||
|
Loading…
Reference in New Issue
Block a user