Class: HammerCLI::Output::Definition
- Inherits:
-
Object
- Object
- HammerCLI::Output::Definition
- Defined in:
- lib/hammer_cli/output/definition.rb
Instance Attribute Summary collapse
-
#fields ⇒ Object
Returns the value of attribute fields.
Instance Method Summary collapse
- #append(fields = nil, &block) ⇒ Object
- #at(path = []) ⇒ Object
- #clear ⇒ Object
- #empty? ⇒ Boolean
- #field_sets ⇒ Object
- #find_field(field_id) ⇒ Object
-
#initialize ⇒ Definition
constructor
A new instance of Definition.
- #insert(mode, field_id, fields = nil, &block) ⇒ Object
- #sets_table ⇒ Object
- #update_field_sets(set_names, field_ids) ⇒ Object
Constructor Details
#initialize ⇒ Definition
Returns a new instance of Definition.
5 6 7 |
# File 'lib/hammer_cli/output/definition.rb', line 5 def initialize @fields = [] end |
Instance Attribute Details
#fields ⇒ Object
Returns the value of attribute fields.
3 4 5 |
# File 'lib/hammer_cli/output/definition.rb', line 3 def fields @fields end |
Instance Method Details
#append(fields = nil, &block) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/hammer_cli/output/definition.rb', line 9 def append(fields = nil, &block) fields = [fields].compact unless fields.is_a?(Array) @fields += fields return @fields unless block_given? dsl = Dsl.new dsl.build(&block) @fields += dsl.fields end |
#at(path = []) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hammer_cli/output/definition.rb', line 36 def at(path = []) path = [path] unless path.is_a? Array return self if path.empty? field = find_field(path[0]) unless field.respond_to?(:output_definition) raise ArgumentError, "Field #{path[0]} doesn't have nested output definition" end field.output_definition.at(path[1..-1]) end |
#clear ⇒ Object
49 50 51 |
# File 'lib/hammer_cli/output/definition.rb', line 49 def clear @fields = [] end |
#empty? ⇒ Boolean
53 54 55 |
# File 'lib/hammer_cli/output/definition.rb', line 53 def empty? @fields.empty? end |
#field_sets ⇒ Object
57 58 59 |
# File 'lib/hammer_cli/output/definition.rb', line 57 def field_sets nested_fields_sets(@fields).uniq.sort end |
#find_field(field_id) ⇒ Object
18 19 20 |
# File 'lib/hammer_cli/output/definition.rb', line 18 def find_field(field_id) @fields[field_index(field_id)] end |
#insert(mode, field_id, fields = nil, &block) ⇒ Object
30 31 32 33 34 |
# File 'lib/hammer_cli/output/definition.rb', line 30 def insert(mode, field_id, fields = nil, &block) definition = self.class.new definition.append(fields, &block) HammerCLI.insert_relative(@fields, mode, field_index(field_id), *definition.fields) end |
#sets_table ⇒ Object
61 62 63 64 65 66 |
# File 'lib/hammer_cli/output/definition.rb', line 61 def sets_table columns = field_sets.unshift(_('Fields')) data = fields_data(@fields, columns).flatten table_gen = HammerCLI::Output::Generators::Table.new(columns, data) table_gen.result end |
#update_field_sets(set_names, field_ids) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/hammer_cli/output/definition.rb', line 22 def update_field_sets(set_names, field_ids) set_names = [set_names] unless set_names.is_a?(Array) field_ids = [field_ids] unless field_ids.is_a?(Array) field_ids.each do |field_id| find_field(field_id).sets = find_field(field_id).sets.concat(set_names).uniq end end |