Sha256: 20131c4d05335edef5bcff05b93956a3cfe6f8b496911cfcab08bfcad59fb955

Contents?: true

Size: 1.64 KB

Versions: 8

Compression:

Stored size: 1.64 KB

Contents

module JsonApiClient
  class Schema
    Property = Struct.new(:name, :type, :default) do
      def cast(value)
        return nil if value.nil?
        return value if type.nil?

        case type.to_sym
        when :int, :integer
          value.to_i
        when :string
          value.to_s
        when :float
          value.to_f
        when :time
          value.is_a?(Time) || nil ? value : Time.parse(value)
        when :boolean
          if value.is_a?(String)
            value == "false" ? false : true
          else
            !!value
          end
        else
          value
        end
      end
    end

    def initialize
      @properties = {}
    end

    # Add a property to the schema
    #
    # @param name [Symbol] the name of the property
    # @param options [Hash] property options
    # @option options [Symbol] :type The property type
    # @option options [Symbol] :default The default value for the property
    # @return [void]
    def add(name, options)
      @properties[name.to_sym] = Property.new(name.to_sym, options[:type], options[:default])
    end

    # How many properties are defined
    #
    # @return [Fixnum] the number of defined properties
    def size
      @properties.size
    end
    alias_method :length, :size

    def each_property(&block)
      @properties.values.each(&block)
    end
    alias_method :each, :each_property

    # Look up a property by name
    #
    # @param property_name [String] the name of the property
    # @return [Property, nil] the property definition for property_name or nil
    def find(property_name)
      @properties[property_name.to_sym]
    end
    alias_method :[], :find
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
json_api_client-1.0.0 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta7 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta6 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta5 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta4 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta3 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta2 lib/json_api_client/schema.rb
json_api_client-1.0.0.beta lib/json_api_client/schema.rb