Class: Oschii::Config
- Inherits:
-
Object
- Object
- Oschii::Config
- Defined in:
- lib/oschii/config.rb
Instance Attribute Summary collapse
-
#clock ⇒ Object
readonly
Returns the value of attribute clock.
-
#components ⇒ Object
readonly
Returns the value of attribute components.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#scl ⇒ Object
readonly
Returns the value of attribute scl.
-
#sda ⇒ Object
readonly
Returns the value of attribute sda.
-
#startups ⇒ Object
readonly
Returns the value of attribute startups.
-
#templates ⇒ Object
readonly
Returns the value of attribute templates.
Class Method Summary collapse
Instance Method Summary collapse
- #add_component(comp) ⇒ Object
- #build ⇒ Object
- #build_custom_component(custom_def, name_prefix = '', indent = 0) ⇒ Object
- #build_missing_targets ⇒ Object
-
#initialize(json) ⇒ Config
constructor
A new instance of Config.
- #inspect ⇒ Object
- #load_templates ⇒ Object
- #to_h ⇒ Object
- #to_json ⇒ Object
- #to_pretty_json ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(json) ⇒ Config
Returns a new instance of Config.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/oschii/config.rb', line 18 def initialize(json) @config = json.is_a?(String) ? JSON.parse(json) : json @components = {} @startups = [] @templates = load_templates @sda = -1 @scl = -1 @clock = 100_000 @name = nil @description = nil build end |
Instance Attribute Details
#clock ⇒ Object (readonly)
Returns the value of attribute clock.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def clock @clock end |
#components ⇒ Object (readonly)
Returns the value of attribute components.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def components @components end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def config @config end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def name @name end |
#scl ⇒ Object (readonly)
Returns the value of attribute scl.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def scl @scl end |
#sda ⇒ Object (readonly)
Returns the value of attribute sda.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def sda @sda end |
#startups ⇒ Object (readonly)
Returns the value of attribute startups.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def startups @startups end |
#templates ⇒ Object (readonly)
Returns the value of attribute templates.
35 36 37 |
# File 'lib/oschii/config.rb', line 35 def templates @templates end |
Class Method Details
.from_file(filename) ⇒ Object
31 32 33 |
# File 'lib/oschii/config.rb', line 31 def self.from_file(filename) Config.new(File.read(filename)) end |
Instance Method Details
#add_component(comp) ⇒ Object
48 49 50 |
# File 'lib/oschii/config.rb', line 48 def add_component(comp) components[comp.reference] = comp end |
#build ⇒ Object
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 |
# File 'lib/oschii/config.rb', line 52 def build @name = config['name'] @description = config['description'] @startups = config['Startups'] || [] puts inspect BASIC_COMPONENT_TYPES.each do |type| config["#{type}s"]&.each do |comp_def| comp = Component.new(type, comp_def) puts " + #{comp}" add_component comp end end if (i2c = config[I2C]) @sda = i2c['sdaPin'] || sda @scl = i2c['sclPin'] || scl @clock = i2c['clock'] || clock i2c['modules']&.each do |comp_def| comp = Component.new(I2C, comp_def) puts " + #{comp}" add_component comp end end # Get templates defined within this config config['Templates']&.each do |conf| templates[conf['name']] = Template.new(conf) end config['Customs']&.each do |custom_def| build_custom_component custom_def end build_missing_targets self end |
#build_custom_component(custom_def, name_prefix = '', indent = 0) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/oschii/config.rb', line 93 def build_custom_component(custom_def, name_prefix = '', indent = 0) template = templates[custom_def['template']] raise TemplateError, "Custom:#{custom_def['name']} - Template '#{custom_def['template']}' not defined" unless template puts "#{' ' * indent} Custom:#{template.name}:#{name_prefix}#{custom_def['name']}" template.build_components(custom_def, name_prefix).each do |comp| puts "#{' ' * (indent+1)} + #{comp}" add_component comp end @startups += template.startups template.&.each do |inner_custom_def| build_custom_component inner_custom_def, "#{custom_def['name']}_", indent + 1 end end |
#build_missing_targets ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/oschii/config.rb', line 112 def build_missing_targets missing_comps = [] components.each do |ref, comp| comp.targets.each do |target_ref| next unless components[target_ref].nil? type, name = target_ref.split(':') puts name name = name.split('/').first unless name.start_with?('*') new_comp = Component.new(type, { 'name' => name, 'warning' => 'Auto-created by Oschii Parser' }) missing_comps << new_comp end end end unless missing_comps.empty? puts puts "WARNING: Creating empty components for unresolved target references." puts " Output configuration may not be valid!" puts missing_comps.uniq do |comp| next if components[comp.reference] puts " + #{comp}" add_component comp end end end |
#inspect ⇒ Object
173 174 175 |
# File 'lib/oschii/config.rb', line 173 def inspect "#<#{self.class} '#{name}'>" end |
#load_templates ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/oschii/config.rb', line 38 def load_templates Dir.glob("./templates/*.json").map do |filename| conf = JSON.parse(File.read(filename)) template = Template.new( conf, conf['name'] || filename.gsub('.json', '').split('/').last ) [template.name, template] end.to_h end |
#to_h ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/oschii/config.rb', line 143 def to_h conf = {} conf[:name] = @name if @name conf[:description] = @description if @description conf[:Startup] = startups components.each do |_ref, comp| if comp.type == I2C conf[I2C] ||= { sdaPin: sda, sclPin: scl, clock: clock, modules: [] } conf[I2C][:modules] << comp.config else array = (conf[comp.array_name] ||= []) array << comp.config end end conf end |
#to_json ⇒ Object
165 166 167 |
# File 'lib/oschii/config.rb', line 165 def to_json to_h.to_json end |
#to_pretty_json ⇒ Object
169 170 171 |
# File 'lib/oschii/config.rb', line 169 def to_pretty_json JSON.pretty_generate to_h end |
#to_s ⇒ Object
177 178 179 180 181 |
# File 'lib/oschii/config.rb', line 177 def to_s components.collect do |_ref, comp| "#{comp.type}:#{comp.name} => #{comp.config}" end.join "\n" end |