Sha256: 56e8dd3d7b431b8c3cca08b218bd91a5e37dce8d929d19440290dc95e8c6d861

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

require File.dirname(__FILE__) + '/simple_property'
require File.dirname(__FILE__) + '/belongs_to_property'

module CouchTomato
  module Persistence
    module Properties
      def self.included(base)
        base.extend ClassMethods
        base.class_eval do
          def self.properties
            @properties ||= {}
            @properties[self.name] ||= []
          end
        end
      end

      module ClassMethods
        # returns all the property names of a model class that have been defined using the #property method
        #
        # example:
        #  class Book
        #    property :title
        #    property :year
        #  end
        #  Book.property_names # => [:title, :year]
        def property_names
          properties.map(&:name)
        end

        def json_create(json, meta={}) #:nodoc:
          return if json.nil?
          instance = super
          # instance.send(:assign_attribute_copies_for_dirty_tracking)
          instance.metadata = meta
          instance
        end

        # Declare a proprty on a model class. properties are not typed by default. You can use any of the basic types by JSON (String, Integer, Fixnum, Array, Hash). If you want a property to be of a custom class you have to define it using the :class option.
        #
        # example:
        #  class Book
        #    property :title
        #    property :year
        #    property :publisher, :class => Publisher
        #  end
        def property(name, options = {})
          clazz = options.delete(:class)
          properties << (clazz || SimpleProperty).new(self, name, options)
        end

        def belongs_to(name) #:nodoc:
          property name, :class => BelongsToProperty
        end

      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/properties.rb
couch_tomato-0.1.5 lib/couch_tomato/persistence/properties.rb
couch_tomato-0.1.4 lib/couch_tomato/persistence/properties.rb
couch_tomato-0.1.3 lib/couch_tomato/persistence/properties.rb
couch_tomato-0.1.2 lib/couch_tomato/persistence/properties.rb
couch_tomato-0.1.1 lib/couch_tomato/persistence/properties.rb
couch_tomato-0.1.0 lib/couch_tomato/persistence/properties.rb