Sha256: c5ebcde98d93dc148345059bb7945797da12e3f3122e05564b0b7a21dd821a04

Contents?: true

Size: 1.8 KB

Versions: 7

Compression:

Stored size: 1.8 KB

Contents

module CouchTomato
  module Persistence
    module Base
      # initialize a new instance of the model optionally passing it a hash of attributes.
      # the attributes have to be declared using the #property method
      # 
      # example: 
      #   class Book
      #     include CouchTomato::Persistence
      #     property :title
      #   end
      #   book = Book.new :title => 'Time to Relax'
      #   book.title # => 'Time to Relax'
      def initialize(attributes = {})
        attributes.each do |name, value|
          self.send("#{name}=", value)
        end if attributes
      end
    
      # assign multiple attributes at once.
      # the attributes have to be declared using the #property method
      #
      # example:
      #   class Book
      #     include CouchTomato::Persistence
      #     property :title
      #     property :year
      #   end
      #   book = Book.new
      #   book.attributes = {:title => 'Time to Relax', :year => 2009}
      #   book.title # => 'Time to Relax'
      #   book.year # => 2009
      def attributes=(hash)
        hash.each do |attribute, value|
          self.send "#{attribute}=", value
        end
      end
    
      # returns all of a model's attributes that have been defined using the #property method as a Hash
      #
      # example:
      #   class Book
      #     include CouchTomato::Persistence
      #     property :title
      #     property :year
      #   end
      #   book = Book.new :year => 2009
      #   book.attributes # => {:title => nil, :year => 2009}
      def attributes
        self.class.properties.inject({}) do |res, property|
          property.serialize(res, self)
          res
        end
      end
    
      def ==(other) #:nodoc:
        other.class == self.class && self.to_json == other.to_json
      end
    
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
couch_tomato-0.2.0 lib/couch_tomato/persistence/base.rb
couch_tomato-0.1.5 lib/couch_tomato/persistence/base.rb
couch_tomato-0.1.4 lib/couch_tomato/persistence/base.rb
couch_tomato-0.1.3 lib/couch_tomato/persistence/base.rb
couch_tomato-0.1.2 lib/couch_tomato/persistence/base.rb
couch_tomato-0.1.1 lib/couch_tomato/persistence/base.rb
couch_tomato-0.1.0 lib/couch_tomato/persistence/base.rb