Class: Dataset::SessionBinding

Inherits:
Object
  • Object
show all
Defined in:
lib/dataset/session_binding.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database_or_parent_binding) ⇒ SessionBinding

Returns a new instance of SessionBinding.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/dataset/session_binding.rb', line 177

def initialize(database_or_parent_binding)
  @id_cache = Hash.new {|h,k| h[k] = {}}
  @record_methods = new_record_methods_module
  @model_finders = new_model_finders_module
  @block_variables = Hash.new
  
  case database_or_parent_binding
  when Dataset::SessionBinding
    @parent_binding = database_or_parent_binding
    @database = parent_binding.database
    @model_finders.module_eval { include database_or_parent_binding.model_finders }
    @block_variables.update(database_or_parent_binding.block_variables)
  else 
    @database = database_or_parent_binding
  end
end

Instance Attribute Details

#block_variablesObject (readonly)

Returns the value of attribute block_variables.



175
176
177
# File 'lib/dataset/session_binding.rb', line 175

def block_variables
  @block_variables
end

#databaseObject (readonly)

Returns the value of attribute database.



173
174
175
# File 'lib/dataset/session_binding.rb', line 173

def database
  @database
end

#model_findersObject (readonly)

Returns the value of attribute model_finders.



174
175
176
# File 'lib/dataset/session_binding.rb', line 174

def model_finders
  @model_finders
end

#parent_bindingObject (readonly)

Returns the value of attribute parent_binding.



173
174
175
# File 'lib/dataset/session_binding.rb', line 173

def parent_binding
  @parent_binding
end

#record_methodsObject (readonly)

Returns the value of attribute record_methods.



174
175
176
# File 'lib/dataset/session_binding.rb', line 174

def record_methods
  @record_methods
end

Instance Method Details

#copy_block_variables(dataset_block) ⇒ Object



194
195
196
197
198
# File 'lib/dataset/session_binding.rb', line 194

def copy_block_variables(dataset_block)
  dataset_block.instance_variables.each do |name|
    self.block_variables[name] = dataset_block.instance_variable_get(name)
  end
end

#create_model(record_type, *args) ⇒ Object



200
201
202
# File 'lib/dataset/session_binding.rb', line 200

def create_model(record_type, *args)
  insert(Dataset::Record::Model, record_type, *args)
end

#create_record(record_type, *args) ⇒ Object



204
205
206
# File 'lib/dataset/session_binding.rb', line 204

def create_record(record_type, *args)
  insert(Dataset::Record::Fixture, record_type, *args)
end

#find_id(record_type_or_meta, symbolic_name) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
# File 'lib/dataset/session_binding.rb', line 208

def find_id(record_type_or_meta, symbolic_name)
  record_meta = record_meta_for_type(record_type_or_meta)
  heirarchy = record_meta.heirarchy
  if local_id = @id_cache[heirarchy.id_cache_key][symbolic_name]
    local_id
  elsif !parent_binding.nil?
    parent_binding.find_id record_meta, symbolic_name
  else
    raise RecordNotFound.new(heirarchy, symbolic_name)
  end
end

#find_model(record_type_or_meta, symbolic_name) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/dataset/session_binding.rb', line 220

def find_model(record_type_or_meta, symbolic_name)
  record_meta = record_meta_for_type(record_type_or_meta)
  heirarchy = record_meta.heirarchy
  if local_id = @id_cache[heirarchy.id_cache_key][symbolic_name]
    heirarchy.base_class.find local_id
  elsif !parent_binding.nil?
    parent_binding.find_model record_meta, symbolic_name
  else
    raise RecordNotFound.new(heirarchy, symbolic_name)
  end
end

#install_block_variables(target) ⇒ Object



232
233
234
235
236
# File 'lib/dataset/session_binding.rb', line 232

def install_block_variables(target)
  block_variables.each do |k,v|
    target.instance_variable_set(k,v)
  end
end

#name_model(record, symbolic_name) ⇒ Object



238
239
240
241
242
243
# File 'lib/dataset/session_binding.rb', line 238

def name_model(record, symbolic_name)
  record_meta = database.record_meta(record.class)
  @model_finders.create_finders(record_meta)
  @id_cache[record_meta.heirarchy.id_cache_key][symbolic_name] = record.id
  record
end

#name_to_sym(name) ⇒ Object



252
253
254
# File 'lib/dataset/session_binding.rb', line 252

def name_to_sym(name)
  name.to_s.underscore.gsub("'", "").gsub("\"", "").gsub(" ", "_").to_sym if name
end

#record_meta_for_type(record_type) ⇒ Object



245
246
247
248
249
250
# File 'lib/dataset/session_binding.rb', line 245

def record_meta_for_type(record_type)
  record_type.is_a?(Record::Meta) ? record_type : begin
    record_class = resolve_record_class(record_type)
    database.record_meta(record_class)
  end
end