Class: Terse::KeywordRuby
Overview
A class storing all information about a keyword & what it should be transformed into
Instance Attribute Summary
Attributes inherited from Keyword
#follow_on_regex, #follow_on_substitute, #is_end, #keyword, #needs_inner_end, #needs_inner_start, #needs_line_ending, #needs_top_level_end, #needs_top_level_start, #regex, #substitute, #try_follow_on_regex
Class Method Summary collapse
Instance Method Summary collapse
-
#gen_regex_from_keyword(keyword) ⇒ Object
Simple regex to match a keyword at the start of a line.
-
#gen_regex_from_keyword_including_follow_on(keyword) ⇒ Object
Regex to match a keyword at the start of a line, and the next word on that line E.g.
-
#initialize(keyword) ⇒ KeywordRuby
constructor
A new instance of KeywordRuby.
-
#set_substitute ⇒ Object
TODO this doesn’t work, needs to process the thing following the keyword, not the keyword itself.
Constructor Details
#initialize(keyword) ⇒ KeywordRuby
Returns a new instance of KeywordRuby.
8 9 10 11 |
# File 'lib/terse/keyword_ruby.rb', line 8 def initialize(keyword) super(keyword) @loop_ending = "end" end |
Class Method Details
.generate_keywords ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/terse/keyword_ruby.rb', line 49 def self.generate_keywords keywords = [] keys = [:req, :reqr, :i, :c, :m, :d, :e, :a, :r, :w] keys.each do |key| k = KeywordRuby.new key # k.keyword = key case key when :req k.substitute = "require" k.try_follow_on_regex = true k.follow_on_substitute = "\"\#{$3}\"" when :reqr k.substitute = "require_relative" k.try_follow_on_regex = true k.follow_on_substitute = "\"\#{$3}\"" when :i k.substitute = "include" when :c k.substitute = "class" k.needs_top_level_end = true when :m k.substitute = "module" k.needs_top_level_end = true when :d k.substitute = "def" k.needs_inner_end = true when :e k.substitute = "end" k.is_end = true when :a k.substitute = "attr_accessor" k.try_follow_on_regex = true k.follow_on_substitute = "$3.to_sym"#"\":\#{$3}\"" when :r k.substitute = "attr_reader" k.try_follow_on_regex = true k.follow_on_substitute = "$3.to_sym" when :w k.substitute = "attr_writer" k.try_follow_on_regex = true k.follow_on_substitute = "$3.to_sym" else # raise "Unknown keyword #{key}" puts "Unknown keyword #{key}" end keywords << k end # end keys.each keywords end |
Instance Method Details
#gen_regex_from_keyword(keyword) ⇒ Object
Simple regex to match a keyword at the start of a line
37 38 39 |
# File 'lib/terse/keyword_ruby.rb', line 37 def gen_regex_from_keyword keyword return /(^\s*)(#{keyword})(\s+|$)/ end |
#gen_regex_from_keyword_including_follow_on(keyword) ⇒ Object
Regex to match a keyword at the start of a line, and the next word on that line E.g. this will also catch (in $3) the item following the “require” keyword This enables the routine to format this follow-on word, e.g. to turn ‘req a_gem’ into ‘require “a_gem”’
45 46 47 |
# File 'lib/terse/keyword_ruby.rb', line 45 def gen_regex_from_keyword_including_follow_on keyword return /(^\s*)(#{keyword})\s+([a-zA-Z0-9_]+)/ end |
#set_substitute ⇒ Object
TODO this doesn’t work, needs to process the thing following the keyword, not the keyword itself
14 15 16 17 18 19 20 21 |
# File 'lib/terse/keyword_ruby.rb', line 14 def set_substitute case @keyword when "a", "r", "w" @substitute = ":" + @substitute when "req", "reqr" @substitute = "\"" + @substitute + "\"" unless ["\"", "'"].include? @substitute[0] end end |