Class: Chisel::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/chisel/resource.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject



10
11
12
# File 'lib/chisel/resource.rb', line 10

def self.all
	raise "You must implement #{self.class.name}.all"
end

.class_cache(*keys) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chisel/resource.rb', line 43

def self.class_cache(*keys)
	cached = true
	keys.each { |key| cached &= @@cache.key?(key) }
	if cached
		if keys.size == 1
			return @@cache[keys[0]]
		else
			return keys.map { |key| @@cache[key] }
		end
	end
	if keys.size == 1
		results = yield
		@@cache[keys[0]] = results
	else
		results = [*yield]
		keys.each_index { |i| @@cache[keys[i]] = results[i] }
	end
	results
end

.find(parameters = {}) ⇒ Object



84
85
86
# File 'lib/chisel/resource.rb', line 84

def self.find(parameters={})
	self.find_in(self.all, parameters)
end

.find_in(resources, parameters = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chisel/resource.rb', line 63

def self.find_in(resources, parameters={})
	return resources if parameters == {}
	results = []
	
	resources.each do |resource|
		matches = true
		parameters.each do |param, value|
			param = param.to_sym
			if resource.respond_to?(param)
				param_value = resource.public_send(param)
				if param_value != value
					matches = false
					break
				end
			end
		end
		results << resource if matches
	end
	results
end

Instance Method Details

#cache(*keys) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chisel/resource.rb', line 22

def cache(*keys)
	@cache ||= {}
	cached = true
	keys.each { |key| cached &= @cache.key?(key) }
	if cached
		if keys.size == 1
			return @cache[keys[0]]
		else
			return keys.map { |key| @cache[key] }
		end
	end
	if keys.size == 1
		results = yield
		@cache[keys[0]] = results
	else
		results = [*yield]
		keys.each_index { |i| @cache[keys[i]] = results[i] }
	end
	results
end

#idObject



6
7
8
# File 'lib/chisel/resource.rb', line 6

def id
	raise "You must implement #{self.class.name}.id"
end

#inspectObject



18
19
20
# File 'lib/chisel/resource.rb', line 18

def inspect
	File.join(*self.id)
end

#resource_typeObject



14
15
16
# File 'lib/chisel/resource.rb', line 14

def resource_type
	self.class.name.underscore
end