Sha256: 1345d459fe9d326c8333c6d410a881d09160e24f166e20fdee27debeb9a71665

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

module StorageRoom
  class Entry < Model
    class_attribute :collection
    
    before_initialize_from_response_data :load_associated_collections
    
    class << self         
      def index_path # :nodoc:
        "#{collection[:@url]}/entries"
      end
            
      def show_path(entry_id) # :nodoc:
        "#{index_path}/#{entry_id}"
      end
            
      def json_name # :nodoc:
        'entry'
      end
      
      def search_path(parameters = {}) # :nodoc:
        parameters.present? ? "#{index_path}?#{parameters.to_query}" : index_path
      end
      
      # Search for objects with specific parameters
      def search(parameters = {})
        Array.load(search_path(parameters))
      end
      
      # Find multiple Entries by their IDs with one query
      def find_by_ids(*ids)
        find_by_urls(*ids.map{|id| show_path(id)})
      end
      
      # Find multiple Entries by their URLs with one query
      def find_by_urls(*urls)
        search(:@url.in => urls)
      end
    end
    
        
    # The collection of a entry
    def collection
      self.class.collection
    end    
    
    # Has this Entry been trashed?
    def trashed?
      self[:@trash]
    end
    
    def id
      self[:@url] ? self[:@url].split('/').last : nil
    end
    
    protected
      # Fetch all associated Collections before the Entry is initialized from the JSON document
      def load_associated_collections # :nodoc:
        collection.try(:load_associated_collections)
      end
    
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
storage_room-0.3.24 lib/storage_room/models/entry.rb
storage_room-0.3.23 lib/storage_room/models/entry.rb
storage_room-0.3.22 lib/storage_room/models/entry.rb
storage_room-0.3.21 lib/storage_room/models/entry.rb
storage_room-0.3.20 lib/storage_room/models/entry.rb
storage_room-0.3.19 lib/storage_room/models/entry.rb
storage_room-0.3.18 lib/storage_room/models/entry.rb
storage_room-0.3.17 lib/storage_room/models/entry.rb
storage_room-0.3.16 lib/storage_room/models/entry.rb
storage_room-0.3.15 lib/storage_room/models/entry.rb
storage_room-0.3.14 lib/storage_room/models/entry.rb