Class: MakeMenu::FieldSet
- Inherits:
-
Object
- Object
- MakeMenu::FieldSet
- Defined in:
- lib/make_menu/field_set.rb
Instance Method Summary collapse
- #add(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block) ⇒ Object
- #build ⇒ Object
- #display ⇒ Object
-
#initialize ⇒ FieldSet
constructor
A new instance of FieldSet.
Constructor Details
#initialize ⇒ FieldSet
Returns a new instance of FieldSet.
3 4 5 6 |
# File 'lib/make_menu/field_set.rb', line 3 def initialize @fields = [] @max_widths = {} end |
Instance Method Details
#add(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/make_menu/field_set.rb', line 43 def add(label = '', value_from_file: nil, color: :normal, none: '[none]'.dark, &block) if value_from_file block = lambda do if File.exists? value_from_file return File.read(value_from_file).strip.color(color) else return none end end end @fields << { label: label, handler: block } end |
#build ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/make_menu/field_set.rb', line 29 def build @max_widths[:label] = 0 @max_widths[:value] = 0 @fields.each do |field| label = field[:label] value = field[:value] = field[:handler].call label_width = label.decolor.size value_width = value.decolor.size @max_widths[:label] = label_width if label_width > @max_widths[:label] @max_widths[:value] = value_width if value_width > @max_widths[:value] end end |
#display ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/make_menu/field_set.rb', line 8 def display build @fields.each do |field| label_cell = field[:label] .align( :right, width: @max_widths[:label] ) value_cel = field[:value] .align( :left, width: @max_widths[:value], pad_right: true ) puts "#{label_cell}#{value_cel}".align(:center) end puts end |