Module: Pickle::Session
- Defined in:
- lib/pickle/session.rb,
lib/pickle/session/parser.rb
Defined Under Namespace
Modules: Parser Classes: ModelNotFoundError, ModelNotKnownError
Class Method Summary collapse
Instance Method Summary collapse
- #build_model(pickle_ref, fields = nil) ⇒ Object
- #build_models(count, pickle_ref, fields = nil) ⇒ Object
- #create_model(pickle_ref, fields = nil) ⇒ Object
- #create_models(count, pickle_ref, fields = nil) ⇒ Object
-
#create_models_from_table(plural_factory, table) ⇒ Object
if a column exists in the table which matches the singular factory name, this is used as the pickle ref.
-
#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_model!(name, fields = nil) ⇒ Object
- #find_models(factory, fields = nil) ⇒ Object
-
#find_models_from_table(plural_factory, table) ⇒ Object
if a column exists in the table which matches the singular factory name, this is used as the pickle ref.
-
#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
24 25 26 |
# File 'lib/pickle/session.rb', line 24 def extended(world_object) proxy_to_pickle_parser(class << world_object; self; end) # metaclass is not 2.1 compatible end |
.included(world_class) ⇒ Object
20 21 22 |
# File 'lib/pickle/session.rb', line 20 def included(world_class) proxy_to_pickle_parser(world_class) end |
Instance Method Details
#build_model(pickle_ref, fields = nil) ⇒ Object
47 48 49 |
# File 'lib/pickle/session.rb', line 47 def build_model(pickle_ref, fields = nil) create_or_build_model(:build, 1, pickle_ref, fields) end |
#build_models(count, pickle_ref, fields = nil) ⇒ Object
51 52 53 |
# File 'lib/pickle/session.rb', line 51 def build_models(count, pickle_ref, fields = nil) create_or_build_model(:build, count, pickle_ref, fields) end |
#create_model(pickle_ref, fields = nil) ⇒ Object
39 40 41 |
# File 'lib/pickle/session.rb', line 39 def create_model(pickle_ref, fields = nil) create_or_build_model(:create, 1, pickle_ref, fields) end |
#create_models(count, pickle_ref, fields = nil) ⇒ Object
43 44 45 |
# File 'lib/pickle/session.rb', line 43 def create_models(count, pickle_ref, fields = nil) create_or_build_model(:create, count, pickle_ref, fields) end |
#create_models_from_table(plural_factory, table) ⇒ Object
if a column exists in the table which matches the singular factory name, this is used as the pickle ref
56 57 58 59 60 61 62 |
# File 'lib/pickle/session.rb', line 56 def create_models_from_table(plural_factory, table) factory = plural_factory.singularize table.hashes.map do |hash| pickle_ref = factory + (hash[factory] ? " \"#{hash.delete(factory)}\"" : "") create_model(pickle_ref, hash) end end |
#created_model(name) ⇒ Object
return the original model stored by create_model or find_model
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/pickle/session.rb', line 105 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 ModelNotKnownError, name end end |
#created_model!(name) ⇒ Object
like created_model, but raise an error if it can’t be found
140 141 142 |
# File 'lib/pickle/session.rb', line 140 def created_model!(name) created_model(name) or raise ModelNotKnownError, name end |
#created_model?(name) ⇒ Boolean
predicate version which raises no errors
118 119 120 |
# File 'lib/pickle/session.rb', line 118 def created_model?(name) (created_model(name) rescue nil) ? true : false end |
#created_models(factory) ⇒ Object
return all original models of specified type
145 146 147 |
# File 'lib/pickle/session.rb', line 145 def created_models(factory) models_by_index(factory) end |
#find_model(a_model_name, fields = nil) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/pickle/session.rb', line 64 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 fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields) conditions = convert_models_to_attributes(model_class, fields) record = Pickle::Adapter.find_first_model(model_class, conditions) store_model(factory, name, record) if record record end |
#find_model!(name, fields = nil) ⇒ Object
79 80 81 |
# File 'lib/pickle/session.rb', line 79 def find_model!(name, fields = nil) find_model(name, fields) or raise ModelNotFoundError, "Can't find #{name}#{" with #{fields}" if fields.present?} from the orm in this scenario" end |
#find_models(factory, fields = nil) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/pickle/session.rb', line 83 def find_models(factory, fields = nil) factory = pickle_parser.canonical(factory) models_by_index(factory).clear model_class = pickle_config.factories[factory].klass conditions = convert_models_to_attributes(model_class, parse_fields(fields)) records = Pickle::Adapter.find_all_models(model_class, conditions) records.each {|record| store_model(factory, nil, record)} end |
#find_models_from_table(plural_factory, table) ⇒ Object
if a column exists in the table which matches the singular factory name, this is used as the pickle ref
96 97 98 99 100 101 102 |
# File 'lib/pickle/session.rb', line 96 def find_models_from_table(plural_factory, table) factory = plural_factory.singularize table.hashes.map do |hash| pickle_ref = factory + (hash[factory] ? " \"#{hash.delete(factory)}\"" : "") find_model(pickle_ref, hash) end end |
#model(name) ⇒ Object
return a newly selected model
123 124 125 126 127 |
# File 'lib/pickle/session.rb', line 123 def model(name) model = created_model(name) return nil unless model Pickle::Adapter.get_model(model.class, model.id) end |
#model!(name) ⇒ Object
like model, but raise an error if it can’t be found
135 136 137 |
# File 'lib/pickle/session.rb', line 135 def model!(name) model(name) or raise ModelNotKnownError, name end |
#model?(name) ⇒ Boolean
predicate version which raises no errors
130 131 132 |
# File 'lib/pickle/session.rb', line 130 def model?(name) (model(name) rescue nil) ? true : false end |
#models(factory) ⇒ Object
return all models of specified type (freshly selected from the database)
150 151 152 153 154 |
# File 'lib/pickle/session.rb', line 150 def models(factory) created_models(factory).map do |model| Pickle::Adapter.get_model(model.class, model.id) end end |
#respond_to_with_pickle_parser?(method, include_private = false) ⇒ Boolean
156 157 158 |
# File 'lib/pickle/session.rb', line 156 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 |