Module: ConceptQL::Metadatable
- Included in:
- Operators::Operator
- Defined in:
- lib/conceptql/behaviors/metadatable.rb
Instance Method Summary collapse
- #allows_many_upstreams ⇒ Object
- #allows_one_upstream ⇒ Object
- #argument(name, options = {}) ⇒ Object
- #auto_label(name, opts = {}) ⇒ Object
- #basic_type(value = nil) ⇒ Object
- #category(category) ⇒ Object
- #derive_metadata_from_validations ⇒ Object
- #desc(value = nil) ⇒ Object
- #domains(*domain_list) ⇒ Object
- #get_desc ⇒ Object
- #humanized_class_name ⇒ Object
- #inherited(upstream) ⇒ Object
- #just_class_name ⇒ Object
- #no_desc ⇒ Object
- #option(name, options = {}) ⇒ Object
- #predominant_domains(*values) ⇒ Object
- #pref_name ⇒ Object
- #preferred_name(value = nil) ⇒ Object
- #reset_categories ⇒ Object
- #standard_description ⇒ Object
- #to_metadata(name, opts = {}) ⇒ Object
- #validate_at_least_one_upstream_to_metadata(*args) ⇒ Object
- #validate_at_most_one_upstream_to_metadata(*args) ⇒ Object
- #validate_no_arguments_to_metadata(*args) ⇒ Object
- #validate_no_upstreams_to_metadata(*args) ⇒ Object
- #validate_one_upstream_to_metadata(*args) ⇒ Object
- #validate_required_options_to_metadata(*args) ⇒ Object
- #warn_about_missing_metadata ⇒ Object
Instance Method Details
#allows_many_upstreams ⇒ Object
55 56 57 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 55 def allows_many_upstreams @max_upstreams = 99 end |
#allows_one_upstream ⇒ Object
59 60 61 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 59 def allows_one_upstream @max_upstreams = 1 end |
#argument(name, options = {}) ⇒ Object
22 23 24 25 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 22 def argument(name, = {}) (@arguments ||= []) @arguments << [name, auto_label(name, )] end |
#auto_label(name, opts = {}) ⇒ Object
32 33 34 35 36 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 32 def auto_label(name, opts = {}) return opts if opts[:label] return opts.merge(label: name.to_s.split('_').join(' ').titlecase) unless opts[:type] == :codelist opts.merge(label: pref_name + " Codes") end |
#basic_type(value = nil) ⇒ Object
50 51 52 53 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 50 def basic_type(value = nil) return @basic_type unless value @basic_type = value end |
#category(category) ⇒ Object
71 72 73 74 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 71 def category(category) (@categories ||= []) @categories << Array(category) end |
#derive_metadata_from_validations ⇒ Object
133 134 135 136 137 138 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 133 def instance_variable_get(:@validations).each do |meth, args| meth = meth.to_s + "_to_metadata" send(meth, args) if respond_to?(meth) end end |
#desc(value = nil) ⇒ Object
12 13 14 15 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 12 def desc(value = nil) return @desc unless value @desc = value end |
#domains(*domain_list) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 38 def domains(*domain_list) @domains = domain_list define_method(:domains) do |db| domain_list end if domain_list.length == 1 define_method(:domain) do domain_list.first end end end |
#get_desc ⇒ Object
121 122 123 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 121 def get_desc @desc ||= standard_description end |
#humanized_class_name ⇒ Object
67 68 69 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 67 def humanized_class_name just_class_name.gsub(/([A-Z])/, ' \1').lstrip end |
#inherited(upstream) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 80 def inherited(upstream) (@options || {}).each do |name, opt| upstream.option name, opt end (@categories || []).each do |cat| upstream.category cat end upstream.basic_type @basic_type case @max_upstreams when 1 upstream.allows_one_upstream when 99 upstream.allows_many_upstreams end end |
#just_class_name ⇒ Object
63 64 65 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 63 def just_class_name self.to_s.split('::').last end |
#no_desc ⇒ Object
178 179 180 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 178 def no_desc desc '' end |
#option(name, options = {}) ⇒ Object
27 28 29 30 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 27 def option(name, = {}) @options ||= {} @options[name] = auto_label(name, ) end |
#predominant_domains(*values) ⇒ Object
17 18 19 20 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 17 def predominant_domains(*values) return @predominant_domains if values.empty? @predominant_domains = values end |
#pref_name ⇒ Object
99 100 101 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 99 def pref_name @preferred_name || humanized_class_name end |
#preferred_name(value = nil) ⇒ Object
7 8 9 10 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 7 def preferred_name(value = nil) return @preferred_name unless value @preferred_name = value end |
#reset_categories ⇒ Object
76 77 78 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 76 def reset_categories @categories = [] end |
#standard_description ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 170 def standard_description table = (!@domains.nil? && @domains.first) table ||= (!predominant_domains.nil? && predominant_domains.first) raise "Can't create description for #{pref_name}" unless table "Selects results from the #{table} table where #{table}'s source value matches the given #{pref_name} codes." end |
#to_metadata(name, opts = {}) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 103 def (name, opts = {}) if opts[:warn] { name: name, preferred_name: pref_name, operation: just_class_name.snakecase, min_upstreams: @max_upstreams || 0, max_upstreams: @max_upstreams || 0, arguments: @arguments || [], options: @options || {}, predominant_domains: @domains || @predominant_domains || [], desc: get_desc, categories: @categories || [], basic_type: @basic_type } end |
#validate_at_least_one_upstream_to_metadata(*args) ⇒ Object
150 151 152 153 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 150 def (*args) @min_upstreams = 1 @max_upstreams = 99 end |
#validate_at_most_one_upstream_to_metadata(*args) ⇒ Object
155 156 157 158 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 155 def (*args) @min_upstreams = 0 @max_upstreams = 1 end |
#validate_no_arguments_to_metadata(*args) ⇒ Object
160 161 162 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 160 def (*args) @arguments = [] end |
#validate_no_upstreams_to_metadata(*args) ⇒ Object
140 141 142 143 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 140 def (*args) @min_upstreams = 0 @max_upstreams = 0 end |
#validate_one_upstream_to_metadata(*args) ⇒ Object
145 146 147 148 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 145 def (*args) @min_upstreams = 1 @max_upstreams = 1 end |
#validate_required_options_to_metadata(*args) ⇒ Object
164 165 166 167 168 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 164 def (*args) args.each do |opt_name| @options[opt_name][:required] = true end end |
#warn_about_missing_metadata ⇒ Object
125 126 127 128 129 130 131 |
# File 'lib/conceptql/behaviors/metadatable.rb', line 125 def missing = [] missing << :categories if (@categories || []).empty? missing << :desc if get_desc.empty? missing << :basic_type unless @basic_type puts "#{just_class_name} is missing #{missing.join(", ")}" unless missing.empty? end |