Class: Circuitdata::Tools
- Inherits:
-
Object
- Object
- Circuitdata::Tools
- Defined in:
- lib/circuitdata/tools.rb
Instance Method Summary collapse
- #create_documentation(ra) ⇒ Object
- #create_structure ⇒ Object
-
#initialize ⇒ Tools
constructor
A new instance of Tools.
- #update_ra(type, key, value) ⇒ Object
Constructor Details
#initialize ⇒ Tools
Returns a new instance of Tools.
2 3 4 5 6 7 |
# File 'lib/circuitdata/tools.rb', line 2 def initialize() require 'json' @schema_path = File.join(File.dirname(__FILE__), 'schema_files/v1/ottp_circuitdata_schema.json') @definitions_path = File.join(File.dirname(__FILE__), 'schema_files/v1/ottp_circuitdata_schema_definitions.json') @ra = {} end |
Instance Method Details
#create_documentation(ra) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/circuitdata/tools.rb', line 128 def create_documentation(ra) ra[:documentation] = "## Elements and tags\n====================\n\n" ra[:structured][:elements].each do |element_key, element_value| ra[:documentation] += "### #{element_key} [link](##{element_key.to_s.downcase.tr(" ", "-")})\n" element_value[:elements].each do |e_key, e_value| ra[:documentation] += "* #{e_key}\n" end ra[:documentation] += "\n" end ra[:structured][:elements].each do |element_key, element_value| ra[:documentation] += "### #{element_key}\n" ra[:documentation] += "Name: #{element_value[:descriptive_name]}\n\n" unless element_value[:descriptive_name].nil? ra[:documentation] += "Aliases: #{element_value[:aliases]}\n\n" unless element_value[:aliases].nil? ra[:documentation] += "#{element_value[:description]}\n" unless element_value[:description].nil? ra[:documentation] += "\n" if element_value[:type] == "array" ra[:documentation] += "**You must specify this as en array when used in a generic product description or a stackup, but as a single object when used in any of the other parts.**\n\n" end element_value[:elements].each do |e_key, e_value| ra[:documentation] += "#### #{e_key}\n" ra[:documentation] += "Aliases: #{e_value[:aliases]}\n\n" unless e_value[:aliases].nil? ra[:documentation] += "#{e_value[:description]}\n\n" unless e_value[:description].nil? ra[:documentation] += "Unit of Measure: #{e_value[:uom][0]}\n\n" unless e_value[:uom].nil? if e_value.has_key? :enum and not e_value[:enum].nil? ra[:documentation] += "Use one of these values:\n" e_value[:enum].each do |ev| ra[:documentation] += "* #{ev}\n" end ra[:documentation] += "\n" end ra[:documentation] += "| | Generic product | Stackup | Profile defaults | Profile enforced | Profile restricted | Capabilities |\n" ra[:documentation] += "|-:|:---------------:|:-------:|:----------------:|:----------------:|:------------------:|:------------:|\n| **Use in:** | " [e_value[:in_product_generic], e_value[:in_product_stackup], e_value[:in_profile_default], e_value[:in_profile_enforced], e_value[:in_profile_restricted], e_value[:in_capabilities]].each do |part| part.nil? ? ra[:documentation] += "Disallowed | " : ra[:documentation] += "Allowed | " end ra[:documentation] += "\n|**Format:** | #{e_value[:type]} | #{e_value[:type]} | #{e_value[:type]} | #{e_value[:type]} | Array of #{e_value[:type]}s | Array of #{e_value[:type]}s |\n" if e_value[:enum].nil? and e_value[:type] == "number" ra[:documentation] += "|**Min value:** | #{e_value[:minimum]} | #{e_value[:minimum]} | #{e_value[:minimum]} | #{e_value[:minimum]} | Each item: #{e_value[:minimum]} | Each item: #{e_value[:minimum]} |\n" unless e_value[:minimum].nil? ra[:documentation] += "|**Max value:** | #{e_value[:maximum]} | #{e_value[:maximum]} | #{e_value[:maximum]} | #{e_value[:maximum]} | Each item : #{e_value[:maximum]} | Each item: #{e_value[:maximum]} |\n" unless e_value[:maximum].nil? end ra[:documentation] += "\n" end end return ra[:documentation] end |
#create_structure ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/circuitdata/tools.rb', line 99 def create_structure @ra[:structured] = {:elements => {}, :custom => {}} @ra[:errors] = {:in_profile_restricted => {}, :in_capabilities => {}} parsed_schema = Circuitdata.read_json(@schema_path)[2] parsed_schema[:properties][:open_trade_transfer_package][:properties][:products][:patternProperties]["^(?!generic$).*".to_sym][:properties][:printed_circuits_fabrication_data][:properties].each do |key, value| self.update_ra(:in_product_generic, key, value) end ["defaults", "restricted", "enforced"].each do |sym| parsed_schema[:properties][:open_trade_transfer_package][:properties][:profiles][:properties][sym.to_sym][:properties][:printed_circuits_fabrication_data][:properties].each do |key, value| case sym when "defaults" t = :in_profile_default when "restricted" t = :in_profile_restricted when "enforced" t = :in_profile_enforced end self.update_ra(t, key, value) end end parsed_schema[:properties][:open_trade_transfer_package][:properties][:capabilities][:properties][:printed_circuits_fabrication_data][:properties].each do |key, value| self.update_ra(:in_capabilities, key, value) end @ra[:structured][:elements].sort.to_h @ra[:structured][:elements].delete(:version) return @ra end |
#update_ra(type, key, value) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/circuitdata/tools.rb', line 9 def update_ra(type, key, value) parsed_elements = Circuitdata.read_json(@definitions_path)[2] unless @ra[:structured][:elements].has_key? key @ra[:structured][:elements][key] = { :type => value[:type], :elements => {}, :name => nil, :description => nil, :aliases => nil } @ra[:structured][:elements][key][:name] = value[:descriptive_name] if value.has_key? :descriptive_name @ra[:structured][:elements][key][:description] = value[:description] if value.has_key? :description if value.has_key? :aliases @ra[:structured][:elements][key][:aliases] = value[:aliases] unless value[:aliases] == "" end end if value[:type] == "array" subelement = value[:items] else subelement = value end if subelement.has_key? :properties subelement[:properties].each do |skey, svalue| unless @ra[:structured][:elements][key][:elements].has_key? skey @ra[:structured][:elements][key][:elements][skey] = { :in_product_generic => false, :in_product_stackup => false, :in_profile_default => false, :in_profile_enforced => false, :in_profile_restricted => false, :in_capabilities => false, :type => nil, :arrayitems => nil, :enum => nil, :description => nil, :uom => nil, :minimum => nil, :maximum => nil } if svalue.has_key? :$ref elements = svalue[:$ref].split('/') if elements.length < 5 element = parsed_elements[elements[1].to_sym][elements[2].to_sym][elements[3].to_sym] else element = parsed_elements[elements[1].to_sym][elements[2].to_sym][elements[3].to_sym][elements[4].to_sym] end else element = nil [:rigid_conductive_layer, :flexible_conductive_layer].include? key.to_sym ? newkey = :conductive_layer : newkey = key.to_sym if parsed_elements[:definitions][:elements].has_key? newkey element = parsed_elements[:definitions][:elements][newkey][skey.to_sym] if parsed_elements[:definitions][:elements][newkey].has_key? skey.to_sym end end unless element.nil? if element.has_key? :type @ra[:structured][:elements][key][:elements][skey][:type] = element[:type] if element[:type] == "array" if element.has_key? :items and element[:items].has_key? :type @ra[:structured][:elements][key][:elements][skey][:arrayitems] == element[:items][:type] end end end @ra[:structured][:elements][key][:elements][skey][:enum] = element[:enum] if element.has_key? :enum @ra[:structured][:elements][key][:elements][skey][:description] = element[:description] if element.has_key? :description @ra[:structured][:elements][key][:elements][skey][:uom] = element[:uom] if element.has_key? :uom @ra[:structured][:elements][key][:elements][skey][:minimum] = element[:minimum] if element.has_key? :minimum @ra[:structured][:elements][key][:elements][skey][:maximum] = element[:maximum] if element.has_key? :maximum end else if [:in_profile_restricted, :in_capabilities].include? type case @ra[:structured][:elements][key][:elements][skey][:type] when *["number", "integer", "boolean", "string"] @ra[:structured][:elements][key][:elements][skey][:type] = "number" if @ra[:structured][:elements][key][:elements][skey][:type] == "integer" unless ( svalue.has_key? :type and svalue[:type] == "array" ) and ( svalue.has_key? :items and svalue[:items].has_key? :type and svalue[:items][:type] == @ra[:structured][:elements][key][:elements][skey][:type]) (@ra[:errors][type][key] ||= {})[skey] = "Type is #{@ra[:structured][:elements][key][:elements][skey][:type]}, wrong check" end when "array" unless svalue.has_key? :type and svalue[:type] == "array" (@ra[:errors][type][key] ||= {})[skey] = "Type is #{@ra[:structured][:elements][key][:elements][skey][:type]}, wrong check" end else puts "unknown type #{@ra[:structured][:elements][key][:elements][skey][:type]} in #{key}, #{skey} when doing #{type}" end end end @ra[:structured][:elements][key][:elements][skey][type] = true end end end |