Class: TDiary::Plugin

Inherits:
Object
  • Object
show all
Includes:
ERB::Util, ViewHelper
Defined in:
lib/tdiary/plugin.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ViewHelper

#base_url, #bot?

Constructor Details

#initialize(params) ⇒ Plugin

Returns a new instance of Plugin.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tdiary/plugin.rb', line 35

def initialize( params )
	@header_procs = []
	@footer_procs = []
	@update_procs = []
	@title_procs = []
	@body_enter_procs = []
	@body_leave_procs = []
	@section_index = {}
	@section_enter_procs = []
	@comment_leave_procs = []
	@subtitle_procs = []
	@section_leave_procs = []
	@edit_procs = []
	@form_procs = []
	@conf_keys = []
	@conf_procs = {}
	@conf_genre_label = {}
	@content_procs = {}
	@startup_procs = []
	@cookies = []
	@javascripts = {}
	@javascript_setting = []

	apply_request_params( params )

	# setup plugin storage
	unless @conf.io_class.method_defined?(:plugin_open)
		require 'tdiary/io/plugin_pstore'
		@conf.io_class.extend(TDiary::IO::PluginPStore)
	end
	@storage = @conf.io_class.plugin_open(@conf)

	# loading plugins
	@plugin_files = []
	plugin_path = @conf.plugin_path || "#{File.dirname(__FILE__)}/plugin"
	plugin_file = ''
	begin
		Dir::glob( "#{plugin_path}/*.rb" ).sort.each do |file|
			plugin_file = file
			load_plugin( file )
			@plugin_files << plugin_file
		end
	rescue ::TDiary::ForceRedirect
		raise
	rescue Exception
		raise PluginError::new( "Plugin error in '#{File::basename( plugin_file )}'.\n#{$!}\n#{$!.backtrace[0]}" )
	end

	# remember the post-load state so the instance can be reused for
	# another request without re-reading and re-parsing plugin files.
	snapshot_reusable_state
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*m) ⇒ Object (private)



426
427
428
429
# File 'lib/tdiary/plugin.rb', line 426

def method_missing( *m )
	super if @debug
	# ignore when no plugin
end

Instance Attribute Details

#comment=(value) ⇒ Object (writeonly)

Sets the attribute comment

Parameters:

  • value

    the value to set the attribute comment to.



13
14
15
# File 'lib/tdiary/plugin.rb', line 13

def comment=(value)
  @comment = value
end

#cookiesObject (readonly)

Returns the value of attribute cookies.



12
13
14
# File 'lib/tdiary/plugin.rb', line 12

def cookies
  @cookies
end

#date=(value) ⇒ Object (writeonly)

Sets the attribute date

Parameters:

  • value

    the value to set the attribute date to.



13
14
15
# File 'lib/tdiary/plugin.rb', line 13

def date=(value)
  @date = value
end

#diaries=(value) ⇒ Object (writeonly)

Sets the attribute diaries

Parameters:

  • value

    the value to set the attribute diaries to.



13
14
15
# File 'lib/tdiary/plugin.rb', line 13

def diaries=(value)
  @diaries = value
end

#last_modified=(value) ⇒ Object (writeonly)

Sets the attribute last_modified

Parameters:

  • value

    the value to set the attribute last_modified to.



13
14
15
# File 'lib/tdiary/plugin.rb', line 13

def last_modified=(value)
  @last_modified = value
end

Class Method Details

.cache_clearObject



30
31
32
# File 'lib/tdiary/plugin.rb', line 30

def cache_clear
	@instance_cache = {}
end

.cache_fetch(key) ⇒ Object



22
23
24
# File 'lib/tdiary/plugin.rb', line 22

def cache_fetch( key )
	( @instance_cache ||= {} )[key]
end

.cache_store(key, plugin) ⇒ Object



26
27
28
# File 'lib/tdiary/plugin.rb', line 26

def cache_store( key, plugin )
	( @instance_cache ||= {} )[key] = plugin
end

Instance Method Details

#eval_src(src) ⇒ Object



115
116
117
118
119
# File 'lib/tdiary/plugin.rb', line 115

def eval_src( src )
	ret = eval( src, binding, "(TDiary::Plugin#eval_src)", 1 )
	@conf.io_class.plugin_close(@storage)
	return ret
end

#load_plugin(file) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/tdiary/plugin.rb', line 100

def load_plugin( file )
	@resource_loaded = false
	begin
		res_file = File::dirname( file ) + "/#{@conf.lang}/" + File::basename( file )
		open( res_file ) do |src|
			instance_eval( src.read, "(plugin/#{@conf.lang}/#{File::basename( res_file )})", 1 )
		end
		@resource_loaded = true
	rescue IOError, Errno::ENOENT
	end
	File::open( file ) do |src|
		instance_eval( src.read, "(plugin/#{File::basename( file )})", 1 )
	end
end

#prepare_for_reuse(params) ⇒ Object

Re-use a fully-loaded plugin instance for a new request. Refreshes the request-scoped data and rolls the per-request accumulators back to the state right after plugins were loaded. This skips load_plugin (file read + instance_eval), which is the dominant cost of rendering.



94
95
96
97
98
# File 'lib/tdiary/plugin.rb', line 94

def prepare_for_reuse( params )
	apply_request_params( params )
	restore_reusable_state
	self
end