Sha256: b15d0842fc2737d2d894227994585b8f37af1e99c4f58c467b07b3bf2204e73f

Contents?: true

Size: 1.84 KB

Versions: 41

Compression:

Stored size: 1.84 KB

Contents

require 'active_resource/exceptions'

module ActiveResource # :nodoc:
  class Schema # :nodoc:
    # attributes can be known to be one of these types. They are easy to
    # cast to/from.
    KNOWN_ATTRIBUTE_TYPES = %w( string integer float )

    # An array of attribute definitions, representing the attributes that
    # have been defined.
    attr_accessor :attrs

    # The internals of an Active Resource Schema are very simple -
    # unlike an Active Record TableDefinition (on which it is based).
    # It provides a set of convenience methods for people to define their
    # schema using the syntax:
    #  schema do
    #    string :foo
    #    integer :bar
    #  end
    #
    #  The schema stores the name and type of each attribute. That is then
    #  read out by the schema method to populate the actual
    #  Resource's schema
    def initialize
      @attrs = {}
    end

    def attribute(name, type, options = {})
      raise ArgumentError, "Unknown Attribute type: #{type.inspect} for key: #{name.inspect}" unless type.nil? || Schema::KNOWN_ATTRIBUTE_TYPES.include?(type.to_s)

      the_type = type.to_s
      # TODO: add defaults
      #the_attr = [type.to_s]
      #the_attr << options[:default] if options.has_key? :default
      @attrs[name.to_s] = the_type
      self
    end

    # The following are the attribute types supported by Active Resource
    # migrations.
    # TODO:  We should eventually support all of these:
    # %w( string text integer float decimal datetime timestamp time date binary boolean ).each do |attr_type|
    KNOWN_ATTRIBUTE_TYPES.each do |attr_type|
      class_eval <<-EOV, __FILE__, __LINE__ + 1
        def #{attr_type.to_s}(*args)
          options = args.extract_options!
          attr_names = args

          attr_names.each { |name| attribute(name, '#{attr_type}', options) }
        end
      EOV
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
activeresource-3.0.20 lib/active_resource/schema.rb
activeresource-3.0.19 lib/active_resource/schema.rb
activeresource-3.0.18 lib/active_resource/schema.rb
activeresource-3.0.17 lib/active_resource/schema.rb
activeresource-3.0.16 lib/active_resource/schema.rb
activeresource-3.0.15 lib/active_resource/schema.rb
activeresource-3.0.14 lib/active_resource/schema.rb
activeresource-3.0.13 lib/active_resource/schema.rb
activeresource-3.0.13.rc1 lib/active_resource/schema.rb
activeresource-3.0.12 lib/active_resource/schema.rb
activeresource-3.0.12.rc1 lib/active_resource/schema.rb
activeresource-3.0.11 lib/active_resource/schema.rb
activeresource-3.0.10 lib/active_resource/schema.rb
activeresource-3.0.10.rc1 lib/active_resource/schema.rb
activeresource-3.0.9 lib/active_resource/schema.rb
activeresource-3.0.9.rc5 lib/active_resource/schema.rb
activeresource-3.0.9.rc4 lib/active_resource/schema.rb
activeresource-3.0.9.rc3 lib/active_resource/schema.rb
activeresource-3.0.9.rc1 lib/active_resource/schema.rb
activeresource-3.0.8 lib/active_resource/schema.rb