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



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/brief/dsl.rb', line 61

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/brief/dsl.rb', line 30

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

  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 if klass.nil?

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

#extend(*args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/brief/dsl.rb', line 13

def extend(*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
    return define(*args, &block)
  end

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

#view(name, &block) ⇒ Object



9
10
11
# File 'lib/brief/dsl.rb', line 9

def view(name, &block)
  Brief.views[name.to_sym] = block
end