# File lib/aerosol/aws_model.rb, line 40 def primary_key(attr = nil) @primary_key = attr unless attr.nil? @primary_key end
module Aerosol::AWSModel::ClassMethods
Public Instance Methods
all()
click to toggle source
# File lib/aerosol/aws_model.rb, line 65 def all raise 'Please define .request_all to use .all' unless respond_to?(:request_all) request_all.map { |struct| from_hash(struct.to_hash) } end
aws_attribute(*attrs)
click to toggle source
# File lib/aerosol/aws_model.rb, line 45 def aws_attribute(*attrs) dsl_attribute(*attrs) aws_attributes.merge(attrs) end
aws_attributes()
click to toggle source
# File lib/aerosol/aws_model.rb, line 87 def aws_attributes @aws_attributes ||= Set.new end
aws_class_attribute(name, klass, primary_key_proc = nil)
click to toggle source
# File lib/aerosol/aws_model.rb, line 50 def aws_class_attribute(name, klass, primary_key_proc = nil) unless klass.ancestors.include?(Aerosol::AWSModel) || (klass == self) raise '.aws_class_attribute requires a Aerosol::AWSModel that is not the current class.' end primary_key_proc ||= proc { |hash| hash[klass.primary_key] } dsl_class_attribute(name, klass) aws_class_attributes.merge!({ name => [klass, primary_key_proc] }) end
aws_class_attributes()
click to toggle source
# File lib/aerosol/aws_model.rb, line 91 def aws_class_attributes @aws_class_attributes ||= {} end
exists?(key)
click to toggle source
# File lib/aerosol/aws_model.rb, line 61 def exists?(key) all.map { |inst| inst.send(primary_key) }.include?(key) end
from_hash(hash)
click to toggle source
# File lib/aerosol/aws_model.rb, line 70 def from_hash(hash) raise 'To use .from_hash, you must specify a primary_key' if primary_key.nil? refs = Hash[aws_class_attributes.map do |name, (klass, primary_key_proc)| value = klass.instances.values.find do |inst| inst.send(klass.primary_key).to_s == primary_key_proc.call(hash).to_s unless inst.send(klass.primary_key).nil? end [name, value] end].reject { |k, v| v.nil? } instance = new! instance.from_aws = true aws_attributes.each { |attr| instance.send(attr, hash[attr]) unless hash[attr].nil? } refs.each { |name, inst| instance.send(name, inst.name) } instance end
primary_key(attr = nil)
click to toggle source