Sha256: c10c138b630830920e8b59fb9c2b68b7a3e48f9ee88cc5db7c7060f0d949e51e
Contents?: true
Size: 1.75 KB
Versions: 6
Compression:
Stored size: 1.75 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 case value when "false", "0", 0, false false when "true", "1", 1, true true else # if it's unknown, use the default value default 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
6 entries across 6 versions & 1 rubygems