Sha256: 3e5280022bd4e25bac3271b6b37475ed128cd2176231e6a040803bea0d8cc6da

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module Brief
  module DSL
    extend ActiveSupport::Concern

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

    def define(*args, &block)
      definition = Brief::Model::Definition.new(args.first, args.extract_options!)
      definition.instance_eval(&block) if block_given?
      definition.validate!
    end

    # 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
    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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
brief-1.2.0 lib/brief/dsl.rb