class Sutty::Cli::Commands::Field

Constants

INVERSE_REQUIRED
VALUE_REQUIRED

Attributes

name[R]
options[R]

Public Class Methods

fields() click to toggle source
   # File lib/sutty/cli/commands/field.rb
32 def self.fields
33   @@fields ||= Dir.glob(source_dir.to_s + '/*.yml.erb').map do |f|
34     File.basename f, '.yml.erb'
35   end
36 end
inverse_required?() click to toggle source
   # File lib/sutty/cli/commands/field.rb
38 def self.inverse_required?
39   ARGV.any? { |f| INVERSE_REQUIRED.include? f }
40 end
new(name, options) click to toggle source
   # File lib/sutty/cli/commands/field.rb
16 def initialize(name, options)
17   @name = name
18   @options = options
19 end
value_required?() click to toggle source
   # File lib/sutty/cli/commands/field.rb
42 def self.value_required?
43   ARGV.any? { |f| VALUE_REQUIRED.include? f }
44 end

Private Class Methods

source_dir() click to toggle source
   # File lib/sutty/cli/commands/field.rb
48 def self.source_dir
49   @@source_dir ||= Pathname(__dir__).join('..', 'templates', 'field')
50 end

Public Instance Methods

execute(input: $stdin, output: $stdout) click to toggle source
   # File lib/sutty/cli/commands/field.rb
21 def execute(input: $stdin, output: $stdout)
22   unless data_layout_contents.scan(/\n#{name}:\n/).empty?
23     logger.info "The #{name} field is already present, please edit #{data_layout}"
24     return true
25   end
26 
27   TTY::File.safe_append_to_file(data_layout) do
28     ERB.new(template_contents, trim_mode: '<>', eoutvar: '@output_buffer').result(context)
29   end
30 end

Private Instance Methods

context() click to toggle source
   # File lib/sutty/cli/commands/field.rb
56 def context
57   return @context if @context
58   @context = OpenStruct.new(**options.transform_keys(&:to_sym))
59   @context[:name] = name
60 
61   @context = @context.instance_eval('binding')
62 end
data_layout() click to toggle source
   # File lib/sutty/cli/commands/field.rb
72 def data_layout
73   @data_layout ||= File.join('_data', 'layouts', options[:layout] + '.yml')
74 end
data_layout_contents() click to toggle source
   # File lib/sutty/cli/commands/field.rb
76 def data_layout_contents
77   @data_layout_contents ||= File.binread data_layout
78 end
logger() click to toggle source
   # File lib/sutty/cli/commands/field.rb
80 def logger
81   @logger ||= TTY::Logger.new
82 end
source_dir() click to toggle source
   # File lib/sutty/cli/commands/field.rb
52 def source_dir
53   self.class.source_dir
54 end
template() click to toggle source
   # File lib/sutty/cli/commands/field.rb
64 def template
65   @template ||= source_dir.join(options[:type] + '.yml.erb')
66 end
template_contents() click to toggle source
   # File lib/sutty/cli/commands/field.rb
68 def template_contents
69   @template_contents ||= File.binread template
70 end