Module: Rubko::Base

Included in:
App, Controller, Model, Plugin
Defined in:
lib/rubko/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/rubko/base.rb', line 104

def method_missing(name, *args)
	if @plugins && @plugins[name]
		@plugins[name]
	elsif parent
		parent.__send__ name, *args
	else
		@plugins[name] = loadPlugin(name) || super
	end
end

Instance Attribute Details

#parentObject

Returns the value of attribute parent.



16
17
18
# File 'lib/rubko/base.rb', line 16

def parent
  @parent
end

Instance Method Details

#camelize(array) ⇒ Object



24
25
26
27
28
# File 'lib/rubko/base.rb', line 24

def camelize(array)
	array.map { |x|
		x.to_s.downcase.capitalize
	}.join
end

#finalizeObject



14
# File 'lib/rubko/base.rb', line 14

def finalize(*) end

#httpGet(url) ⇒ Object



114
115
116
117
# File 'lib/rubko/base.rb', line 114

def httpGet(url)
	require 'net/http'
	Net::HTTP.get_response(URI.parse url).body
end

#initObject



13
# File 'lib/rubko/base.rb', line 13

def init(*) end

#initialize(parent = nil) ⇒ Object



6
7
8
9
10
11
# File 'lib/rubko/base.rb', line 6

def initialize(parent = nil)
	@parent = parent
	finalizers << self if parent
	@plugins = {}
	puts "#{self.class} initialized." unless production?
end

#jsonParse(resource) ⇒ Object



119
120
121
# File 'lib/rubko/base.rb', line 119

def jsonParse(resource)
	JSON.parse resource, symbolize_names: true
end

#loadController(name, *args) ⇒ Object



65
66
67
68
69
# File 'lib/rubko/base.rb', line 65

def loadController(name, *args)
	require 'rubko/controller'
	return false unless loadFile "./controllers/#{name}.rb"
	load :controller, name, *args
end

#loadFile(name) ⇒ Object



18
19
20
21
22
# File 'lib/rubko/base.rb', line 18

def loadFile(name)
	require(name) || true
rescue LoadError
	false
end

#loadModel(name, *args) ⇒ Object



71
72
73
74
75
# File 'lib/rubko/base.rb', line 71

def loadModel(name, *args)
	require 'rubko/model'
	return false unless loadFile "./models/#{name}.rb"
	load :model, name, *args
end

#loadPlugin(name, *args) ⇒ Object



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
# File 'lib/rubko/base.rb', line 39

def loadPlugin(name, *args)
	require 'rubko/plugin'

	process = -> search, range, proc=nil {
		p = Dir[search].sort_by { |x|
			x.split('/').size
		}.first
		if p
			loadFile p
			p[range].split '/'
		else
			proc.call if proc
		end
	}

	path = uncamelize(name).reverse * '/'
	name = process.call "./plugins/**/#{path}.rb", 10..-4, -> {
		d = __dir__
		process.call "#{d}/plugins/**/#{path}.rb", (d.size+9)..-4
	}
	return false unless name

	loadFile "./config/#{name * '/'}.rb"
	load :plugin, name, *args
end

#loadView(*name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rubko/base.rb', line 77

def loadView(*name)
	if production?
		template = memory[:views, name]
	end
	unless template
		fileName = 'views/'+name.join('/')+'.erb'
		if File.exists? fileName
			require 'erb'
			template = ERB.new File.read(fileName)
		else
			fileName = 'views/'+name.join('/')+'.haml'
			return unless File.exists? fileName
			require 'haml'
			template = Haml::Engine.new File.read(fileName), ugly: true, remove_whitespace: true
		end
		memory[:views, name] = template if production?
		puts "Template #{name * '/'} initialized."
	end

	case template.class.to_s
	when 'ERB'
		template.result binding
	when 'Haml::Engine'
		template.render binding
	end
end

#uncamelize(str) ⇒ Object



29
30
31
# File 'lib/rubko/base.rb', line 29

def uncamelize(str)
	str.to_s.split(/(?=[A-Z])/).map(&:downcase)
end