Sha256: e91357721fd67f460de6da496b20a584a59198d0a215b7fd5e3b5d40dcd05f16

Contents?: true

Size: 1.14 KB

Versions: 3

Compression:

Stored size: 1.14 KB

Contents

module ParseModel

  module Model
    attr_accessor :PFObject
    
    def initialize(pf_object=nil)
      if pf_object
        @PFObject = pf_object
      else
        @PFObject = PFObject.objectWithClassName(self.class.to_s)
      end
    end
    
    def method_missing(method, *args, &block)
      if fields.include?(method)
        @PFObject.objectForKey(method)
      elsif fields.map {|f| "#{f}="}.include?("#{method}")
        method = method.split("=")[0]
        @PFObject.setObject(args.first, forKey:method)
      elsif @PFObject.respond_to?(method)
        @PFObject.send(method, *args, &block)
      else
        super
      end
    end
        
    def fields
      self.class.send(:get_fields)
    end
  
    module ClassMethods
      def fields(*args)
        args.each {|arg| field(arg)}
      end
    
      def field(name)
        @fields ||= []
        @fields << name
      end

      def query
        ParseModel::Query.alloc.initWithClassNameAndClassObject(self.name, classObject:self)
      end
      
      def get_fields
        @fields
      end
    end
    
    def self.included(base)
      base.extend(ClassMethods)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ParseModel-0.0.6 lib/ParseModel/Model.rb
ParseModel-0.0.5 lib/ParseModel/Model.rb
ParseModel-0.0.3 lib/ParseModel/Model.rb