Module: Schemas

Defined in:
lib/json_column.rb

Class Method Summary collapse

Class Method Details

.const_missing(name) ⇒ Object



17
18
19
# File 'lib/json_column.rb', line 17

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

.load_schema_file(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/json_column.rb', line 21

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