Module: Pickle::Session
- Defined in:
- lib/pickle/session.rb,
lib/pickle/session/parser.rb
Defined Under Namespace
Modules: Parser
Class Method Summary collapse
Instance Method Summary collapse
- #create_model(a_model_name, fields = nil) ⇒ Object
-
#created_model(name) ⇒ Object
return the original model stored by create_model or find_model.
-
#created_model!(name) ⇒ Object
like created_model, but raise an error if it can’t be found.
-
#created_model?(name) ⇒ Boolean
predicate version which raises no errors.
-
#created_models(factory) ⇒ Object
return all original models of specified type.
- #find_model(a_model_name, fields = nil) ⇒ Object
- #find_models(factory, fields = nil) ⇒ Object
-
#model(name) ⇒ Object
return a newly selected model.
-
#model!(name) ⇒ Object
like model, but raise an error if it can’t be found.
-
#model?(name) ⇒ Boolean
predicate version which raises no errors.
-
#models(factory) ⇒ Object
return all models of specified type (freshly selected from the database).
- #respond_to_with_pickle_parser?(method, include_private = false) ⇒ Boolean
Class Method Details
.extended(world_object) ⇒ Object
8 9 10 |
# File 'lib/pickle/session.rb', line 8 def extended(world_object) proxy_to_pickle_parser(class << world_object; self; end) # metaclass is not 2.1 compatible end |
.included(world_class) ⇒ Object
4 5 6 |
# File 'lib/pickle/session.rb', line 4 def included(world_class) proxy_to_pickle_parser(world_class) end |
Instance Method Details
#create_model(a_model_name, fields = nil) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/pickle/session.rb', line 23 def create_model(a_model_name, fields = nil) factory, label = *parse_model(a_model_name) raise ArgumentError, "Can't create with an ordinal (e.g. 1st user)" if label.is_a?(Integer) record = pickle_config.factories[factory].create(parse_fields(fields)) store_model(factory, label, record) end |
#created_model(name) ⇒ Object
return the original model stored by create_model or find_model
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/pickle/session.rb', line 47 def created_model(name) factory, name_or_index = *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) ⇒ Object
like created_model, but raise an error if it can’t be found
80 81 82 |
# File 'lib/pickle/session.rb', line 80 def created_model!(name) created_model(name) or raise "Can't find pickle model: '#{name}' in this scenario" end |
#created_model?(name) ⇒ Boolean
predicate version which raises no errors
60 61 62 |
# File 'lib/pickle/session.rb', line 60 def created_model?(name) (created_model(name) rescue nil) ? true : false end |
#created_models(factory) ⇒ Object
return all original models of specified type
85 86 87 |
# File 'lib/pickle/session.rb', line 85 def created_models(factory) models_by_index(factory) end |
#find_model(a_model_name, fields = nil) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/pickle/session.rb', line 30 def find_model(a_model_name, fields = nil) factory, name = *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 = pickle_config.factories[factory].klass if record = model_class.find(:first, :conditions => convert_models_to_attributes(model_class, parse_fields(fields))) store_model(factory, name, record) end end |
#find_models(factory, fields = nil) ⇒ Object
39 40 41 42 43 44 |
# File 'lib/pickle/session.rb', line 39 def find_models(factory, fields = nil) models_by_index(factory).clear model_class = pickle_config.factories[factory].klass records = model_class.find(:all, :conditions => convert_models_to_attributes(model_class, parse_fields(fields))) records.each {|record| store_model(factory, nil, record)} end |
#model(name) ⇒ Object
return a newly selected model
65 66 67 |
# File 'lib/pickle/session.rb', line 65 def model(name) (model = created_model(name)) && model.class.find(model.id) end |
#model!(name) ⇒ Object
like model, but raise an error if it can’t be found
75 76 77 |
# File 'lib/pickle/session.rb', line 75 def model!(name) model(name) or raise "Can't find pickle model: '#{name}' in this scenario" end |
#model?(name) ⇒ Boolean
predicate version which raises no errors
70 71 72 |
# File 'lib/pickle/session.rb', line 70 def model?(name) (model(name) rescue nil) ? true : false end |
#models(factory) ⇒ Object
return all models of specified type (freshly selected from the database)
90 91 92 |
# File 'lib/pickle/session.rb', line 90 def models(factory) created_models(factory).map{|model| model.class.find(model.id) } end |
#respond_to_with_pickle_parser?(method, include_private = false) ⇒ Boolean
94 95 96 |
# File 'lib/pickle/session.rb', line 94 def respond_to_with_pickle_parser?(method, include_private = false) respond_to_without_pickle_parser?(method, include_private) || pickle_parser.respond_to?(method, include_private) end |