Class: BSNS::Base
- Inherits:
-
Object
show all
- Extended by:
- BSNS
- Defined in:
- lib/bsns.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from BSNS
acts_as_collection, configure, get_config, has_many, has_one
Constructor Details
#initialize(data = {}) ⇒ Base
Returns a new instance of Base.
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/bsns.rb', line 55
def initialize data={}
get_defaults.each do |k,v|
data[k] ||= v
end
data.each do |k,v|
accessor = "#{k}="
if respond_to? accessor
self.send accessor, v
else
instance_variable_set "@#{k}", v
end
end
end
|
Class Method Details
.class ⇒ Object
98
99
100
|
# File 'lib/bsns.rb', line 98
def self.class
self
end
|
.content_path(p) ⇒ Object
111
112
113
|
# File 'lib/bsns.rb', line 111
def self.content_path p
set_v :relative_path, p
end
|
.get_v(k) ⇒ Object
86
87
88
89
90
91
92
|
# File 'lib/bsns.rb', line 86
def self.get_v k
begin
return class_variable_get "@@#{k}"
rescue
return nil
end
end
|
.load(file) ⇒ Object
124
125
126
127
|
# File 'lib/bsns.rb', line 124
def self.load file
data = load_data file
self.new data
end
|
.load_data(file) ⇒ Object
115
116
117
118
119
120
121
122
|
# File 'lib/bsns.rb', line 115
def self.load_data file
return load_from_collection file if get_v :single_file
data = {}
data['load_path'] = file
path = (root_path+relative_path.gsub(/\/$/, '')+'/')+file.to_s+'.yml'
throw "File not found: #{path}" unless File.exists? path
YAML.load_file path
end
|
.load_from_collection(f) ⇒ Object
137
138
139
140
141
142
143
|
# File 'lib/bsns.rb', line 137
def self.load_from_collection f
unless get_v :data
data = YAML.load_file (root_path + relative_path + '.yml')
set_v :data, data
end
get_v(:data)[f.to_s]
end
|
.load_or_nil(file) ⇒ Object
129
130
131
132
133
134
135
|
# File 'lib/bsns.rb', line 129
def self.load_or_nil file
begin
load_data file
rescue
return nil
end
end
|
.relative_path ⇒ Object
107
108
109
|
# File 'lib/bsns.rb', line 107
def self.relative_path
get_v(:relative_path) || self.class.name.to_s.downcase.pluralize
end
|
.root_path ⇒ Object
102
103
104
105
|
# File 'lib/bsns.rb', line 102
def self.root_path
path = BSNS.get_config(:content_path) || 'content'
path.gsub(/\/$/, '')+'/'
end
|
.set_v(k, v) ⇒ Object
94
95
96
|
# File 'lib/bsns.rb', line 94
def self.set_v k, v
class_variable_set "@@#{k}", v
end
|
Instance Method Details
#collection_from_array(arr, model, opts) ⇒ Object
Return an array of all the references for a thing.
146
147
148
|
# File 'lib/bsns.rb', line 146
def collection_from_array arr, model, opts
arr.map { |d| obj_from_dataset d, model, opts }
end
|
#define_linking_field(field, value) ⇒ Object
169
170
171
172
|
# File 'lib/bsns.rb', line 169
def define_linking_field field, value
self.class.module_eval { attr_accessor field.to_sym }
self.send "#{field}=", value
end
|
#get_defaults ⇒ Object
69
70
71
72
73
74
75
76
|
# File 'lib/bsns.rb', line 69
def get_defaults
data = get_v :defaults
unless data
data = self.class.load_or_nil "_defaults"
set_v :defaults, data
end
data || {}
end
|
#get_v(k) ⇒ Object
78
79
80
|
# File 'lib/bsns.rb', line 78
def get_v k
self.class.get_v k
end
|
#obj_from_dataset(d, model, opts = {}) ⇒ Object
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
# File 'lib/bsns.rb', line 150
def obj_from_dataset d, model, opts={}
= opts[:with]
if d.instance_of?(Hash) && !opts[:embedded]
id = d.keys[0]
value = d[id]
d = [id, value]
end
klass = obj = model.to_s.capitalize.constantize
if opts[:embedded]
obj = klass.new d
else
id = d.instance_of?(Array) ? d[0] : d
obj = klass.load id
end
obj.define_linking_field , d[1] if
obj
end
|
#set_v(k, v) ⇒ Object
82
83
84
|
# File 'lib/bsns.rb', line 82
def set_v k, v
self.class.set_v k, v
end
|