Method: Brief::DSL#action

Defined in:
lib/brief/dsl.rb

#action(identifier, _options = {}, &block) ⇒ Object

defines a method on the model instance named after the identifier and then creates a CLI command matching that, so for example:

given a model called ‘Post’ and an action named ‘publish’ the brief CLI executable will respond to:

brief publish posts PATH_GLOB

this will find all of the Post models from the documents matching PATH_GLOB and call the publish method on them



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
# File 'lib/brief/dsl.rb', line 48

def action(identifier, _options = {}, &block)
  Object.class.class_eval do
    command "#{identifier}" do |c|
      c.syntax = "brief #{identifier}"
      c.description = "run the #{identifier} command"

      c.action do |args, opts|
        briefcase = Brief.case

        path_args = args.select { |arg| arg.is_a?(String) && arg.match(/\.md$/) }

        path_args.select! do |arg|
          path = briefcase.repository.root.join(arg)
          path.exist?
        end

        path_args.map! { |p| briefcase.repository.root.join(p) }

        models = path_args.map { |path| Brief::Document.new(path) }.map(&:to_model)

        block.call(Brief.case, models, opts)
      end
    end rescue nil
  end
end