lib/brief/util.rb in brief-1.16.2 vs lib/brief/util.rb in brief-1.17.0
- old
+ new
@@ -20,16 +20,56 @@
else
[nil, {}.to_mash]
end
end
+ def self.create_singular_method_dispatcher_command_for(action, klass)
+ identifier = "#{ action } #{ klass.type_alias }"
+
+ Object.class.class_eval do
+ command "#{identifier}" do |c|
+ c.syntax = "brief #{identifier} PATH"
+ c.description = "run the #{identifier} command on the model specified at path"
+
+ c.action do |args, _opts|
+ briefcase = $briefcase || Brief.case
+
+ path_args = args.select { |arg| arg.is_a?(String) && arg.match(/\.md$/) }
+
+ path_args.select! do |arg|
+ briefcase.root.join(arg).exist?
+ end
+
+ path_args.map! { |p| briefcase.root.join(p) }
+
+ models = briefcase.documents_at(*path_args).map(&:to_model)
+
+ if models.empty?
+ model_finder = c.name.to_s.split(' ').last
+ models = briefcase.send(model_finder)
+ elsif models.length > 1
+ puts "You passed more than one model. If this is what you want, use the pluralized version of this command to be safe"
+ else
+ model = models.first
+ model && model.send(action)
+ end
+ end
+ end rescue nil
+ end
+ end
+
+ # takes the actions from the models and creates a command
+ # that lets you dispatch the action to a group of models at the
+ # paths / directory / glob you pass
def self.create_method_dispatcher_command_for(action, klass)
+ create_singular_method_dispatcher_command_for(action,klass)
+
identifier = "#{ action } #{ klass.type_alias.to_s.pluralize }"
Object.class.class_eval do
command "#{identifier}" do |c|
- c.syntax = "brief #{identifier}"
- c.description = "run the #{identifier} command"
+ c.syntax = "brief #{identifier} PATHS"
+ c.description = "run the #{identifier} command on the models at PATHS"
c.action do |args, _opts|
briefcase = $briefcase || Brief.case
path_args = args.select { |arg| arg.is_a?(String) && arg.match(/\.md$/) }