Class: Yammy::Base

Inherits:
Object
  • Object
show all
Extended by:
Yammy
Defined in:
lib/yammy.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Yammy

has_many

Constructor Details

#initialize(data = {}) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yammy.rb', line 23

def initialize data={}
	set_defaults
	data.each do |k,v|
		accessor = "#{k}="
		if respond_to? accessor
			self.send accessor, v
		else
			instance_variable_set "@#{k}", v
		end
	end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



21
22
23
# File 'lib/yammy.rb', line 21

def name
  @name
end

Class Method Details

.classObject



35
36
37
# File 'lib/yammy.rb', line 35

def self.class
	self
end

.find(path) ⇒ Object



47
48
49
# File 'lib/yammy.rb', line 47

def self.find path
	base_path
end

.load(file) ⇒ Object



58
59
60
61
62
# File 'lib/yammy.rb', line 58

def self.load file
	data = YAML.load_file root_path.join relative_path, file.to_s+'.yml'
	data['name'] = file
	self.new data
end

.relative_pathObject



43
44
45
# File 'lib/yammy.rb', line 43

def self.relative_path
	self.class.name.to_s.downcase.pluralize
end

.relative_path=(p) ⇒ Object



51
52
53
# File 'lib/yammy.rb', line 51

def self.relative_path= p
	@@relative_path = p.to_s
end

.root_pathObject



39
40
41
# File 'lib/yammy.rb', line 39

def self.root_path
	Rails.root.join Rails.application.config.yammy_content_dir
end

Instance Method Details

#collection_from_array(arr, model, extra_key = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/yammy.rb', line 64

def collection_from_array arr, model, extra_key = nil
	# Return an array of all the references for a thing.
	arr.map do |d| 
		id  = d.instance_of?(Array) ? d[0] : d
		obj = model.to_s.capitalize.constantize.load id
		obj.send("name=", id)
		obj.send(extra_key.to_s+"=", d[1]) if extra_key
		obj
	end
end

#set_defaultsObject



55
56
# File 'lib/yammy.rb', line 55

def set_defaults
end