Sha256: eaa7d2ce757268f9eefc4234a4d13b68cb7f0a19d7ab7ee67d18d1b40eb425bf

Contents?: true

Size: 1.28 KB

Versions: 94

Compression:

Stored size: 1.28 KB

Contents

module Recurly
  class Schema
    # A mixin that allows a class to be treated like a recurly
    # object. This gives the class the power to describe
    # it's schema. It adds the *define_attribute* method
    # and a *schema* reader.
    module SchemaFactory

      # Gets the schema for this class
      # @return [Schema]
      def schema
        @schema ||= ::Recurly::Schema.new
      end

      protected

      # Macro that allows this class to define it's schema and associated
      # attribute getters and setters.
      #
      # @example
      #   class Account
      #     extend Schema::SchemaFactory
      #     define_attribute :code, String, {:read_only=>true}
      #   end
      #   account = Account.new(code: "mycode")
      #   account.schema
      #   #=> Recurly::Schema
      #   acount.code = "newcode" # this method protected since read_only = true
      #   account.code
      #   #=> "mycode"
      def define_attribute(name, type, options = {})
        attribute = schema.add_attribute(name, type, options)

        # Define the reader
        define_method(name) do
          self.attributes[name]
        end

        # Define the writer
        define_method("#{name}=") do |val|
          self.attributes[name] = val
        end

        self
      end
    end
  end
end

Version data entries

94 entries across 94 versions & 1 rubygems

Version Path
recurly-4.56.0 lib/recurly/schema/schema_factory.rb
recurly-4.55.0 lib/recurly/schema/schema_factory.rb
recurly-4.54.0 lib/recurly/schema/schema_factory.rb
recurly-4.53.0 lib/recurly/schema/schema_factory.rb
recurly-4.52.0 lib/recurly/schema/schema_factory.rb
recurly-4.49.0 lib/recurly/schema/schema_factory.rb
recurly-4.48.1 lib/recurly/schema/schema_factory.rb
recurly-4.48.0 lib/recurly/schema/schema_factory.rb
recurly-4.47.0 lib/recurly/schema/schema_factory.rb
recurly-4.46.0 lib/recurly/schema/schema_factory.rb
recurly-4.45.0 lib/recurly/schema/schema_factory.rb
recurly-4.44.0 lib/recurly/schema/schema_factory.rb
recurly-4.43.0 lib/recurly/schema/schema_factory.rb
recurly-4.42.0 lib/recurly/schema/schema_factory.rb
recurly-4.41.0 lib/recurly/schema/schema_factory.rb
recurly-4.40.0 lib/recurly/schema/schema_factory.rb
recurly-4.39.0 lib/recurly/schema/schema_factory.rb
recurly-4.38.0 lib/recurly/schema/schema_factory.rb
recurly-4.37.0 lib/recurly/schema/schema_factory.rb
recurly-3.28.0 lib/recurly/schema/schema_factory.rb