Module: Aerosol::AWSModel::ClassMethods
- Defined in:
- lib/aerosol/aws_model.rb
Instance Method Summary collapse
- #all ⇒ Object
- #aws_attribute(*attrs) ⇒ Object
- #aws_attributes ⇒ Object
- #aws_class_attribute(name, klass, primary_key_proc = nil) ⇒ Object
- #aws_class_attributes ⇒ Object
- #exists?(key) ⇒ Boolean
- #from_hash(hash) ⇒ Object
- #primary_key(attr = nil) ⇒ Object
Instance Method Details
#all ⇒ Object
65 66 67 68 |
# 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) ⇒ 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_attributes ⇒ Object
87 88 89 |
# 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) ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# 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 ⇒ Object
91 92 93 |
# File 'lib/aerosol/aws_model.rb', line 91 def aws_class_attributes @aws_class_attributes ||= {} end |
#exists?(key) ⇒ Boolean
61 62 63 |
# File 'lib/aerosol/aws_model.rb', line 61 def exists?(key) all.map { |inst| inst.send(primary_key) }.include?(key) end |
#from_hash(hash) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# 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) ⇒ 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 |