Sha256: f6f4c2db711fdbd13cf62096d8d5792576c8a9d29c6375b3831ce7e5b0562dd4

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 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
        self.values.select { |attribute| attribute.type == :link }
      end
      
      def simple_attributes
        self.values.select { |attribute| attribute.type == :simple_attribute }        
      end
      
      def collections
        self.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

1 entries across 1 versions & 1 rubygems

Version Path
purzelrakete-restful-0.2.6 lib/restful/apimodel/resource.rb