Sha256: cf66fa6c0c7dcbfc3aac7ea789940b79adff2c649c4b55dfac5af63167ff1363

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

require 'active_support/concern'
module SchemaTools
  module Modules
    # Add schema properties to a class by including this module and defining from
    # which schema to inherit attributes.
    module Attributes
      extend ActiveSupport::Concern

      def schema_attrs
        @schema_attrs ||= {}
      end

      module ClassMethods

        # @param [Symbol|String] schema name
        # @param [Hash<Symbol|String>] opts
        # @options opts [String] :path schema path
        def has_schema_attrs(schema, opts={})
          schema = SchemaTools::Reader.read(schema, opts[:path])
          # make getter / setter
          schema[:properties].each do |key, val|
            # getter
            define_method key do
              schema_attrs[key]
            end
            #setter
            unless val[:readonly]
              define_method "#{key}=" do |value|
                #TODO validations?
                schema_attrs[key] = value
              end
            end
          end
        end

      end # ClassMethods

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
json_schema_tools-0.0.6 lib/schema_tools/modules/attributes.rb
json_schema_tools-0.0.5 lib/schema_tools/modules/attributes.rb
json_schema_tools-0.0.4 lib/schema_tools/modules/attributes.rb
json_schema_tools-0.0.3 lib/schema_tools/modules/attributes.rb
json_schema_tools-0.0.2 lib/schema_tools/modules/attributes.rb