Module: Ey::Core::Associations
- Included in:
- Client::Account, Client::AccountReferral, Client::AccountTrial, Client::Addon, Client::Address, Client::Alert, Client::Application, Client::ApplicationArchive, Client::ApplicationDeployment, Client::AutoScalingAlarm, Client::AutoScalingGroup, Client::BackupFile, Client::BaseAutoScalingPolicy, Client::Blueprint, Client::Component, Client::Contact, Client::ContainerServiceDeployment, Client::Cost, Client::DatabasePlanUsage, Client::DatabaseServer, Client::DatabaseServerRevision, Client::DatabaseServerSnapshot, Client::DatabaseServerUsage, Client::DatabaseService, Client::Deployment, Client::Environment, Client::EnvironmentPlanUsage, Client::EnvironmentVariable, Client::Feature, Client::Firewall, Client::FirewallRule, Client::Keypair, Client::KeypairDeployment, Client::LegacyAlert, Client::LoadBalancer, Client::LoadBalancerNode, Client::LoadBalancerService, Client::Log, Client::LogicalDatabase, Client::Membership, Client::Message, Client::PlanUsage, Client::Provider, Client::ProviderLocation, Client::Request, Client::Server, Client::ServerEvent, Client::ServerUsage, Client::SslCertificate, Client::Storage, Client::StorageUser, Client::SupportTrial, Client::Task, Client::UntrackedAddress, Client::UntrackedServer, Client::User, Client::Volume
- Defined in:
- lib/ey-core/associations.rb
Instance Method Summary collapse
- #assoc_accessor(name, options = {}) ⇒ Object (also: #has_one)
- #assoc_coverage(options) ⇒ Object
- #assoc_reader(name, options = {}) ⇒ Object
- #assoc_writer(name, options = {}) ⇒ Object
- #associations ⇒ Object
- #collection_reader(name, options = {}) ⇒ Object (also: #has_many)
Instance Method Details
#assoc_accessor(name, options = {}) ⇒ Object Also known as: has_one
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ey-core/associations.rb', line 100 def assoc_accessor(name, ={}) assoc_coverage() if self.associations[name] raise ArgumentError, "#{self.name} association[#{name}] specified more than once" else self.associations[name] = end aliases = Array([:aliases] || name.to_s) attribute(:"#{name}_url", aliases: aliases, parser: lambda { |v,_| v.is_a?(String) && v }) assoc_reader(name, ) assoc_writer(name, ) end |
#assoc_coverage(options) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 |
# File 'lib/ey-core/associations.rb', line 2 def assoc_coverage() if defined? Cistern::Coverage call = Cistern::Coverage.find_caller_before("ey-core/associations.rb") if call and call.label.start_with?("<class:") [:coverage_file] = call.absolute_path [:coverage_line] = call.lineno [:coverage_hits] = 0 end end end |
#assoc_reader(name, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/ey-core/associations.rb', line 18 def assoc_reader(name, ={}) assoc_key = [:key] || "#{name}_id" collection = [:collection] || "#{name}s" resource = [:resource] || name assoc_name = [:assoc_name] || resource # handle polymorphic assocations # # really only used for connector sources, as they may be cluster_components or addons disambiguate = Proc.new do |object, url, index=0| if object.is_a?(Array) && index > (object.size - 1) raise "No matching object found in #{object}" elsif object.is_a?(Array) && url url_pieces = URI.parse(url).path.split("/").map { |piece| piece.gsub(/-|_/,'') } # "/cluster-components/x" => ["clustercomponents", "x"] object.find do |model| normalized_model = model.to_s.gsub(/-|_/, '') # [:cluster_components, :addons] => ["clustercomponents", "addons"] url_pieces.find { |piece| piece.include?(normalized_model) } end elsif object.is_a?(Array) object[index] else object end end define_method(name) do url = self.send("#{name}_url") id = send(assoc_key) if id || url begin index ||= 0 this_resource = disambiguate[resource, url, index] this_assoc_name = disambiguate[assoc_name, url, index] this_collection = disambiguate[collection, url, index] fetched = self.connection.send("get_#{this_assoc_name}", {"id" => id, "url" => url}).body rescue RuntimeError => e if e..match(/url needed/i) index += 1 retry if index < 3 else raise e end rescue Ey::Core::Response::NotFound index += 1 index < 3 ? retry : raise end unless fetched[this_resource.to_s] raise "Expected a '#{this_resource.to_s}' in '#{fetched.inspect}'" end attributes = fetched[this_resource.to_s] if self.connection.respond_to?(this_collection) attributes = attributes.merge(collection: self.connection.send(this_collection)) end self.connection.send(this_assoc_name, attributes) end end end |
#assoc_writer(name, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ey-core/associations.rb', line 83 def assoc_writer(name, ={}) assoc_key = [:key] || "#{name}_id" url_key = [:key] || "#{name}_url" attr_accessor assoc_key define_method("#{name}=") do |assoc| if assoc.respond_to?(:identity) self.send("#{assoc_key}=", assoc.identity) elsif assoc.is_a?(String) && assoc.match(URI::regexp) self.send("#{url_key}=", assoc) else self.send("#{assoc_key}=", assoc) end end end |
#associations ⇒ Object
14 15 16 |
# File 'lib/ey-core/associations.rb', line 14 def associations @associations ||= {} end |
#collection_reader(name, options = {}) ⇒ Object Also known as: has_many
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ey-core/associations.rb', line 119 def collection_reader(name, ={}) assoc_coverage() if self.associations[name] raise ArgumentError, "#{self.name} association[#{name}] specified more than once" else self.associations[name] = end attribute(:"#{name}_url", aliases: [:aliases] || [name.to_s]) assoc_key = [:key] || name collection_key = [:model] || assoc_key define_method(name) do value = self.send("#{name}_url") if self.identity && !value raise "Cannot load #{name} association: #{name}_url is not set" end # @fixme this allows collections to be stored in a url variable on create before being replaced # by a url entry after create upon {#merge_attributes} if value.nil? [] elsif value.is_a?(String) && value.match(URI::regexp) self.connection.send(collection_key, {"url" => value}) elsif value.is_a?(Hash) [value] else Array(value) end end end |