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.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/oschii/config.rb', line 13 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.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def clock @clock end |
#components ⇒ Object (readonly)
Returns the value of attribute components.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def components @components end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def config @config end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def description @description end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def name @name end |
#scl ⇒ Object (readonly)
Returns the value of attribute scl.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def scl @scl end |
#sda ⇒ Object (readonly)
Returns the value of attribute sda.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def sda @sda end |
#startups ⇒ Object (readonly)
Returns the value of attribute startups.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def startups @startups end |
#templates ⇒ Object (readonly)
Returns the value of attribute templates.
30 31 32 |
# File 'lib/oschii/config.rb', line 30 def templates @templates end |
Class Method Details
Instance Method Details
#add_component(comp) ⇒ Object
43 44 45 |
# File 'lib/oschii/config.rb', line 43 def add_component(comp) components[comp.reference] = comp end |
#build ⇒ Object
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 |
# File 'lib/oschii/config.rb', line 47 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
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/oschii/config.rb', line 88 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/oschii/config.rb', line 107 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(':') 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
167 168 169 |
# File 'lib/oschii/config.rb', line 167 def inspect "#<#{self.class} '#{name}'>" end |
#load_templates ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/oschii/config.rb', line 33 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
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/oschii/config.rb', line 137 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
159 160 161 |
# File 'lib/oschii/config.rb', line 159 def to_json to_h.to_json end |
#to_pretty_json ⇒ Object
163 164 165 |
# File 'lib/oschii/config.rb', line 163 def to_pretty_json JSON.pretty_generate to_h end |
#to_s ⇒ Object
171 172 173 174 175 |
# File 'lib/oschii/config.rb', line 171 def to_s components.collect do |_ref, comp| "#{comp.type}:#{comp.name} => #{comp.config}" end.join "\n" end |