class Vue::Helpers::VueObject
NOTE: Vue
components can be called MULTIPLE times, so we can't store the calling args OR the block here.
But note that Vue
root-apps can only be called once, so should we continue to store the vue-root calling args & block here, or pass them in at run-time as well? I think it ALL has to be dynamic.
Attributes
attr_accessor :repo, *defaults.keys
Public Class Methods
Creates custom attr readers that tie in to defaults.
# File lib/vue/helpers/vue_object.rb, line 48 def self.custom_attr_reader(*args) args.each do |a| define_method(a) do |use_default=true| get_attribute(a, use_default) end end end
Concatenates subclass defaults with master class defaults.
# File lib/vue/helpers/vue_object.rb, line 42 def self.defaults super_defaults = superclass.singleton_class.method_defined?(__method__) ? superclass.defaults : (@defaults || {}) super_defaults.merge(@defaults || {}) end
# File lib/vue/helpers/vue_object.rb, line 76 def initialize(name, **options) @name = name #puts "VueObject created: #{name}, self: #{self}" initialize_options(**options) end
Public Instance Methods
# File lib/vue/helpers/vue_object.rb, line 155 def context repo.context end
Internal methods
# File lib/vue/helpers/vue_object.rb, line 62 def defaults self.class.defaults end
Get attribute, considering upstream possibilities.
# File lib/vue/helpers/vue_object.rb, line 181 def get_attribute(attribute, use_default=true) case when ( val = instance_variable_get("@#{attribute}"); !val.nil? ); val when type != 'root' && root.respond_to?(attribute) && ( val = root.send(attribute, use_default); !val.nil? ); val when use_default && Vue::Helpers.respond_to?(attribute) && ( val = Vue::Helpers.send(attribute); !val.nil? ); val end end
# File lib/vue/helpers/vue_object.rb, line 82 def initialize_options(**options) @repo ||= options.delete(:repo) #locals = options.delete(:locals) || {} #puts "\n#{self.class.name}.initialize_options '#{name}' #{options.inspect}" return self unless options.size > 0 && !@initialized locals = options.delete(:locals) || {} #puts "\n#{self.class.name}.initialize_options '#{name}', #{options.inspect}, locals:#{locals.inspect}" merged_options = defaults.dup.merge(options) # Sets each default ivar, unless ivar is already non-nil. merged_options.each do |k,v| #puts "Setting ivar '#{k}' with '#{v}', was previously '#{instance_variable_get('@' + k.to_s)}'" #instance_variable_set("@#{k}", v) if v && instance_variable_get("@#{k}").nil? #!(v.respond_to?(:empty) && v.empty?) instance_variable_set("@#{k}", v) if instance_variable_get("@#{k}").nil? end @file_name ||= @name #load_dot_vue if file_name load_tilt_template if file_name #&& !tilt_template # We need this to discover subcomponents, otherwise # vue_app won't know about them until it's too late. render_template(**locals) #puts "\n#{self.class.name} initialized." #print_ivars @initialized = true #puts "VueObject initialized options: #{name}, self: #{self}" self end
# File lib/vue/helpers/vue_object.rb, line 168 def js_var_name name.to_s.camelize end
Loads a dot-vue into a tilt template, but doesn't render or parse it.
# File lib/vue/helpers/vue_object.rb, line 117 def load_tilt_template self.tilt_template = context.load_template(file_name.to_sym, template_engine:template_engine(false)) end
Parses a rendered sfc file. Returns [nil, template-as-html, nil, script-as-js]. Must be HTML (already rendered from ruby template).
# File lib/vue/helpers/vue_object.rb, line 134 def parse_sfc(**locals) @parsed_sfc ||= ( #rendered_template = render_template(**locals) rslt = {} rslt[:template], rslt[:script] = render_template(**locals).to_s.match(/(.*<template>(.*)<\/template>)*.*(<script>(.*)<\/script>)/m).to_a.values_at(2,4) rslt ) end
# File lib/vue/helpers/vue_object.rb, line 149 def parsed_script(**locals) @parsed_script ||= ( parse_sfc(**locals)[:script] ) end
# File lib/vue/helpers/vue_object.rb, line 143 def parsed_template(**locals) @parsed_template ||= ( parse_sfc(**locals)[:template] ) end
For debugging.
# File lib/vue/helpers/vue_object.rb, line 67 def print_ivars puts "\nIVARS:" instance_variables.each do |v| unless v.to_s[/repo/i] puts "#{v}: #{instance_variable_get(v)}" end end end
Renders loaded tilt_template. TODO: Find a way to easily insert attributes like 'name', 'root_name', etc. into locals.
Or just insert the current vue object instance into the locals
# File lib/vue/helpers/vue_object.rb, line 124 def render_template(**locals) @rendered_template ||= ( #puts "\n#{self.class.name} '#{name}' calling render_template with tilt_template: #{tilt_template&.file}, engine: #{template_engine}, locals: #{locals}" context.render_ruby_template(tilt_template, template_engine:template_engine(false), locals:locals.merge(vo:self, vue_object:self)) if !tilt_template.to_s.empty? ) end
Gets a defined wrapper, and interpolates it with the given locals & options.
# File lib/vue/helpers/vue_object.rb, line 163 def wrapper(wrapper_name, **locals) #, **wrapper_options) #Vue::Helpers.send(wrapper_name).interpolate(**wrapper_options.merge(locals)) Vue::Helpers.send(wrapper_name).interpolate(locals) end