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



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/brief/dsl.rb', line 84

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

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

Register a new command for this briefcase.

Pass the name of the command, options, and a block.

The block will get called with the briefcase, and any arguments

currently doesn’t accept any options



27
28
29
30
31
32
# File 'lib/brief/dsl.rb', line 27

def command(name, options={}, &block)
  Brief.commands[name.to_sym] = {
    options: options,
    handler: block
  }
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

defines a new model class



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 53

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

Extends an existing class



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

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

#href_builder(&block) ⇒ Object



16
17
18
# File 'lib/brief/dsl.rb', line 16

def href_builder(&block)
  Brief.href_builder = block
end

#view(name, &block) ⇒ Object

Define a view of the briefcase. Pass a block which returns a hash that displays the desired content



12
13
14
# File 'lib/brief/dsl.rb', line 12

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