Class: CollectionBase::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/collection_base/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path) ⇒ Base

Returns a new instance of Base.



6
7
8
# File 'lib/collection_base/base.rb', line 6

def initialize (base_path)
  @base_path = base_path
end

Instance Attribute Details

#base_pathObject

Returns the value of attribute base_path.



4
5
6
# File 'lib/collection_base/base.rb', line 4

def base_path
  @base_path
end

Instance Method Details

#load_json(object) ⇒ Object



67
68
69
70
71
# File 'lib/collection_base/base.rb', line 67

def load_json(object)
  Dir.glob("#{@base_path}/#{object}/*.json")
rescue => e
  e.message
end

#load_libraries(lib) ⇒ Object



73
74
75
76
77
# File 'lib/collection_base/base.rb', line 73

def load_libraries(lib)
  Dir.glob("#{@base_path}/libraries/#{lib}/*.json")
rescue => e
  e.message
end

#open_headObject



55
56
57
58
59
# File 'lib/collection_base/base.rb', line 55

def open_head
  File.open(@base_path + '/index.json', mode: "r:utf-8").read
rescue => e
  e.message
end

#open_json(file) ⇒ Object



49
50
51
52
53
# File 'lib/collection_base/base.rb', line 49

def open_json(file)
  File.open(file, mode: "r:utf-8").read
rescue => e
  e.message
end

#open_sample(model) ⇒ Object



61
62
63
64
65
# File 'lib/collection_base/base.rb', line 61

def open_sample(model)
  File.open(@base_path + "/support/sample/#{model}.json", mode: "r:utf-8").read
rescue => e
  e.message
end

#process_librariesObject



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

def process_libraries
  results = []
  lib_index = @base_path + '/libraries/index.json'
  libraries = JSON.parse(open_json(lib_index)).to_a
  libraries.collect do |lib|
    schemas = []
    files = load_libraries(lib["file"]).to_a
    files.collect do |file|
      hash = {"uri" => File.basename(file),
              "schema" => open_json(file).to_s,
              "library" => {
                  "_reference" => true,
                  "name" => lib["name"]
              }
      }
      schemas << hash
    end
    results << {"name" => lib["name"], "schemas" => schemas}
  end
  results
end

#process_model(object) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/collection_base/base.rb', line 10

def process_model(object)
  result = []
  files = load_json(object).to_a
  files.collect do |file|
    result << JSON.parse(open_json(file))
  end
  result
end

#sample_data(file) ⇒ Object



45
46
47
# File 'lib/collection_base/base.rb', line 45

def sample_data(file)
  JSON.parse(open_sample(file))
end

#shared_baseObject



41
42
43
# File 'lib/collection_base/base.rb', line 41

def shared_base
  JSON.parse(open_head)
end