Module: Scorpio::ResourceBase::PickleAdapter

Includes:
Pickle::Adapter::Base
Defined in:
lib/scorpio/pickle_adapter.rb

Class Method Summary collapse

Class Method Details

.column_names(klass) ⇒ Object

get a list of column names for a given class



19
20
21
# File 'lib/scorpio/pickle_adapter.rb', line 19

def self.column_names(klass)
  klass.all_schema_properties.to_a
end

.create_model(klass, attributes) ⇒ Object

Create a model using attributes



50
51
52
# File 'lib/scorpio/pickle_adapter.rb', line 50

def self.create_model(klass, attributes)
  klass.new(attributes).post
end

.find_all_models(klass, conditions) ⇒ Object

Find all models matching conditions



41
42
43
44
45
46
47
# File 'lib/scorpio/pickle_adapter.rb', line 41

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



35
36
37
38
# File 'lib/scorpio/pickle_adapter.rb', line 35

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



24
25
26
27
28
29
30
31
32
# File 'lib/scorpio/pickle_adapter.rb', line 24

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_classesObject

Gets a list of the available models for this adapter

all of the Scorpio models MUST be loaded before this gets called.



14
15
16
# File 'lib/scorpio/pickle_adapter.rb', line 14

def self.model_classes
  ObjectSpace.each_object(Class).select { |klass| klass < ::Scorpio::ResourceBase }
end