Sha256: 135c96c0dc8a7242f49ef62bdefd705e7900375f4d5cf9239337b008df18b9ca

Contents?: true

Size: 1.21 KB

Versions: 33

Compression:

Stored size: 1.21 KB

Contents

module CouchRest
  
  # Basic attribute support for adding getter/setter + validation
  class Property
    attr_reader :name, :type, :read_only, :alias, :default, :casted, :init_method, :options
    
    # attribute to define
    def initialize(name, type = nil, options = {})
      @name = name.to_s
      parse_type(type)
      parse_options(options)
      self
    end
    
    
    private
    
      def parse_type(type)
        if type.nil?
          @type = 'String'
        elsif type.is_a?(Array) && type.empty?
          @type = 'Array'
        else
          @type = type.is_a?(Array) ? [type.first.to_s] : type.to_s
        end
      end
      
      def parse_options(options)
        return if options.empty?
        @validation_format  = options.delete(:format)     if options[:format]
        @read_only          = options.delete(:read_only)  if options[:read_only]
        @alias              = options.delete(:alias)      if options[:alias]
        @default            = options.delete(:default)    if options[:default]
        @casted             = options[:casted] ? true : false
        @init_method        = options[:send] ? options.delete(:send) : 'new'
        @options            = options
      end
    
  end
end

Version data entries

33 entries across 33 versions & 15 rubygems

Version Path
brianmario-couchrest-0.23 lib/couchrest/more/property.rb
gbuesing-couchrest-0.23 lib/couchrest/more/property.rb
glasner-couchrest-0.2.2 lib/couchrest/more/property.rb
gohanlonllc-couchrest-0.2.3.1 lib/couchrest/more/property.rb
halfninja-couchrest-0.23.2 lib/couchrest/more/property.rb
halfninja-couchrest-0.23.3 lib/couchrest/more/property.rb
jchris-couchrest-0.16 lib/couchrest/more/property.rb
jchris-couchrest-0.17.0 lib/couchrest/more/property.rb
jchris-couchrest-0.2.1 lib/couchrest/more/property.rb
jchris-couchrest-0.2.2 lib/couchrest/more/property.rb
jchris-couchrest-0.2 lib/couchrest/more/property.rb
jchris-couchrest-0.22 lib/couchrest/more/property.rb
jchris-couchrest-0.23 lib/couchrest/more/property.rb
jkestr-couchrest-0.23 lib/couchrest/more/property.rb
jrun-couchrest-0.17.1 lib/couchrest/more/property.rb
jrun-couchrest-0.2.1.1 lib/couchrest/more/property.rb
jrun-couchrest-0.2.1 lib/couchrest/more/property.rb
mattetti-couchrest-0.15 lib/couchrest/more/property.rb
mattetti-couchrest-0.16 lib/couchrest/more/property.rb
mattetti-couchrest-0.17 lib/couchrest/more/property.rb