Module: Brief::DSL

Extended by:
ActiveSupport::Concern
Included in:
Briefcase
Defined in:
lib/brief/dsl.rb

Instance Method Summary collapse

Instance Method Details

#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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/brief/dsl.rb', line 44

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

#config(options = {}, &block) ⇒ Object



5
6
7
# File 'lib/brief/dsl.rb', line 5

def config(options = {}, &block)
  Brief::Configuration.instance.instance_eval(&block) if block_given?
end

#define(*args, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/brief/dsl.rb', line 9

def define(*args, &block)
  options     = args.dup.extract_options!
  name        = args.first
  type_alias  = options.fetch(:type_alias) { name.downcase.parameterize.gsub(/-/, '_') }

  namespace = Brief.configuration.model_namespace || Brief::Model

  klass = namespace.const_get(type_alias.camelize) rescue nil

  if klass
    # raise class already defined
  end

  klass ||= namespace.const_set(type_alias.camelize, Class.new).tap do |k|
    k.send(:include, Brief::Model)
    k.definition ||= Brief::Model::Definition.new(args.first, args.extract_options!)
    k.name ||= name
    k.type_alias ||= type_alias
    Brief::Model.classes << k
  end

  klass.definition.instance_eval(&block) if block_given?
  klass.definition.validate!
end