Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add posibility to register external code lists. #198

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ DEPENDENCIES
yard (~> 0.9.12)

BUNDLED WITH
1.17.2
1.17.3
12 changes: 12 additions & 0 deletions lib/stupidedi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,16 @@ def self.caller(depth = 2)
k.split(":")
end
end

@@external_code_lists = Stupidedi::Schema::ExternalCodeListStorage.empty

class << self
def external_code_lists
@@external_code_lists
end
end

def self.configure
yield self
end
end
43 changes: 22 additions & 21 deletions lib/stupidedi/schema.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# frozen_string_literal: true
module Stupidedi
module Schema
autoload :AbstractElementUse, "stupidedi/schema/abstract_element_use"
autoload :AbstractUse, "stupidedi/schema/abstract_use"
autoload :ComponentElementUse, "stupidedi/schema/component_element_use"
autoload :CompositeElementUse, "stupidedi/schema/composite_element_use"
autoload :SegmentUse, "stupidedi/schema/segment_use"
autoload :SimpleElementUse, "stupidedi/schema/simple_element_use"
autoload :AbstractElementUse, "stupidedi/schema/abstract_element_use"
autoload :AbstractUse, "stupidedi/schema/abstract_use"
autoload :ComponentElementUse, "stupidedi/schema/component_element_use"
autoload :CompositeElementUse, "stupidedi/schema/composite_element_use"
autoload :SegmentUse, "stupidedi/schema/segment_use"
autoload :SimpleElementUse, "stupidedi/schema/simple_element_use"

autoload :AbstractDef, "stupidedi/schema/abstract_def"
autoload :AbstractElementDef, "stupidedi/schema/abstract_element_def"
autoload :CompositeElementDef, "stupidedi/schema/composite_element_def"
autoload :FunctionalGroupDef, "stupidedi/schema/functional_group_def"
autoload :InterchangeDef, "stupidedi/schema/interchange_def"
autoload :LoopDef, "stupidedi/schema/loop_def"
autoload :SegmentDef, "stupidedi/schema/segment_def"
autoload :SimpleElementDef, "stupidedi/schema/simple_element_def"
autoload :TableDef, "stupidedi/schema/table_def"
autoload :TransactionSetDef, "stupidedi/schema/transaction_set_def"
autoload :AbstractDef, "stupidedi/schema/abstract_def"
autoload :AbstractElementDef, "stupidedi/schema/abstract_element_def"
autoload :CompositeElementDef, "stupidedi/schema/composite_element_def"
autoload :FunctionalGroupDef, "stupidedi/schema/functional_group_def"
autoload :InterchangeDef, "stupidedi/schema/interchange_def"
autoload :LoopDef, "stupidedi/schema/loop_def"
autoload :SegmentDef, "stupidedi/schema/segment_def"
autoload :SimpleElementDef, "stupidedi/schema/simple_element_def"
autoload :TableDef, "stupidedi/schema/table_def"
autoload :TransactionSetDef, "stupidedi/schema/transaction_set_def"

autoload :ElementReq, "stupidedi/schema/element_req"
autoload :SegmentReq, "stupidedi/schema/segment_req"
autoload :RepeatCount, "stupidedi/schema/repeat_count"
autoload :SyntaxNote, "stupidedi/schema/syntax_note"
autoload :CodeList, "stupidedi/schema/code_list"
autoload :ElementReq, "stupidedi/schema/element_req"
autoload :SegmentReq, "stupidedi/schema/segment_req"
autoload :RepeatCount, "stupidedi/schema/repeat_count"
autoload :SyntaxNote, "stupidedi/schema/syntax_note"
autoload :CodeList, "stupidedi/schema/code_list"
autoload :ExternalCodeListStorage, "stupidedi/schema/external_code_list_storage"
end
end
2 changes: 0 additions & 2 deletions lib/stupidedi/schema/abstract_element_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class AbstractElementDef < AbstractDef

abstract :composite?

# @return [AbstractSet<CodeList>]
abstract :code_lists

# @return [String]
def descriptor
Expand Down
2 changes: 1 addition & 1 deletion lib/stupidedi/schema/abstract_element_use.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class AbstractElementUse < AbstractUse

def_delegators :requirement, :forbidden?, :required?, :optional?

def_delegators :definition, :code_lists
def_delegators :definition

# @return [Symbol]
abstract :id
Expand Down
44 changes: 16 additions & 28 deletions lib/stupidedi/schema/code_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,50 @@ def internal?
end

class Internal < CodeList
def_delegators :@hash, :at, :defined_at?
attr_reader :hash

def_delegators :hash, :at, :defined_at?

def initialize(hash)
@hash = hash
end

# @return [Array<String>]
def codes
@hash.keys
hash.keys
end

def external?
false
end

# Some elements are qualifiers that select which code list
# is applicable to the qualified element. For instance, the
# diagnosis codes in the HI segment are qualified by E1270
# "Code List Qualifier Code", which indicates which code list
# should be used to validate the E1271's diagnosis code. It
# maybe ICD-9, ICD-10, etc.
#
# @return [AbstractSet<CodeList>]
def code_lists(subset = Sets.universal)
related =
@hash.select do |k, v|
subset.include?(k) and v.is_a?(Schema::CodeList)
end

if subset.finite?
Sets.build([self]) + related.map{|k,v| v }
else
Sets.build([self])
end
end
end

class External < CodeList
# @return [String]
attr_reader :id

def_delegators :code_dictionary, :at, :defined_at?

def initialize(id)
@id = id
end

def external?
true
def codes
code_dictionary.keys
end

def code_lists(values = Sets.universal)
Sets.build([self])
def external?
true
end

def to_str
"CodeList.external(#{@id})"
end

private

def code_dictionary
Stupidedi.external_code_lists.fetch(id)
end
end
end

Expand Down
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/composite_element_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,6 @@ def empty(usage, position)
Values::CompositeElementVal.new([], usage)
end

# @return [AbstractSet<CodeList>]
def code_lists
@component_uses.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text("CompositeElementDef[#{@id}]")
Expand Down
34 changes: 34 additions & 0 deletions lib/stupidedi/schema/external_code_list_storage.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true
module Stupidedi
using Refinements

module Schema
class ExternalCodeListStorage
attr_reader :storage

def_delegators :storage, :fetch, :empty?

def initialize(storage)
@storage = storage
end

def register(id, repository)
if storage.has_key?(id)
warn "You are trying to override an existing external code list with id: #{id}. Please make sure each external code list is registered once."
end

storage[id] = repository
end

def fetch(id)
storage.fetch(id, Hash.new)
end

class << self
def empty
self.new(Hash.new)
end
end
end
end
end
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/functional_group_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ def functional_group?
true
end

# @return [AbstractSet<CodeList>
def code_lists
children.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text("FunctionalGroupDef")
Expand Down
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/interchange_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,6 @@ def interchange?
true
end

# @return [AbstractSet<CodeList>]
def code_lists
children.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text("InterchangeDef[#{id}]")
Expand Down
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/loop_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ def loop?
true
end

# @return [AbstractSet<CodeList>]
def code_lists
children.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text("LoopDef[#{@id}]")
Expand Down
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/segment_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ def segment?
true
end

# @return [AbstractSet<CodeList>]
def code_lists
@element_uses.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text "SegmentDef[#{@id}]"
Expand Down
2 changes: 1 addition & 1 deletion lib/stupidedi/schema/segment_use.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class SegmentUse < AbstractUse
# @return [SegmentDef]
attr_reader :definition

def_delegators :definition, :id, :name, :descriptor, :code_lists
def_delegators :definition, :id, :name, :descriptor

# @see X222 B.1.1.3.12.6 Data Segment Requirement Designators
#
Expand Down
8 changes: 0 additions & 8 deletions lib/stupidedi/schema/simple_element_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ def simple_use(requirement, repeat_count, parent = nil)
def component_use(requirement, parent = nil)
ComponentElementUse.new(self, requirement, Sets.universal, parent)
end

# This is overridden (if needed) by the concrete subclasses. Specifically
# the "ID" element types have an extra attribute to link the CodeList.
#
# @return [AbstractSet<CodeList>]
def code_lists(subset = Sets.universal)
Sets.empty
end
end
end
end
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/table_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,6 @@ def table?
true
end

# @return [AbstractSet<CodeList>]
def code_lists
children.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text("TableDef[#{@id}]")
Expand Down
5 changes: 0 additions & 5 deletions lib/stupidedi/schema/transaction_set_def.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ def transaction?
true
end

# @return [AbstractSet<CodeList>]
def code_lists
@table_defs.map(&:code_lists).inject(&:|)
end

# @return [void]
def pretty_print(q)
q.text("TransactionSetDef[#{@functional_group}#{@id}]")
Expand Down
6 changes: 6 additions & 0 deletions lib/stupidedi/versions/common/element_types/id.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ def inspect
else
value = ansi.red(@value)
end
elsif definition.code_list.try(&:external?)
if definition.code_list.defined_at?(@value)
value = "#{@value}: " + ansi.dark(definition.code_list.at(@value))
else
value = ansi.red(@value)
end
else
value = @value
end
Expand Down
Loading