class Terse::KeywordRuby
A class storing all information about a keyword & what it should be transformed into
Public Class Methods
generate_keywords()
click to toggle source
# 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
new(keyword)
click to toggle source
Calls superclass method
Terse::Keyword::new
# File lib/terse/keyword_ruby.rb, line 8 def initialize(keyword) super(keyword) @loop_ending = "end" end
Public Instance Methods
gen_regex_from_keyword(keyword)
click to toggle source
Simple regex to match a keyword at the start of a line
# 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)
click to toggle source
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”'
# 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()
click to toggle source
TODO this doesn't work, needs to process the thing following the keyword, not the keyword itself
# 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