Sha256: 99f237f9f28f5719db5dc221e81518c50fd235e8f84c547b1c728664b3e0f5ac

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module OrientDB

  class Document

    def values
      field_names.map { |field_name| [field_name, self[field_name]] }
    end

    alias :db :database

    def [](field_name)
      field field_name.to_s
    end

    def []=(field_name, value)
      field field_name.to_s, value
    end

    def field?(name)
      contains_field(name.to_s) || schema_class.exists_property?(name.to_s)
    end

    def respond_to?(method_name)
      return true if field?(method_name.to_s)
      match = method_name.to_s.match(/(.*?)([?=!]?)$/)
      return true if match[2] == '='
      return true if match[2] == '?' && field?(match[1])
      super
    end

    def method_missing(method_name, *args, &blk)
      return self[method_name] if field?(method_name)

      match = method_name.to_s.match(/(.*?)([?=!]?)$/)
      case match[2]
        when "="
          self[match[1]] =  args.first
        when "?"
          field(match[1]) ? !!self[match[1]] : super
        else
          super
      end
    end

    def rid
      identity.to_s
    end

    def inspect
      props = values.map { |k, v| "#{k}:#{v.inspect}" }.join(' ')
      %{#<OrientDB::Document:#{class_name}:#{rid}#{props.empty? ? '' : ' ' + props}>}
    end

    alias :to_s :inspect

    class << self

      alias_method :native_new, :new

      def new(db, klass_name, fields = {})
        obj = native_new db, klass_name.to_s
        fields.each do |name, value|
          obj.field name.to_s, value
        end
        obj
      end

      def create(db, klass_name, fields = {})
        obj = new db, klass_name, fields
        obj.save
        obj
      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
orientdb-0.0.10-jruby lib/orientdb/document.rb
orientdb-0.0.9-jruby lib/orientdb/document.rb