Module: Scorpio::ResourceBase::PickleAdapter
- Includes:
- Pickle::Adapter::Base
- Defined in:
- lib/scorpio/pickle_adapter.rb
Class Method Summary collapse
-
.column_names(klass) ⇒ Object
get a list of column names for a given class.
-
.create_model(klass, attributes) ⇒ Object
Create a model using attributes.
-
.find_all_models(klass, conditions) ⇒ Object
Find all models matching conditions.
-
.find_first_model(klass, conditions) ⇒ Object
Find the first instance matching conditions.
-
.get_model(klass, id) ⇒ Object
Get an instance by id of the model.
-
.model_classes ⇒ Object
Gets a list of the available models for this adapter.
Class Method Details
.column_names(klass) ⇒ Object
get a list of column names for a given class
17 18 19 |
# File 'lib/scorpio/pickle_adapter.rb', line 17 def self.column_names(klass) klass.all_schema_properties.to_a end |
.create_model(klass, attributes) ⇒ Object
Create a model using attributes
48 49 50 |
# File 'lib/scorpio/pickle_adapter.rb', line 48 def self.create_model(klass, attributes) klass.new(attributes).post end |
.find_all_models(klass, conditions) ⇒ Object
Find all models matching conditions
39 40 41 42 43 44 45 |
# File 'lib/scorpio/pickle_adapter.rb', line 39 def self.find_all_models(klass, conditions) klass.index.select do |record| (conditions || {}).all? do |attr, value| record.public_send(attr) == value end end end |
.find_first_model(klass, conditions) ⇒ Object
Find the first instance matching conditions
33 34 35 36 |
# File 'lib/scorpio/pickle_adapter.rb', line 33 def self.find_first_model(klass, conditions) # TODO don't load all find_all_models(klass, conditions).first end |
.get_model(klass, id) ⇒ Object
Get an instance by id of the model
22 23 24 25 26 27 28 29 30 |
# File 'lib/scorpio/pickle_adapter.rb', line 22 def self.get_model(klass, id) if klass.respond_to?(:read) klass.read(id: id) elsif klass.respond_to?(:index) return klass.index.detect { |record| record.id == id } else raise end end |
.model_classes ⇒ Object
Gets a list of the available models for this adapter
all of the Scorpio models MUST be loaded before this gets called.
12 13 14 |
# File 'lib/scorpio/pickle_adapter.rb', line 12 def self.model_classes ObjectSpace.each_object(Class).select { |klass| klass < ::Scorpio::ResourceBase } end |