Sha256: 706fa8d96dfd2cce63e027dc8a83a01d8538b6bb35c9e833ade146eff9a7c98e

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

require 'json-schema'
require 'json_column/json_validator'
require 'json_column/json_column_array'
require 'json_column/json_column_object'
require 'json_column/acts_as_json_column'

module JsonColumn
  # class Railtie < ::Rails::Railtie
  #   initializer 'activeservice.autoload', :before => :set_autoload_paths do |app|
  #     app.config.autoload_paths << Rails.root.join('app', 'models', 'schemas')
  #   end
  # end
end


module Schemas
  def self.const_missing(name)
    self.const_set(name, self.load_schema_file(name))
  end

  def self.load_schema_file(name)
    file = Dir["#{Rails.root}/app/models/schemas/#{name.to_s.underscore}*"].select {|f| f =~ /.*.(json|ya?ml)\z/ }
    if file.blank?
      raise "no such schema defined: #{name}"
    end
    sch = YAML.load_file(file[0]).with_indifferent_access

    # if we did find the schema file we create a module
    # with the schema accessible in the schema method
    # this looks like if the yml file is in fact a ruby
    # module.
    Module.new do
      @schema = sch
      def self.schema
        @schema
      end
    end
  end
end


## we could preload schema file in cache by looping and invoking all Schemas::(constant)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json_column-0.6.6 lib/json_column.rb
json_column-0.6.5 lib/json_column.rb