Sha256: 6c4dda5f282bf7fde128305b633dba48618948952b913a0d5db6b40bb20d84e9

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

module Parse
    module Model
        module Scaffold
            
            class ParseInspector

                Attribute = Struct.new :name, :type do
                    def to_s
                        self.name
                    end
                end

                @@protected_fields = ['objectId', 'createdAt', 'updatedAt', 'ACL']

                def self.parse(obj)

                    @@protected_fields.each {|x| obj.delete x}

                    attrs = []

                    obj.each do |k, v|
                        value_type = determine_type(v)
                        attr = Attribute.new(k, value_type)
                        attrs << attr
                    end

                    attrs
                end

                # Map Parse types to Ruby types if possible, otherwise, leave them as a 
                def self.determine_type(val)

                    return val.class.to_s.to_sym if [String, Array].include? val.class

                    if val.is_a? Hash

                        # If we don't see the Parse '__type' key that they use for encoding
                        # complex types, then this is just a simple object
                        return Hash if !val.has_key? '__type'

                        # Otherwise, this is probably a Parse type (Relation, Pointer, GeoPoint, etc.)
                        return val['__type'].to_sym
                    end

                    return :Boolean if [TrueClass, FalseClass].include? val.class

                    # Last resort
                    val.class.to_s.to_sym
                end
            end
        end
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
parse-model-scaffold-0.1.1 lib/parse-model-scaffold/parse_inspector.rb
parse-model-scaffold-0.0.1 lib/parse-model-scaffold/parse_inspector.rb