Sha256: 898e2c9bb49cc003b79c12e4853fb9bcb9ccfd2a4751fe1a62d02df8540b6709

Contents?: true

Size: 1.84 KB

Versions: 18

Compression:

Stored size: 1.84 KB

Contents

module JsonApiClient
  module Helpers
    module Schemable
      extend ActiveSupport::Concern

      included do
        initializer do |obj, params|
          obj.send(:set_default_values)
        end
      end

      module ClassMethods
        # Returns the schema for this resource class
        #
        # @return [Schema] the schema for this resource class
        def schema
          @schema ||= Schema.new
        end

        # Declares a new property by name
        #
        # @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
        def property(name, options = {})
          schema.add(name, options)
        end

        # Declare multiple properties with the same optional options
        #
        # @param [Array<Symbol>] names
        # @param options [Hash] property options
        # @option options [Symbol] :type The property type
        # @option options [Symbol] :default The default value for the property
        def properties(*names)
          options = names.last.is_a?(Hash) ? names.pop : {}
          names.each do |name|
            property name, options
          end
        end
      end

      protected

      def set_attribute(name, value)
        property = property_for(name)
        value = property.cast(value) if property
        super(name, value)
      end

      def has_attribute?(attr_name)
        !!property_for(attr_name) || super
      end

      def set_default_values
        self.class.schema.each_property do |property|
          attributes[property.name] = property.default unless attributes.has_key?(property.name)
        end
      end

      def property_for(name)
        self.class.schema.find(name)
      end

    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/helpers/schemable.rb
json_api_client-0.9.5 lib/json_api_client/helpers/schemable.rb
json_api_client-0.9.4 lib/json_api_client/helpers/schemable.rb
json_api_client-0.9.3 lib/json_api_client/helpers/schemable.rb
json_api_client-1.0.0.beta5 lib/json_api_client/helpers/schemable.rb
json_api_client-1.0.0.beta4 lib/json_api_client/helpers/schemable.rb
json_api_client-0.9.2 lib/json_api_client/helpers/schemable.rb
json_api_client-1.0.0.beta3 lib/json_api_client/helpers/schemable.rb
json_api_client-1.0.0.beta2 lib/json_api_client/helpers/schemable.rb
json_api_client-0.9.0 lib/json_api_client/helpers/schemable.rb
json_api_client-1.0.0.beta lib/json_api_client/helpers/schemable.rb
json_api_client-0.8.1 lib/json_api_client/helpers/schemable.rb
json_api_client-0.8.0 lib/json_api_client/helpers/schemable.rb
json_api_client-0.7.1 lib/json_api_client/helpers/schemable.rb
json_api_client-0.7.0 lib/json_api_client/helpers/schemable.rb
json_api_client-0.6.0 lib/json_api_client/helpers/schemable.rb
json_api_client-0.5.1 lib/json_api_client/helpers/schemable.rb
json_api_client-0.5.0 lib/json_api_client/helpers/schemable.rb