Sha256: 4a7e8294307b4f68b706704273ee087b4688bd30a4c49c954e8c257aa8c7c07c

Contents?: true

Size: 1.92 KB

Versions: 156

Compression:

Stored size: 1.92 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 text integer float decimal datetime timestamp time date binary boolean )

    # 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 schema of the actual
    #  resource.
    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.
    KNOWN_ATTRIBUTE_TYPES.each do |attr_type|
      # def string(*args)
      #   options = args.extract_options!
      #   attr_names = args
      #
      #   attr_names.each { |name| attribute(name, 'string', options) }
      # end
      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

156 entries across 130 versions & 11 rubygems

Version Path
activeresource-3.2.22.5 lib/active_resource/schema.rb
activeresource-3.2.22.4 lib/active_resource/schema.rb
activeresource-3.2.22.3 lib/active_resource/schema.rb
activeresource-3.2.22.2 lib/active_resource/schema.rb
activeresource-3.2.22.1 lib/active_resource/schema.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activeresource-3.2.12/lib/active_resource/schema.rb
activeresource-3.2.22 lib/active_resource/schema.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activeresource-3.2.12/lib/active_resource/schema.rb
activeresource-3.2.21 lib/active_resource/schema.rb
activeresource-3.2.20 lib/active_resource/schema.rb
activeresource-3.2.19 lib/active_resource/schema.rb
activeresource-3.2.18 lib/active_resource/schema.rb
activeresource-3.2.17 lib/active_resource/schema.rb
activeresource-3.2.16 lib/active_resource/schema.rb
activeresource-3.2.15 lib/active_resource/schema.rb
activeresource-3.2.15.rc3 lib/active_resource/schema.rb
activeresource-3.2.15.rc2 lib/active_resource/schema.rb
activeresource-3.2.15.rc1 lib/active_resource/schema.rb
activeresource-3.2.14 lib/active_resource/schema.rb
activeresource-3.2.14.rc2 lib/active_resource/schema.rb