Sha256: 12cfcfe8804d266d334e325d97321262ba18f567dfbe53d09bebcf261bd69aa0

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

module EY
  class CloudClient
    class ApiStruct < Struct
      def self.new(*args, &block)
        args = [:api] | args
        super(*args) do |*block_args|
          block.call(*block_args) if block

          def self.from_array(api, array, common_values = {})
            if array
              array.map do |values|
                from_hash(api, values.merge(common_values))
              end
            end
          end

          def self.from_hash(api, attrs_or_struct)
            return nil unless attrs_or_struct

            if attrs_or_struct.respond_to?(:attributes=)
              # already a model
              obj = attrs_or_struct
            elsif obj = api.registry.find(self, attrs_or_struct['id'])
              obj.attributes = attrs_or_struct
            else
              obj = new(api, attrs_or_struct)
              api.registry.set(self, obj.id, obj)
            end
            obj
          end
        end
      end

      def initialize(api, attrs)
        self.api = api
        self.attributes = attrs
      end

      def attributes=(attrs)
        attrs.each do |key, val|
          setter = :"#{key}="
          if respond_to?(setter)
            send(setter, val)
          end
        end
      end

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
engineyard-cloud-client-1.0.13 lib/engineyard-cloud-client/models/api_struct.rb
engineyard-cloud-client-1.0.12 lib/engineyard-cloud-client/models/api_struct.rb
engineyard-cloud-client-1.0.11 lib/engineyard-cloud-client/models/api_struct.rb
engineyard-cloud-client-1.0.10 lib/engineyard-cloud-client/models/api_struct.rb
engineyard-cloud-client-1.0.9 lib/engineyard-cloud-client/models/api_struct.rb