Sha256: 6b3b36029628e6efbc5e7fb2f409d0b79493ab45eb3fb5133284a23af3fa13a4

Contents?: true

Size: 1.17 KB

Versions: 5

Compression:

Stored size: 1.17 KB

Contents

#
#  Resource model. Something like a DOM model for the api. 
#
module Restful
  module ApiModel
    class Resource
      attr_accessor :base, :path, :url, :values, :name, :type
      
      def initialize(name, url)
        self.url = url[:url]
        self.path = url[:path]
        self.base = url[:base]                
        self.name = name
        self.type = :resource
        self.values = []
      end
      
      def links
        values.select { |attribute| attribute.type == :link }
      end
      
      def simple_attributes
        values.select { |attribute| attribute.type == :simple_attribute }        
      end
      
      def collections
        values.select { |attribute| attribute.type == :collection }
      end
      
      # invoke serialization
      def serialize(type)
        serializer = Restful::Serializers::Base.serializer(type)
        serializer.serialize(self)
      end
      
      # invoke deserialization
      def deserialize_from(type)
        serializer = Restful::Serializers::Base.serializer(type)
        serializer.deserialize(self)
      end
      
      def full_url
        base.blank? ? url : "#{ base }#{ path }"
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
purzelrakete-restful-0.2.1 lib/restful/apimodel/resource.rb
purzelrakete-restful-0.2.2 lib/restful/apimodel/resource.rb
purzelrakete-restful-0.2.3 lib/restful/apimodel/resource.rb
purzelrakete-restful-0.2.4 lib/restful/apimodel/resource.rb
purzelrakete-restful-0.2.5 lib/restful/apimodel/resource.rb