Class: Terraspace::Seeder::Content
- Inherits:
-
Object
- Object
- Terraspace::Seeder::Content
- Extended by:
- Memoist
- Defined in:
- lib/terraspace/seeder/content.rb
Instance Method Summary collapse
- #all_vars ⇒ Object
- #build ⇒ Object
- #build_line(name, meta) ⇒ Object
- #desc_example(desc) ⇒ Object
- #escape(type, value) ⇒ Object
-
#initialize(parsed) ⇒ Content
constructor
A new instance of Content.
- #optional_vars ⇒ Object
- #required_vars ⇒ Object
- #rpad ⇒ Object
- #select_vars(parsed) ⇒ Object
- #var_value(meta) ⇒ Object
Constructor Details
#initialize(parsed) ⇒ Content
Returns a new instance of Content.
5 6 7 |
# File 'lib/terraspace/seeder/content.rb', line 5 def initialize(parsed) @parsed = parsed end |
Instance Method Details
#all_vars ⇒ Object
73 74 75 |
# File 'lib/terraspace/seeder/content.rb', line 73 def all_vars select_vars(@parsed) end |
#build ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/terraspace/seeder/content.rb', line 9 def build lines = [] lines << "# Required variables:" unless required_vars.empty? required_vars.each do |name, | lines << build_line(name, ) end lines << "" unless required_vars.empty? lines << "# Optional variables:" unless optional_vars.empty? optional_vars.each do |name, | lines << build_line(name, ) end lines.join("\n") + "\n" end |
#build_line(name, meta) ⇒ Object
23 24 25 26 27 28 |
# File 'lib/terraspace/seeder/content.rb', line 23 def build_line(name, ) value = var_value() name = "# #{name}" if ["default"] # optional so add as a comment name = "%-#{rpad}s" % name # rpad to align = signs "#{name} = #{value}" end |
#desc_example(desc) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/terraspace/seeder/content.rb', line 58 def desc_example(desc) return unless desc md = desc.match(/([eE]xample|IE): (.*)/) return unless md md[2] end |
#escape(type, value) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/terraspace/seeder/content.rb', line 41 def escape(type, value) if type == "map(string)" "{...} # #{type}" elsif type&.include?('(') # complex type "[...] # #{type}" elsif %w[null any true false].include?(value) value # no quotes else %Q|"#{value}"| # add quotes end end |
#optional_vars ⇒ Object
69 70 71 |
# File 'lib/terraspace/seeder/content.rb', line 69 def optional_vars select_vars(@parsed) { || !["default"].nil? } end |
#required_vars ⇒ Object
65 66 67 |
# File 'lib/terraspace/seeder/content.rb', line 65 def required_vars select_vars(@parsed) { || ["default"].nil? } end |
#rpad ⇒ Object
53 54 55 |
# File 'lib/terraspace/seeder/content.rb', line 53 def rpad all_vars.keys.map(&:size).max + 2 end |
#select_vars(parsed) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/terraspace/seeder/content.rb', line 77 def select_vars(parsed) vars = parsed.dig("variable") return [] unless vars vars.select do |name,| block_given? ? yield() : true end end |
#var_value(meta) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/terraspace/seeder/content.rb', line 30 def var_value() type, default = ["type"], ["default"] if default escape(type, default) else value = desc_example(["description"]) || type || "any" escape(type, value) end end |