Sha256: 6b26c298862b6ef9a03b175368bf6b2a03aa2c61a9af834abe6d0a52495d4063

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

module Siteleaf
  class Entity
    
    def initialize(attributes = {})
      self.attributes = attributes    
    end
    
    def self.all
      result = Client.get "#{self.endpoint}"
      result.map { |r| self.new(r) } if result
    end
    
    def self.find(id)
      result = Client.get "#{self.endpoint}/#{id}"
      self.new(result) if result
    end
    
    def self.create(attributes = {})
      self.new(attributes).save
    end
    
    def save
      if self.id
        result = Client.put "#{self.class.endpoint}/#{self.id}", self.attributes
      else
        result = Client.post "#{self.create_endpoint}", self.attributes
      end
      if result
        self.attributes = result
        return self
      end
    end
    
    def self.delete(id)
      Client.delete "#{self.endpoint}/#{id}"
    end
    
    def delete
      Client.delete "#{self.class.endpoint}/#{self.id}"
    end
    
    def attributes
      Hash[self.instance_variables.map { |name| [name[1..-1], self.instance_variable_get(name)] }]
    end
    
    def attributes=(attributes = {})
      attributes.each_pair { |k, v| self.instance_variable_set("@#{k}", v) }
    end
    
    def self.class_name
      self.name.split('::')[-1]
    end
    
    def self.endpoint
      "#{self.class_name.downcase}s"
    end
    
    def create_endpoint
      self.class.endpoint
    end
    
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
siteleaf-0.9.14 lib/siteleaf/entity.rb
siteleaf-0.9.13 lib/siteleaf/entity.rb
siteleaf-0.9.12 lib/siteleaf/entity.rb
siteleaf-0.9.11 lib/siteleaf/entity.rb
siteleaf-0.9.10 lib/siteleaf/entity.rb
siteleaf-0.9.9 lib/siteleaf/entity.rb
siteleaf-0.9.8 lib/siteleaf/entity.rb
siteleaf-0.9.7 lib/siteleaf/entity.rb
siteleaf-0.9.6 lib/siteleaf/entity.rb
siteleaf-0.9.5 lib/siteleaf/entity.rb
siteleaf-0.9.3 lib/siteleaf/entity.rb
siteleaf-0.9.2 lib/siteleaf/entity.rb
siteleaf-0.9.1 lib/siteleaf/entity.rb
siteleaf-0.9.0 lib/siteleaf/entity.rb