Class: Benry::CmdApp::Registry
- Inherits:
-
Object
- Object
- Benry::CmdApp::Registry
- Defined in:
- lib/benry/cmdapp.rb
Instance Method Summary collapse
- #abbrev_add(abbrev, prefix) ⇒ Object
- #abbrev_each ⇒ Object
- #abbrev_exist?(abbrev) ⇒ Boolean
- #abbrev_get_prefix(abbrev) ⇒ Object
- #abbrev_resolve(action) ⇒ Object
- #category_add(prefix, desc = nil) ⇒ Object
- #category_add_via_action(action) ⇒ Object
- #category_count_actions(depth, all: false) ⇒ Object
- #category_each(&block) ⇒ Object
- #category_exist?(prefix) ⇒ Boolean
- #category_get_desc(prefix) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #metadata_action2aliases(all: true) ⇒ Object
- #metadata_add(metadata) ⇒ Object
- #metadata_del(name) ⇒ Object
- #metadata_each(all: true, &b) ⇒ Object
- #metadata_exist?(name) ⇒ Boolean
- #metadata_get(name) ⇒ Object
- #metadata_lookup(name) ⇒ Object
Constructor Details
#initialize ⇒ Registry
Returns a new instance of Registry.
830 831 832 833 834 |
# File 'lib/benry/cmdapp.rb', line 830 def initialize() @metadata_dict = {} # {name => (ActionMetadata|AliasMetadata)} @category_dict = {} # {prefix => description} @abbrev_dict = {} end |
Instance Method Details
#abbrev_add(abbrev, prefix) ⇒ Object
958 959 960 961 962 |
# File 'lib/benry/cmdapp.rb', line 958 def abbrev_add(abbrev, prefix) #; [!n475k] registers abbrev with prefix. @abbrev_dict[abbrev] = prefix nil end |
#abbrev_each ⇒ Object
974 975 976 977 978 979 980 981 |
# File 'lib/benry/cmdapp.rb', line 974 def abbrev_each() #; [!2oo4o] yields each abbrev name and prefix. @abbrev_dict.keys.sort.each do |abbrev| prefix = @abbrev_dict[abbrev] yield abbrev, prefix end nil end |
#abbrev_exist?(abbrev) ⇒ Boolean
969 970 971 972 |
# File 'lib/benry/cmdapp.rb', line 969 def abbrev_exist?(abbrev) #; [!tjbdy] returns true/false if abbrev registered or not. return @abbrev_dict.key?(abbrev) end |
#abbrev_get_prefix(abbrev) ⇒ Object
964 965 966 967 |
# File 'lib/benry/cmdapp.rb', line 964 def abbrev_get_prefix(abbrev) #; [!h1dvb] returns prefix bound to abbrev. return @abbrev_dict[abbrev] end |
#abbrev_resolve(action) ⇒ Object
983 984 985 986 987 988 989 990 991 992 |
# File 'lib/benry/cmdapp.rb', line 983 def abbrev_resolve(action) #; [!n7zsy] replaces abbrev in action name with prefix. if action =~ /\A[-\w]+:/ abbrev = $&; rest = $' prefix = @abbrev_dict[abbrev] return prefix + rest if prefix end #; [!kdi3o] returns nil if abbrev not found in action name. return nil end |
#category_add(prefix, desc = nil) ⇒ Object
897 898 899 900 901 902 903 904 |
# File 'lib/benry/cmdapp.rb', line 897 def category_add(prefix, desc=nil) #; [!k27in] registers prefix if not registered yet. #; [!xubc8] registers prefix whenever desc is not a nil. if ! @category_dict.key?(prefix) || desc @category_dict[prefix] = desc end nil end |
#category_add_via_action(action) ⇒ Object
906 907 908 909 910 911 912 913 914 915 |
# File 'lib/benry/cmdapp.rb', line 906 def category_add_via_action(action) #; [!ztrfj] registers prefix of action. #; [!31pik] do nothing if prefix already registered. #; [!oqq7j] do nothing if action has no prefix. if action =~ /\A(?:[-\w]+:)+/ prefix = $& @category_dict[prefix] = nil unless @category_dict.key?(prefix) end nil end |
#category_count_actions(depth, all: false) ⇒ Object
937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 |
# File 'lib/benry/cmdapp.rb', line 937 def category_count_actions(depth, all: false) dict = {} #; [!8wipx] includes prefix of hidden actions if `all: true` passed. (all: all) do || name = .name next unless name =~ /:/ #; [!5n3qj] counts prefix of specified depth. arr = name.split(':') # ex: "a:b:c:xx" -> ["a", "b", "c", "xx"] arr.pop() # ex: ["a", "b", "c", "xx"] -> ["a", "b", "c"] arr = arr.take(depth) if depth > 0 # ex: ["a", "b", "c"] -> ["a", "b"] (if depth==2) prefix = arr.join(':') + ':' # ex: ["a", "b"] -> "aa:bb:" dict[prefix] = (dict[prefix] || 0) + 1 # ex: dict["aa:bb:"] = (dict["aa:bb:"] || 0) + 1 #; [!r2frb] counts prefix of lesser depth. while (arr.pop(); ! arr.empty?) # ex: ["a", "b"] -> ["a"] prefix = arr.join(':') + ':' # ex: ["a"] -> "a:" dict[prefix] ||= 0 # ex: dict["a:"] ||= 0 end end return dict end |
#category_each(&block) ⇒ Object
923 924 925 926 927 928 929 |
# File 'lib/benry/cmdapp.rb', line 923 def category_each(&block) #; [!67r3i] returns Enumerator object if block not given. return enum_for(:category_each) unless block_given?() #; [!g3d1z] yields block with each prefix and desc. @category_dict.each(&block) nil end |
#category_exist?(prefix) ⇒ Boolean
917 918 919 920 921 |
# File 'lib/benry/cmdapp.rb', line 917 def category_exist?(prefix) #; [!79cyx] returns true if prefix is already registered. #; [!jx7fk] returns false if prefix is not registered yet. return @category_dict.key?(prefix) end |
#category_get_desc(prefix) ⇒ Object
931 932 933 934 935 |
# File 'lib/benry/cmdapp.rb', line 931 def category_get_desc(prefix) #; [!d47kq] returns description if prefix is registered. #; [!otp1b] returns nil if prefix is not registered. return @category_dict[prefix] end |
#metadata_action2aliases(all: true) ⇒ Object
875 876 877 878 879 880 881 882 883 |
# File 'lib/benry/cmdapp.rb', line 875 def (all: true) #; [!krry6] returns a Hash object (key: action name, value: alias metadatas). dict = {} # {action_name => [alias_metadata]} (all: all) do |md| #; [!zhcm6] skips actions which has no aliases. (dict[md.action] ||= []) << md if md.alias? end return dict end |
#metadata_add(metadata) ⇒ Object
836 837 838 839 840 841 842 |
# File 'lib/benry/cmdapp.rb', line 836 def () ! @metadata_dict.key?(.name) or raise "** assertion failed: metadata.name=#{.name.inspect}" #; [!8bhxu] registers metadata with it's name as key. @metadata_dict[.name] = #; [!k07kp] returns registered metadata objet. return end |
#metadata_del(name) ⇒ Object
850 851 852 853 854 855 |
# File 'lib/benry/cmdapp.rb', line 850 def (name) @metadata_dict.key?(name) or raise "** assertion failed: name=#{name.inspect}" #; [!69vo7] deletes metadata object corresponding to name. #; [!8vg6w] returns deleted metadata object. return @metadata_dict.delete(name) end |
#metadata_each(all: true, &b) ⇒ Object
863 864 865 866 867 868 869 870 871 872 873 |
# File 'lib/benry/cmdapp.rb', line 863 def (all: true, &b) #; [!3l6r7] returns Enumerator object if block not given. return enum_for(:metadata_each, all: all) unless block_given?() #; [!r8mb3] yields each metadata object if block given. #; [!qvc77] ignores hidden metadata if `all: false` passed. @metadata_dict.keys.sort.each do |name| = @metadata_dict[name] yield if all || ! .hidden? end nil end |
#metadata_exist?(name) ⇒ Boolean
857 858 859 860 861 |
# File 'lib/benry/cmdapp.rb', line 857 def (name) #; [!0ck5n] returns true if metadata object registered. #; [!x7ziz] returns false if metadata object not registered. return @metadata_dict.key?(name) end |
#metadata_get(name) ⇒ Object
844 845 846 847 848 |
# File 'lib/benry/cmdapp.rb', line 844 def (name) #; [!l5m49] returns metadata object corresponding to name. #; [!rztk2] returns nil if metadata not found for the name. return @metadata_dict[name] end |
#metadata_lookup(name) ⇒ Object
885 886 887 888 889 890 891 892 893 894 895 |
# File 'lib/benry/cmdapp.rb', line 885 def (name) #; [!dcs9v] looks up action metadata recursively if alias name specified. #; [!f8fqx] returns action metadata and alias args. alias_args = [] md = (name) while md != nil && md.alias? alias_args = md.args + alias_args if md.args && ! md.args.empty? md = (md.action) end return md, alias_args end |