Module: Aerosol::AWSModel::ClassMethods

Defined in:
lib/aerosol/aws_model.rb

Instance Method Summary collapse

Instance Method Details

#allObject



63
64
65
66
# File 'lib/aerosol/aws_model.rb', line 63

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) ⇒ Object



45
46
47
48
# File 'lib/aerosol/aws_model.rb', line 45

def aws_attribute(*attrs)
  dsl_attribute(*attrs)
  aws_attributes.merge(attrs)
end

#aws_attributesObject



85
86
87
# File 'lib/aerosol/aws_model.rb', line 85

def aws_attributes
  @aws_attributes ||= Set.new
end

#aws_class_attribute(name, klass) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/aerosol/aws_model.rb', line 50

def aws_class_attribute(name, klass)
  unless klass.ancestors.include?(Aerosol::AWSModel) || (klass == self)
    raise '.aws_class_attribute requires a Aerosol::AWSModel that is not the current class.'
  end

  dsl_class_attribute(name, klass)
  aws_class_attributes.merge!({ name => klass })
end

#aws_class_attributesObject



89
90
91
# File 'lib/aerosol/aws_model.rb', line 89

def aws_class_attributes
  @aws_class_attributes ||= {}
end

#exists?(key) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/aerosol/aws_model.rb', line 59

def exists?(key)
  all.map { |inst| inst.send(primary_key) }.include?(key)
end

#from_hash(hash) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/aerosol/aws_model.rb', line 68

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|
    value = klass.instances.values.find do |inst|
      inst.send(klass.primary_key).to_s == hash[klass.primary_key].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) ⇒ Object



40
41
42
43
# File 'lib/aerosol/aws_model.rb', line 40

def primary_key(attr = nil)
  @primary_key = attr unless attr.nil?
  @primary_key
end