Class: Pickle::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/pickle/session.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Session

Returns a new instance of Session.



3
4
5
6
# File 'lib/pickle/session.rb', line 3

def initialize(options = {})
  self.parser = options[:parser] || Pickle.parser
  @config = parser.config
end

Instance Method Details

#create_model(a_model_name, fields = nil) ⇒ Object

Raises:

  • (ArgumentError)


8
9
10
11
12
13
# File 'lib/pickle/session.rb', line 8

def create_model(a_model_name, fields = nil)
  factory, label = *parser.parse_model(a_model_name)
  raise ArgumentError, "Can't create with an ordinal (e.g. 1st user)" if label.is_a?(Integer)
  record = config.factories[factory].create(parser.parse_fields(fields))
  store_model(factory, label, record)
end

#created_model(name) ⇒ Object

return the original model stored by create_model or find_model



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pickle/session.rb', line 32

def created_model(name)
  factory, name_or_index = *parser.parse_model(name)
  
  if name_or_index.blank?
    models_by_index(factory).last
  elsif name_or_index.is_a?(Integer)
    models_by_index(factory)[name_or_index]
  else
    models_by_name(factory)[name_or_index] or raise "model: #{name} does not refer to known model in this scenario"
  end
end

#created_model?(name) ⇒ Boolean

predicate version which raises no errors

Returns:

  • (Boolean)


45
46
47
# File 'lib/pickle/session.rb', line 45

def created_model?(name)
  (created_model(name) rescue nil) ? true : false
end

#created_models(factory) ⇒ Object

return all original models of specified type



60
61
62
# File 'lib/pickle/session.rb', line 60

def created_models(factory)
  models_by_index(factory)
end

#find_model(a_model_name, fields = nil) ⇒ Object

Raises:

  • (ArgumentError)


15
16
17
18
19
20
21
22
# File 'lib/pickle/session.rb', line 15

def find_model(a_model_name, fields = nil)
  factory, name = *parser.parse_model(a_model_name)
  raise ArgumentError, "Can't find a model with an ordinal (e.g. 1st user)" if name.is_a?(Integer)
  model_class = config.factories[factory].klass
  if record = model_class.find(:first, :conditions => convert_models_to_attributes(model_class, parser.parse_fields(fields)))
    store_model(factory, name, record)
  end
end

#find_models(factory, fields = nil) ⇒ Object



24
25
26
27
28
29
# File 'lib/pickle/session.rb', line 24

def find_models(factory, fields = nil)
  models_by_index(factory).clear
  model_class = config.factories[factory].klass
  records = model_class.find(:all, :conditions => convert_models_to_attributes(model_class, parser.parse_fields(fields)))
  records.each {|record| store_model(factory, nil, record)}
end

#model(name) ⇒ Object

return a newly selected model



50
51
52
# File 'lib/pickle/session.rb', line 50

def model(name)
  (model = created_model(name)) && model.class.find(model.id)
end

#model?(name) ⇒ Boolean

predicate version which raises no errors

Returns:

  • (Boolean)


55
56
57
# File 'lib/pickle/session.rb', line 55

def model?(name)
  (model(name) rescue nil) ? true : false
end

#models(factory) ⇒ Object

return all models of specified type (freshly selected from the database)



65
66
67
# File 'lib/pickle/session.rb', line 65

def models(factory)
  created_models(factory).map{|model| model.class.find(model.id) }
end