How to use section method of ClassMethods Package

Best Howitzer_ruby code snippet using ClassMethods.section

template.rb

Source:template.rb Github

copy

Full Screen

2require 'erb'3module YARD4 module Templates5 module Template6 attr_accessor :class, :section7 attr_reader :options8 class << self9 # Extra includes are mixins that are included after a template is created. These10 # mixins can be registered by plugins to operate on templates and override behaviour.11 #12 # Note that this array can be filled with modules or proc objects. If a proc object13 # is given, the proc will be called with the {Template#options} hash containing14 # relevant template information like the object, format, and more. The proc should15 # return a module or nil if there is none.16 #17 # @example Adding in extra mixins to include on a template18 # Template.extra_includes << MyHelper19 # @example Conditionally including a mixin if the format is html20 # Template.extra_includes << proc {|opts| MyHelper if opts.format == :html }21 # @return [Array<Module, Proc>] a list of modules to be automatically included22 # into any new template module23 attr_accessor :extra_includes24 # @!parse extend ClassMethods25 # @private26 def included(klass)27 klass.extend(ClassMethods)28 end29 # Includes the {extra_includes} modules into the template object.30 #31 # @param [Template] template the template object to mixin the extra includes.32 # @param [SymbolHash] options the options hash containing all template information33 # @return [void]34 def include_extra(template, options)35 extra_includes.each do |mod|36 mod = mod.call(options) if mod.is_a?(Proc)37 next unless mod.is_a?(Module)38 template.extend(mod)39 end40 end41 end42 self.extra_includes = [43 proc do |options|44 {:html => Helpers::HtmlHelper,45 :text => Helpers::TextHelper,46 :dot => Helpers::UMLHelper}[options.format]47 end48 ]49 include ErbCache50 include Helpers::BaseHelper51 include Helpers::MethodHelper52 module ClassMethods53 attr_accessor :path, :full_path54 # @return [Array<String>] a list of full paths55 # @note This method caches path results. Paths should not be modified56 # after this method is called; call {#reset_full_paths} to reset cache.57 def full_paths58 reset_full_paths unless defined? @cached_included_modules59 return @full_paths if included_modules == @cached_included_modules60 @cached_included_modules = included_modules61 @full_paths = included_modules.inject([full_path]) do |paths, mod|62 paths |= mod.full_paths if mod.respond_to?(:full_paths)63 paths64 end65 end66 # Resets cache for {#full_paths}67 def reset_full_paths68 @cached_included_modules = nil69 end70 def initialize(path, full_paths)71 full_path = full_paths.shift72 self.path = path73 self.full_path = full_path74 include_inherited(full_paths)75 include_parent76 load_setup_rb77 end78 # Searches for a file identified by +basename+ in the template's79 # path as well as any mixed in template paths. Equivalent to calling80 # {ClassMethods#find_nth_file} with index of 1.81 #82 # @param [String] basename the filename to search for83 # @return [String] the full path of a file on disk with filename84 # +basename+ in one of the template's paths.85 # @see find_nth_file86 def find_file(basename)87 find_nth_file(basename)88 end89 # Searches for the nth file (where n = +index+) identified90 # by basename in the template's path and any mixed in template paths.91 #92 # @param [String] basename the filename to search for93 # @param [Fixnum] index the nth existing file to return94 # @return [String] the full path of the nth file on disk with95 # filename +basename+ in one of the template paths96 def find_nth_file(basename, index = 1)97 n = 198 full_paths.each do |path|99 file = File.join(path, basename)100 if File.file?(file)101 return file if index == n102 n += 1103 end104 end105 nil106 end107 def is_a?(klass)108 return true if klass == Template109 super(klass)110 end111 # Creates a new template object to be rendered with {Template#run}112 def new(*args)113 obj = Object.new.extend(self)114 obj.class = self115 obj.send(:initialize, *args)116 obj117 end118 def run(*args)119 new(*args).run120 end121 # rubocop:disable Style/MethodName122 # Alias for creating {Engine.template}.123 def T(*path)124 Engine.template(*path)125 end126 # Alias for creating a {Section} with arguments127 # @see Section#initialize128 # @since 0.6.0129 def S(*args)130 Section.new(*args)131 end132 # rubocop:enable Style/MethodName133 private134 def include_parent135 pc = path.to_s.split('/')136 if pc.size > 1137 pc.pop138 pc = pc.join('/')139 begin140 include Engine.template(pc)141 rescue ArgumentError142 include Engine.template!(pc, full_path.gsub(%r{/[^/]+$}, ''))143 end144 end145 end146 def include_inherited(full_paths)147 full_paths.reverse.each do |full_path|148 include Engine.template!(path, full_path)149 end150 end151 def load_setup_rb152 setup_file = File.join(full_path, 'setup.rb')153 if File.file? setup_file154 module_eval(File.read(setup_file).taint, setup_file, 1)155 end156 end157 end158 def initialize(opts = TemplateOptions.new)159 opts_class = opts.class160 opts_class = TemplateOptions if opts_class == Hash161 @cache = {}162 @cache_filename = {}163 @sections = []164 @options = opts_class.new165 add_options(opts)166 Template.include_extra(self, options)167 init168 end169 # Loads a template specified by path. If +:template+ or +:format+ is170 # specified in the {#options} hash, they are prepended and appended171 # to the path respectively.172 #173 # @param [Array<String, Symbol>] path the path of the template174 # @return [Template] the loaded template module175 def T(*path) # rubocop:disable Style/MethodName176 path.unshift(options.template) if options.template177 path.push(options.format) if options.format178 self.class.T(*path)179 end180 # Sets the sections (and subsections) to be rendered for the template181 #182 # @example Sets a set of erb sections183 # sections :a, :b, :c # searches for a.erb, b.erb, c.erb184 # @example Sets a set of method and erb sections185 # sections :a, :b, :c # a is a method, the rest are erb files186 # @example Sections with subsections187 # sections :header, [:name, :children]188 # # the above will call header.erb and only renders the subsections189 # # if they are yielded by the template (see #yieldall)190 # @param [Array<Symbol, String, Template, Array>] args the sections191 # to use to render the template. For symbols and strings, the192 # section will be executed as a method (if one exists), or rendered193 # from the file "name.erb" where name is the section name. For194 # templates, they will have {Template::ClassMethods#run} called on them.195 # Any subsections can be yielded to using yield or {#yieldall}196 def sections(*args)197 @sections = Section.new(nil, *args) unless args.empty?198 @sections199 end200 # Initialization called on the template. Override this in a 'setup.rb'201 # file in the template's path to implement a template202 #203 # @example A default set of sections204 # def init205 # sections :section1, :section2, [:subsection1, :etc]206 # end207 # @see #sections208 def init209 end210 # Runs a template on +sects+ using extra options. This method should211 # not be called directly. Instead, call the class method {ClassMethods#run}212 #213 # @param [Hash, nil] opts any extra options to apply to sections214 # @param [Section, Array] sects a section list of sections to render215 # @param [Fixnum] start_at the index in the section list to start from216 # @param [Boolean] break_first if true, renders only the first section217 # @yield [opts] calls for the subsections to be rendered218 # @yieldparam [Hash] opts any extra options to yield219 # @return [String] the rendered sections joined together220 def run(opts = nil, sects = sections, start_at = 0, break_first = false, &block)221 out = String.new("")222 return out if sects.nil?223 sects = sects[start_at..-1] if start_at > 0224 sects = Section.new(nil, sects) unless sects.is_a?(Section)225 add_options(opts) do226 sects.each do |s|227 self.section = s228 subsection_index = 0229 value = render_section(section) do |*args|230 value = with_section do231 run(args.first, section, subsection_index, true, &block)232 end233 subsection_index += 1234 value235 end236 out << (value || "")237 break if break_first238 end239 end240 out241 end242 # Yields all subsections with any extra options243 #244 # @param [Hash] opts extra options to be applied to subsections245 def yieldall(opts = nil, &block)246 with_section { run(opts, section, &block) }247 end248 # @param [String, Symbol] section the section name249 # @yield calls subsections to be rendered250 # @return [String] the contents of the ERB rendered section251 def erb(section, &block)252 method_name = ErbCache.method_for(cache_filename(section)) do253 erb_with(cache(section), cache_filename(section))254 end255 send(method_name, &block)256 end257 # Returns the contents of a file. If +allow_inherited+ is set to +true+,258 # use +{{{__super__}}}+ inside the file contents to insert the contents259 # of the file from an inherited template. For instance, if +templates/b+260 # inherits from +templates/a+ and file "test.css" exists in both directories,261 # both file contents can be retrieved by having +templates/b/test.css+ look262 # like:263 #264 # {{{__super__}}}265 # ...266 # body { css styles here }267 # p.class { other styles }268 #269 # @param [String] basename the name of the file270 # @param [Boolean] allow_inherited whether inherited templates can271 # be inserted with +{{{__super__}}}+272 # @return [String] the contents of a file identified by +basename+. All273 # template paths (including any mixed in templates) are searched for274 # the file275 # @see ClassMethods#find_file276 # @see ClassMethods#find_nth_file277 def file(basename, allow_inherited = false)278 file = self.class.find_file(basename)279 raise ArgumentError, "no file for '#{basename}' in #{self.class.path}" unless file280 data = IO.read(file)281 if allow_inherited282 superfile = self.class.find_nth_file(basename, 2)283 data.gsub!('{{{__super__}}}', superfile ? IO.read(superfile) : "")284 end285 data286 end287 # Calls the ERB file from the last inherited template with {#section}.erb288 #289 # @param [Symbol, String] sect if provided, uses a specific section name290 # @return [String] the rendered ERB file in any of the inherited template291 # paths.292 def superb(sect = section, &block)293 filename = self.class.find_nth_file(erb_file_for(sect), 2)294 return "" unless filename295 method_name = ErbCache.method_for(filename) { erb_with(IO.read(filename), filename) }296 send(method_name, &block)297 end298 def options=(value)299 @options = value300 set_ivars301 end302 def inspect303 "Template(#{self.class.path}) [section=#{section.name}]"304 end305 protected306 def erb_file_for(section)307 "#{section}.erb"308 end309 def erb_with(content, filename = nil)310 erb = ERB.new(content, nil, options.format == :text ? '<>' : nil)311 erb.filename = filename if filename312 erb313 end314 private315 def render_section(section, &block)316 section = section.name if section.is_a?(Section)317 case section318 when Section, String, Symbol319 if respond_to?(section)320 send(section, &block)321 else322 erb(section, &block)323 end324 when Module, Template325 section.run(options, &block) if section.is_a?(Template)326 end || ""327 end328 def cache(section)329 content = @cache[section.to_sym]330 return content if content331 file = cache_filename(section)332 @cache_filename[section.to_sym] = file333 raise ArgumentError, "no template for section '#{section}' in #{self.class.path}" unless file334 @cache[section.to_sym] = IO.read(file)335 end336 def cache_filename(section)337 @cache_filename[section.to_sym] ||=338 self.class.find_file(erb_file_for(section))339 end340 def set_ivars341 options.each do |k, v|342 instance_variable_set("@#{k}", v)343 end344 end345 def add_options(opts = nil)346 return(yield) if opts.nil? && block_given?347 cur_opts = options if block_given?348 self.options = options.merge(opts)349 if block_given?350 value = yield351 self.options = cur_opts352 value353 end354 end355 def with_section356 sect = section357 value = yield358 self.section = sect359 value360 end361 end362 end363end...

Full Screen

Full Screen

configurable.rb

Source:configurable.rb Github

copy

Full Screen

...12#13module Blacklight::ConfigurableExt14 include Blacklight::Lens15 # ===========================================================================16 # :section: Blacklight::Configurable overrides17 # ===========================================================================18 public19 # Instance methods for blacklight_config, so get a deep copy of the20 # class-level config.21 #22 # @return [Blacklight::Configuration]23 #24 # This method overrides:25 # @see Blacklight::Configurable#blacklight_config26 #27 def blacklight_config(name = nil)28 if name29 blacklight_config_for(name)30 elsif @blacklight_config31 @blacklight_config32 else33 Log.warn(__method__, '@blacklight_config unset')34 @blacklight_config = current_lens.blacklight_config35 end36 rescue => e37 Log.warn {38 [39 "[Configurable] #{e}",40 "class #{self.class}",41 "ancestors #{self.class.ancestors}"42 ].join("\n ")43 }44 raise e45 end46 # ===========================================================================47 # :section: Blacklight::Configurable::ClassMethods overrides48 # ===========================================================================49 public50 module ClassMethods51 include Blacklight::Configurable52 include Blacklight::Lens53 # =========================================================================54 # :section: Blacklight::Configurable::ClassMethods overrides55 # =========================================================================56 public57 # Lazy-load a deep_copy of superclass configuration if present (or a58 # default_configuration if not), which will be legacy load or new empty59 # config.60 #61 # Note that the @blacklight_config variable is a Ruby62 # "instance method on class object" that won't be automatically available63 # to subclasses, that's why we lazy load to "inherit" how we want.64 #65 # @param [String, Symbol, ..., nil] name66 #67 # @return [Blacklight::Configuration]68 #69 # This method overrides:70 # @see Blacklight::Configurable::ClassMethods#blacklight_config71 #72 def blacklight_config(name = nil)73 if name74 blacklight_config_for(name)75 else76 @blacklight_config ||= current_lens.blacklight_config77 end78 end79 # =========================================================================80 # :section:81 # =========================================================================82 public83 # Fields that are suppressed in the default inspection of a configuration.84 NO_INSPECT =85 %i(facet_fields index_fields show_fields search_fields sort_fields)86 # Generate an easier-to-read inspection of a configuration by coverting87 # Hash-like elements to Hash.88 #89 # @param [ActiveSupport::OrderedOptions] config90 # @param [TrueClass, FalseClass, nil] all91 #92 # @return [String]93 #94 def config_inspect(config, all = false)95 h = config.to_h96 h = h.except(*NO_INSPECT) unless all97 hashify(h).pretty_inspect.gsub(/:([^=\n]+)(=>)/, '\1 \2 ')98 end99 # =========================================================================100 # :section:101 # =========================================================================102 private103 # Covert Hash-like elements to Hash.104 #105 # @param [Object] value106 #107 # @return [Object]108 #109 def hashify(value)110 case value111 when Hash, OpenStruct112 value.to_h.map { |k, v| [hashify(k), hashify(v)] }.to_h113 when Array114 value.map { |v| hashify(v) }...

Full Screen

Full Screen

section

Using AI Code Generation

copy

Full Screen

1 def self.included(base)2 base.extend(ClassMethods)3 def section(name)4MyClass.section('one')5 def self.extended(base)6 base.extend(ClassMethods)7 def section(name)8MyClass.section('one')9 def self.extended(base)10 base.extend(ClassMethods)11 def section(name)12MyClass.section('one')13 def self.included(base)14 base.extend(ClassMethods)15 def section(name)16MyClass.section('one')17 def self.included(base)18 base.extend(ClassMethods)19 def section(name)20MyClass.section('one')21 def self.extended(base)22 base.extend(ClassMethods)23 def section(name)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful