Sha256: 18fab9359a4e1ea6908d8aca88182f7918aece0f5b7cb0762fde5efc7748349a

Contents?: true

Size: 894 Bytes

Versions: 5

Compression:

Stored size: 894 Bytes

Contents

require 'json'
require 'fileutils'

module Spout
  module Models
    # Base class for spout variables, forms, and domains that are read from JSON
    # files
    class Record
      class << self
        # Only returns records with zero json errors, nil otherwise
        def find_by_id(id)
          file_name = Dir.glob(expected_path(id), File::FNM_CASEFOLD).first
          variable = new(file_name, dictionary_root)
          (variable.errors.size > 0 ? nil : variable)
        end

        private

        def record_folder
          "#{name.split('::').last.to_s.downcase}s"
        end

        def expected_filename(id)
          "#{id.to_s.downcase}.json"
        end

        def expected_path(id)
          File.join(dictionary_root, record_folder, '**', expected_filename(id))
        end

        def dictionary_root
          FileUtils.pwd
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spout-0.11.0 lib/spout/models/record.rb
spout-0.11.0.rc lib/spout/models/record.rb
spout-0.11.0.beta3 lib/spout/models/record.rb
spout-0.11.0.beta2 lib/spout/models/record.rb
spout-0.11.0.beta1 lib/spout/models/record.rb