forked from THCLab/oca-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from blelump/work
A lot of refactoring
- Loading branch information
Showing
42 changed files
with
2,842 additions
and
458 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
require 'fileutils' | ||
require 'odca/odca.rb' | ||
require 'odca/hashlink_generator' | ||
require 'odca/schema_parser' | ||
require 'json' | ||
require 'pp' | ||
|
||
module Odca | ||
class BigParser | ||
attr_reader :records, :output_dir, :overlay_dtos | ||
|
||
def initialize(records, output_dir) | ||
@overlay_dtos = [] | ||
@records = records | ||
@output_dir = output_dir | ||
end | ||
|
||
def call | ||
columns_number = records[0].size | ||
|
||
puts 'Reading overlays ...' | ||
(6..columns_number - 1).each do |i| | ||
overlay_dtos << OverlayDto.new( | ||
index: i, | ||
name: records[2][i], | ||
role: records[0][i], | ||
purpose: records[1][i], | ||
language: records[3][i] | ||
) | ||
end | ||
records.slice!(0, 4) | ||
|
||
puts 'Overlays loaded, start creating objects' | ||
schemas = separate_schemas(records) | ||
|
||
schemas.each do |schema| | ||
schema_base, overlays = schema.call | ||
save(schema_base: schema_base, overlays: overlays) | ||
end | ||
end | ||
|
||
private def separate_schemas(records) | ||
schema_name = '' | ||
schema_first_row = 0 | ||
records.each_with_object([]).with_index do |(row, memo), i| | ||
schema_name = row[0] if i.zero? | ||
next_record = records[i + 1] | ||
next if next_record && schema_name == next_record[0] | ||
memo << Odca::SchemaParser.new( | ||
records[schema_first_row..i], overlay_dtos | ||
) | ||
schema_name = next_record[0] if next_record | ||
schema_first_row = i + 1 | ||
end | ||
end | ||
|
||
def save(schema_base:, overlays:) | ||
path = "#{output_dir}/#{schema_base.name}" | ||
|
||
puts "Writing SchemaBase: #{schema_base.name}" | ||
save_schema_base(schema_base, path: path) | ||
|
||
overlays.each do |overlay| | ||
next if overlay.empty? | ||
puts "Processing #{overlay.description}" | ||
|
||
puts 'Saving object...' | ||
save_overlay( | ||
Odca::ParentfulOverlay.new( | ||
parent: schema_base, overlay: overlay | ||
), | ||
path: path | ||
) | ||
end | ||
end | ||
|
||
def save_schema_base(schema_base, path:) | ||
unless Dir.exist?(path) | ||
puts 'Create dir' | ||
FileUtils.mkdir_p(path) | ||
end | ||
|
||
File.open("#{path}.json", 'w') do |f| | ||
f.write(JSON.pretty_generate(schema_base)) | ||
end | ||
end | ||
|
||
def save_overlay(parentful_overlay, path:) | ||
overlay_class_name = parentful_overlay.overlay.class | ||
.name.split('::').last | ||
hl = 'hl:' + HashlinkGenerator.call(parentful_overlay) | ||
|
||
File.open("#{path}/#{overlay_class_name}-#{hl}.json", 'w') do |f| | ||
f.write(JSON.pretty_generate(parentful_overlay)) | ||
end | ||
end | ||
|
||
class OverlayDto | ||
attr_reader :index, :name, :role, :purpose, :language | ||
|
||
def initialize(index:, name:, role:, purpose:, language:) | ||
@index = index | ||
@name = name | ||
@role = role | ||
@purpose = purpose | ||
@language = language | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
require 'digest/sha2' | ||
require 'base58' | ||
require 'json' | ||
|
||
module Odca | ||
class HashlinkGenerator | ||
def self.call(schema) | ||
Base58.encode( | ||
Digest::SHA2.hexdigest( | ||
JSON.pretty_generate(schema) | ||
).to_i(16) | ||
) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Odca | ||
class NullValue | ||
%i[to_s to_str].each do |method| | ||
define_method method do | ||
'' | ||
end | ||
end | ||
|
||
def empty? | ||
true | ||
end | ||
|
||
def nil? | ||
true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'odca/schema_base' | ||
require 'odca/parentful_overlay' | ||
require 'odca/overlays/format_overlay.rb' | ||
require 'odca/overlays/label_overlay.rb' | ||
require 'odca/overlays/encode_overlay.rb' | ||
require 'odca/overlays/entry_overlay.rb' | ||
require 'odca/overlays/information_overlay.rb' | ||
require 'odca/overlays/conditional_overlay.rb' | ||
require 'odca/overlays/source_overlay.rb' | ||
require 'odca/overlays/review_overlay.rb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'odca/overlays/header' | ||
|
||
module Odca | ||
module Overlays | ||
class ConditionalOverlay | ||
extend Forwardable | ||
attr_accessor :hidden_attributes, :required_attributes | ||
attr_reader :header | ||
|
||
def_delegators :header, | ||
:issued_by, :type, | ||
:role, :purpose, | ||
:description, :description= | ||
|
||
def initialize(header) | ||
header.type = 'spec/overlay/conditional/1.0' | ||
@header = header | ||
end | ||
|
||
def as_json(options = {}) | ||
to_h.to_json(*options) | ||
end | ||
|
||
def to_h | ||
header.to_h.merge( | ||
hidden_attributes: hidden_attributes, | ||
required_attributes: required_attributes | ||
) | ||
end | ||
|
||
# @deprecated | ||
def is_valid? | ||
warn('[DEPRECATION] `is_valid?` is deprecated. ' \ | ||
'Please use `empty?` instead.') | ||
!empty? | ||
end | ||
|
||
def empty? | ||
hidden_attributes.empty? || required_attributes.empty? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
require 'odca/overlays/header' | ||
require 'odca/null_value' | ||
|
||
module Odca | ||
module Overlays | ||
class EncodeOverlay | ||
extend Forwardable | ||
attr_accessor :language | ||
attr_reader :encoding_attributes, :header | ||
|
||
DEFAULT_ENCODING = 'utf-8'.freeze | ||
|
||
def_delegators :header, | ||
:issued_by, :type, | ||
:role, :purpose, | ||
:description, :description= | ||
|
||
def initialize(header) | ||
@encoding_attributes = [] | ||
header.type = 'spec/overlay/encode/1.0' | ||
@header = header | ||
end | ||
|
||
def to_json(options = {}) | ||
to_h.to_json(*options) | ||
end | ||
|
||
def to_h | ||
header.to_h.merge( | ||
language: language, | ||
default_encoding: DEFAULT_ENCODING, | ||
attr_encoding: attr_encoding | ||
) | ||
end | ||
|
||
# @deprecated | ||
def is_valid? | ||
warn('[DEPRECATION] `is_valid?` is deprecated. ' \ | ||
'Please use `empty?` instead.') | ||
!empty? | ||
end | ||
|
||
def empty? | ||
encoding_attributes.empty? | ||
end | ||
|
||
def add_encoding_attribute(encoding_attribute) | ||
return if encoding_attribute.nil? || encoding_attribute.encoding.empty? | ||
encoding_attributes << encoding_attribute | ||
end | ||
|
||
private def attr_encoding | ||
encoding_attributes.each_with_object({}) do |attr, memo| | ||
memo[attr.attr_name] = attr.encoding | ||
end | ||
end | ||
|
||
class EncodingAttribute | ||
attr_reader :attr_name, :encoding | ||
|
||
def initialize(attr_name:, encoding:) | ||
@attr_name = attr_name | ||
@encoding = encoding | ||
end | ||
end | ||
|
||
class InputValidator | ||
attr_reader :attr_name, :value | ||
|
||
def initialize(attr_name:, value:) | ||
if attr_name.strip.empty? | ||
raise 'Attribute name is expected to be non empty String' | ||
end | ||
|
||
@attr_name = attr_name | ||
@value = value | ||
end | ||
|
||
def call | ||
encoding = if value.nil? || value.strip.empty? | ||
Odca::NullValue.new | ||
else | ||
value.strip | ||
end | ||
|
||
{ | ||
attr_name: attr_name.strip, | ||
encoding: encoding | ||
} | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.