Sha256: 2549a950b6e33ac6135ef9811c7f321a2de46d26b9f29160230454b03cc8956f

Contents?: true

Size: 1.17 KB

Versions: 3

Compression:

Stored size: 1.17 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
        # @options opts [SchemaTools::Reader] :reader instance, instead of global reader/registry
        def has_schema_attrs(schema, opts={})
          reader = opts[:reader] || SchemaTools::Reader
          schema = 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

3 entries across 3 versions & 1 rubygems

Version Path
json_schema_tools-0.1.0 lib/schema_tools/modules/attributes.rb
json_schema_tools-0.0.8 lib/schema_tools/modules/attributes.rb
json_schema_tools-0.0.7 lib/schema_tools/modules/attributes.rb