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.
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/bsns.rb', line 53
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
96
97
98
|
# File 'lib/bsns.rb', line 96
def self.class
self
end
|
.content_path(p) ⇒ Object
109
110
111
|
# File 'lib/bsns.rb', line 109
def self.content_path p
set_v :relative_path, p
end
|
.get_v(k) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/bsns.rb', line 84
def self.get_v k
begin
return class_variable_get "@@#{k}"
rescue
return nil
end
end
|
.load(file) ⇒ Object
122
123
124
125
|
# File 'lib/bsns.rb', line 122
def self.load file
data = load_data file
self.new data
end
|
.load_data(file) ⇒ Object
113
114
115
116
117
118
119
120
|
# File 'lib/bsns.rb', line 113
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
135
136
137
138
139
140
141
|
# File 'lib/bsns.rb', line 135
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
127
128
129
130
131
132
133
|
# File 'lib/bsns.rb', line 127
def self.load_or_nil file
begin
load_data file
rescue
return nil
end
end
|
.relative_path ⇒ Object
105
106
107
|
# File 'lib/bsns.rb', line 105
def self.relative_path
get_v(:relative_path) || self.class.name.to_s.downcase.pluralize
end
|
.root_path ⇒ Object
100
101
102
103
|
# File 'lib/bsns.rb', line 100
def self.root_path
path = BSNS.get_config(:content_path) || 'content'
path.gsub(/\/$/, '')+'/'
end
|
.set_v(k, v) ⇒ Object
92
93
94
|
# File 'lib/bsns.rb', line 92
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.
144
145
146
|
# File 'lib/bsns.rb', line 144
def collection_from_array arr, model, opts
arr.map { |d| obj_from_dataset d, model, opts }
end
|
#define_linking_field(field, value) ⇒ Object
167
168
169
170
|
# File 'lib/bsns.rb', line 167
def define_linking_field field, value
self.class.module_eval { attr_accessor field.to_sym }
self.send "#{field}=", value
end
|
#get_defaults ⇒ Object
67
68
69
70
71
72
73
74
|
# File 'lib/bsns.rb', line 67
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
76
77
78
|
# File 'lib/bsns.rb', line 76
def get_v k
self.class.get_v k
end
|
#obj_from_dataset(d, model, opts = {}) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
# File 'lib/bsns.rb', line 148
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
80
81
82
|
# File 'lib/bsns.rb', line 80
def set_v k, v
self.class.set_v k, v
end
|