Class: CollectionBase::Base
- Inherits:
-
Object
- Object
- CollectionBase::Base
- Defined in:
- lib/collection_base/base.rb
Instance Attribute Summary collapse
-
#base_path ⇒ Object
Returns the value of attribute base_path.
Instance Method Summary collapse
-
#initialize(base_path) ⇒ Base
constructor
A new instance of Base.
- #load_json(object) ⇒ Object
- #load_libraries(lib) ⇒ Object
- #open_head ⇒ Object
- #open_json(file) ⇒ Object
- #open_sample(model) ⇒ Object
- #process_libraries ⇒ Object
- #process_model(object) ⇒ Object
- #sample_data(file) ⇒ Object
- #shared_base ⇒ Object
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_path ⇒ Object
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. 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. end |
#open_head ⇒ Object
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. 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. 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. end |
#process_libraries ⇒ Object
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_base ⇒ Object
41 42 43 |
# File 'lib/collection_base/base.rb', line 41 def shared_base JSON.parse(open_head) end |