Sha256: 2bec8d377410b2ee685d4ffe6c1e9ccd0cd53d92239b18ee93a65380453561ba

Contents?: true

Size: 1.7 KB

Versions: 3

Compression:

Stored size: 1.7 KB

Contents

module CouchRest::Model
  module CastedModel
   
    extend ActiveSupport::Concern

    included do
      include CouchRest::Model::Configuration
      include CouchRest::Model::Callbacks
      include CouchRest::Model::Properties
      include CouchRest::Model::PropertyProtection
      include CouchRest::Model::Associations
      include CouchRest::Model::Validations
      attr_accessor :casted_by
    end
    
    def initialize(keys = {})
      raise StandardError unless self.is_a? Hash
      prepare_all_attributes(keys)
      super()
    end
    
    def []= key, value
      super(key.to_s, value)
    end
    
    def [] key
      super(key.to_s)
    end
    
    # Gets a reference to the top level extended
    # document that a model is saved inside of
    def base_doc
      return nil unless @casted_by
      @casted_by.base_doc
    end
    
    # False if the casted model has already
    # been saved in the containing document
    def new?
      @casted_by.nil? ? true : @casted_by.new?
    end
    alias :new_record? :new?

    def persisted?
      !new?
    end

    # The to_param method is needed for rails to generate resourceful routes.
    # In your controller, remember that it's actually the id of the document.
    def id
      return nil if base_doc.nil?
      base_doc.id
    end
    alias :to_key :id
    alias :to_param :id
    
    # Sets the attributes from a hash
    def update_attributes_without_saving(hash)
      hash.each do |k, v|
        raise NoMethodError, "#{k}= method not available, use property :#{k}" unless self.respond_to?("#{k}=")
      end      
      hash.each do |k, v|
        self.send("#{k}=",v)
      end
    end
    alias :attributes= :update_attributes_without_saving
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
couchrest_model-1.1.0.beta lib/couchrest/model/casted_model.rb
couchrest_model-1.0.0 lib/couchrest/model/casted_model.rb
couchrest_model-radiant-1.0.0 lib/couchrest/model/casted_model.rb