Module: Command::OgCommands

Defined in:
lib/command-set/og.rb

Defined Under Namespace

Classes: DisplayCommand, HasManyEditCommand, HasManyListCommand, HasOneCommand, OgModeCommand, PropertyCommand

Class Method Summary collapse

Class Method Details

.command_set(config) ⇒ Object



361
362
363
364
365
366
367
368
369
370
# File 'lib/command-set/og.rb', line 361

def command_set(config)
  set = CommandSet.new("og_commands")
  def set.entity_modes
    return @entity_modes||={}
  end
  normalize_config(config)
  entity_commands_from_config(set, config)
  list_command_from_config(set, config)
  return set
end

.entity_commands_from_config(set, config) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/command-set/og.rb', line 475

def entity_commands_from_config(set, config)
  config.each do |entity_config|
    next if entity_config["edit_command"].nil?

    my_mode = entity_mode(entity_config["edit_config"])
    set.entity_modes[entity_config["class"]]=my_mode

    set.command(OgModeCommand, entity_config["edit_command"]) do
      switch_to entity_config["real_class"], entity_config["select_by"]
      mode my_mode
    end
  end
end

.entity_mode(fields_config) ⇒ Object



531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'lib/command-set/og.rb', line 531

def entity_mode(fields_config)
  mode = CommandSet.define_commands do
    include_commands StandardCommands::Quit

    command "exit" do
      subject_methods :interpreter, :current_state

      doesnt_undo
      action do
        entity = subject.current_state.pop
        entity.save
        subject.interpreter.pop_mode
      end
    end

    command "delete!" do
      subject_methods :interpreter, :current_state

      action do
        entity = subject.current_state.pop
        entity.delete
        subject.interpreter.pop_mode
      end
    end

    command DisplayCommand, "display" do
      simple_fields fields_config["simple_fields"].map{|f| [f["name"], f["field_name"]]}
      singles = fields_config["single_relations"].map do |f|
        [ f["name"], f["field_name"], f["target"]["select_by"] ]
      end
      single_relations singles
      many_relations fields_config["many_relations"].map{|f| [f["name"], f["field_name"]]}
    end

    fields_config["simple_fields"].each do |property|
      command PropertyCommand, property["name"] do
        field_name property["field_name"]
        if property["edit?"]
          editable
        end
      end
    end

    fields_config["single_relations"].each do |relation|
      command HasOneCommand, relation["name"] do
        optional_value relation["target"]["real_class"], relation["target"]["select_by"]
        has_one relation["field_name"]
        identified_by relation["target"]["select_by"]
      end
    end

    fields_config["many_relations"].each do |relation_config|
      unless relation_config["list?"] or relation_config["edit?"]
        next
      end
      sub_command relation_config["name"] do
        if relation_config["list?"]
          command HasManyListCommand, :list do
            has_many relation_config["field_name"]
            listed_as relation_config["target"]["select_by"]
          end
        end

        if relation_config["edit?"]
          command HasManyEditCommand, :add do
            find_a relation_config["target"]["real_class"], 
              relation_config["target"]["select_by"]
            add_to relation_config["field_name"]
          end

          command HasManyEditCommand, :remove do
            find_a relation_config["target"]["real_class"],
              relation_config["target"]["select_by"]
            remove_from relation_config["field_name"]
          end
        end
      end
    end
  end
  return mode
end

.list_command_from_config(set, config) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
# File 'lib/command-set/og.rb', line 489

def list_command_from_config(set, config)
  lists = {}
  config.find_all{|item| item["listable?"] }.each do |item|
    entry = item["list_as"]	
    entry_class = item["real_class"]
    find_by = item["select_by"]

    lists[entry] = [entry_class, find_by]
  end

  set.command "list" do
    argument :what, lists.keys

    optional_argument :search, "A substring of the name of the thing you're looking for"

    doesnt_undo

    define_method(:make_list) do |klass, list_attribute, search_term|
      objects = [] 
      if search_term.nil?
        objects = klass.find
      else
        objects = klass.find(:condition => ["#{list_attribute} like ?",
                                            "%#{search_term}%"])
      end

      objects.map do |object|
        object.__send__(list_attribute)
      end.each do |item|
        puts item
      end
    end

    action do
      list_me = lists[what]
      raise CommandException if list_me.nil?
      make_list(*(list_me + [search]))
    end
  end
end

.normalize_config(config) ⇒ Object

This is a messy method that ensures that the configuration hash for the CommandSet is well constructed. It should catch errors and do coversions, set defaults, etc. to set up the creation of the CommandSet. It’s source may be instructive for creating or editing config hashes.



376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/command-set/og.rb', line 376

def normalize_config(config)
  entity_count = 1
  required_names = ["class"]
  config.each do |entity_config|
    required_names.each do |name|
      if entity_config[name].nil?
        raise RuntimeError, "Entity #{entity_config.pretty_inspect} missing #{name}!"
      end
    end

    begin
      real_class = constant(entity_config["class"])
      entity_config["real_class"] = real_class
    rescue NameError
      raise RuntimeError, "Class: #{entity_config["class"]} unrecognized.  Missing require?"
    end

    entity_config["select_by"] = "primary_key" if entity_config["select_by"].nil?

    unless entity_config["edit_command"].nil?
      if entity_config["edit_config"].nil? 
        raise RuntimeError, "Class: #{item["class"]} has an edit_command but no edit_config!"
      end
    end

    if entity_config["listable?"]
      if entity_config["list_as"].nil?
        if not entity_config["plural_name"].nil?
          entity_config["list_as"] = entity_config["plural_name"]
        elsif not entity_config["edit_command"].nil?
          entity_config["list_as"] = entity_config["edit_command"] + "s"
        else
          raise RuntimeError, "Class: #{entity_config["class"]} marked listable, " +
            "but without a list name!"
        end
      end
    end
    entity_count += 1
  end

  #fields pass - so that relations can reuse entity defs
  config.each do |entity_config|
    fields_config = entity_config["edit_config"]
    next if fields_config.nil?

    fields_config["simple_fields"]||=[]
    fields_config["simple_fields"].each do |field_config|
      if field_config["field_name"].nil?
        raise RuntimeError, "Class: #{entity_config["class"]} field missing field_name!"
      end

      field_config["name"]||=field_config["field_name"]
    end

    fields_config["single_relations"]||=[]
    fields_config["single_relations"].each do |relation_config|
      if relation_config["field_name"].nil?
        raise RuntimeError, "Class: #{entity_config["class"]} relation " +
                "missing field_name!"
      end

      relation_config["name"]||=relation_config["field_name"]
      if relation_config["target"].nil?
        raise RuntimeError, "Class: #{entity_config["class"]} relation " +
                  "#{relation_config["name"]} has no target!"
      end

      relation_config["target"]["select_by"]||="name"
    end

    fields_config["many_relations"]||=[]
    fields_config["many_relations"].each do |relation_config|
      if relation_config["field_name"].nil?
        raise RuntimeError, "Class: #{entity_config["class"]} field missing field_name!"
      end

      relation_config["name"]||=relation_config["field_name"]

      if relation_config["target"].nil?
        raise RuntimeError, "Class: #{entity_config["class"]} relation " +
                    "#{relation_config["name"]} has no target!"
      end

      relation_config["target"]["select_by"]||="name"
      unless Class === relation_config["target"]["real_class"]
        if relation_config["target"]["class"].nil?
          raise RuntimeError,
                        "Class: #{entity_config["class"]} relation " + 
                        "#{relation_config["name"]} target has no class!"
        else
          relation_config["target"]["real_class"] = 
            constant(relation_config["target"]["real_class"])
        end
      end
    end
  end
end