Module: Opal::Connect::ConnectPlugins::Base::ClassMethods
- Included in:
- Opal::Connect
- Defined in:
- lib/opal/connect.rb
Instance Method Summary collapse
- #build(code, stubs = false) ⇒ Object
- #builder(stubs = false) ⇒ Object
- #included_files ⇒ Object
- #javascript(klass, method, *opts) ⇒ Object
-
#plugin(plugin, *args, &block) ⇒ Object
Load a new plugin into the current class.
- #plugin_paths ⇒ Object
- #read_file(file) ⇒ Object
- #render(method, *args, &block) ⇒ Object
- #setup(&block) ⇒ Object
- #write_entry_file(klass = false, method = false, *options) ⇒ Object
- #write_file(name, code, current_version, stubs = false) ⇒ Object
Instance Method Details
#build(code, stubs = false) ⇒ Object
246 247 248 |
# File 'lib/opal/connect.rb', line 246 def build(code, stubs = false) builder(stubs).build_str(code, '(inline)').to_s end |
#builder(stubs = false) ⇒ Object
239 240 241 242 243 244 |
# File 'lib/opal/connect.rb', line 239 def builder(stubs = false) Opal::Builder.new( stubs: stubs || Connect.stubbed_files, compiler_options: { dynamic_require_severity: :ignore } ) end |
#included_files ⇒ Object
235 236 237 |
# File 'lib/opal/connect.rb', line 235 def included_files @_included_files ||= [] end |
#javascript(klass, method, *opts) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/opal/connect.rb', line 250 def javascript(klass, method, *opts) return unless klass js = [] [:javascript].uniq.each { |block| js << klass.instance_exec(&block) } %{ Document.ready? do #{js.join(';')} klass = #{klass.class.name}.new klass.__send__(:#{method}, *JSON.parse(Base64.decode64('#{Base64.encode64 opts.to_json}'))) if klass.respond_to?(:#{method}) end } end |
#plugin(plugin, *args, &block) ⇒ Object
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/opal/connect.rb', line 201 def plugin(plugin, *args, &block) included = (args.first == :included) ? args.shift : false raise ConnectError, "Cannot add a plugin to a frozen Connect class" if RUBY_ENGINE != 'opal' && frozen? if plugin.is_a?(Symbol) Connect.[:plugins_loaded] << plugin Connect.[:plugins] << plugin unless Connect.[:plugins].include? plugin plugin = ConnectPlugins.load_plugin(plugin) end plugin.load_dependencies(self, *args, &block) if !included && plugin.respond_to?(:load_dependencies) return unless plugin include(plugin::InstanceMethods) if defined?(plugin::InstanceMethods) extend(plugin::ClassMethods) if defined?(plugin::ClassMethods) unless included Connect.extend(plugin::ConnectClassMethods) if defined?(plugin::ConnectClassMethods) Connect.include(plugin::ConnectInstanceMethods) if defined?(plugin::ConnectInstanceMethods) Connect.instance_exec(plugin, &plugin::ConnectSetup) if defined?(plugin::ConnectSetup) unless RUBY_ENGINE == 'opal' Connect.[:javascript] << plugin::ConnectJavascript if defined?(plugin::ConnectJavascript) end end plugin.configure(self, *args, &block) if !included && plugin.respond_to?(:configure) nil end |
#plugin_paths ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/opal/connect.rb', line 310 def plugin_paths plugins_path = Connect.[:plugins_path] plugins = [] ConnectPlugins.plugins.each do |name, _| if plugins_path && File.exist?("#{plugins_path}/#{name}.rb") plugins << "#{plugins_path}/#{name}" else plugins << "opal/connect/plugins/#{name}" end end Connect.[:requires].each { |plugin_path| plugins << plugin_path } plugins end |
#read_file(file) ⇒ Object
306 307 308 |
# File 'lib/opal/connect.rb', line 306 def read_file(file) File.read "#{Dir.pwd}/.connect/#{file}" end |
#render(method, *args, &block) ⇒ Object
182 183 184 |
# File 'lib/opal/connect.rb', line 182 def render(method, *args, &block) new.render(method, *args, &block) end |
#setup(&block) ⇒ Object
186 187 188 189 190 191 192 193 |
# File 'lib/opal/connect.rb', line 186 def setup(&block) if block_given? @_setup_block = block Connect.[:setup_blocks][self] = @_setup_block end @_setup_block end |
#write_entry_file(klass = false, method = false, *options) ⇒ Object
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/opal/connect.rb', line 265 def write_entry_file(klass = false, method = false, *) js = [] path = "#{Dir.pwd}/.connect" files = Connect.included_files.dup.uniq.map { |file| "require '#{file}'" }.join(';') entry = Connect.[:entry] = Base64.encode64 Connect..to_json plugins = plugin_paths.dup.map { |plugin_path| plugin_path = "require '#{plugin_path}'" }.join(';') Connect.[:javascript].uniq.each { |block| js << klass.instance_exec(&block) } if klass entry_code = %{ require 'opal-connect' #{plugins} options = JSON.parse(Base64.decode64('#{}')) options.each { |key, value| Opal::Connect.options[key] = value } #{entry} #{files} # make sure we include the default plugins with connect Opal::Connect.options[:plugins].each { |plug| Opal::Connect.plugin plug } Opal::Connect.setup #{js.join(';')} } FileUtils.mkdir_p(path) File.write("#{path}/entry.rb", entry_code) Connect.files.each { |name, (code, version, stubs)| write_file name, code, version, stubs } end |
#write_file(name, code, current_version, stubs = false) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/opal/connect.rb', line 294 def write_file(name, code, current_version, stubs = false) path = "#{Dir.pwd}/.connect" version_path = "#{path}/#{name}_version" version = File.exist?(version_path) ? File.read(version_path) : false save_path = "#{path}/#{name}.js" if !File.exist?(save_path) || !version || (version && version != current_version) File.write version_path, current_version File.write(save_path, build(code, stubs)) end end |