Class: TerseRuby::Keyword
- Inherits:
-
Object
- Object
- TerseRuby::Keyword
- Defined in:
- lib/terse_ruby/keyword.rb
Overview
A class storing all information about a keyword & what it should be transformed into
Instance Attribute Summary collapse
-
#follow_on_regex ⇒ Object
Returns the value of attribute follow_on_regex.
-
#follow_on_substitute ⇒ Object
Returns the value of attribute follow_on_substitute.
-
#is_end ⇒ Object
Returns the value of attribute is_end.
-
#keyword ⇒ Object
Returns the value of attribute keyword.
-
#needs_inner_end ⇒ Object
Returns the value of attribute needs_inner_end.
-
#needs_top_level_end ⇒ Object
Returns the value of attribute needs_top_level_end.
-
#regex ⇒ Object
Returns the value of attribute regex.
-
#substitute ⇒ Object
Returns the value of attribute substitute.
-
#try_follow_on_regex ⇒ Object
Returns the value of attribute 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) ⇒ Keyword
constructor
A new instance of Keyword.
-
#set_substitute ⇒ Object
TODO this doesn’t work, needs to process the thing following the keyword, not the keyword itself.
Constructor Details
#initialize(keyword) ⇒ Keyword
Returns a new instance of Keyword.
15 16 17 18 19 |
# File 'lib/terse_ruby/keyword.rb', line 15 def initialize(keyword) @keyword = keyword @regex = gen_regex_from_keyword keyword @follow_on_regex = gen_regex_from_keyword_including_follow_on keyword end |
Instance Attribute Details
#follow_on_regex ⇒ Object
Returns the value of attribute follow_on_regex.
8 9 10 |
# File 'lib/terse_ruby/keyword.rb', line 8 def follow_on_regex @follow_on_regex end |
#follow_on_substitute ⇒ Object
Returns the value of attribute follow_on_substitute.
9 10 11 |
# File 'lib/terse_ruby/keyword.rb', line 9 def follow_on_substitute @follow_on_substitute end |
#is_end ⇒ Object
Returns the value of attribute is_end.
13 14 15 |
# File 'lib/terse_ruby/keyword.rb', line 13 def is_end @is_end end |
#keyword ⇒ Object
Returns the value of attribute keyword.
5 6 7 |
# File 'lib/terse_ruby/keyword.rb', line 5 def keyword @keyword end |
#needs_inner_end ⇒ Object
Returns the value of attribute needs_inner_end.
12 13 14 |
# File 'lib/terse_ruby/keyword.rb', line 12 def needs_inner_end @needs_inner_end end |
#needs_top_level_end ⇒ Object
Returns the value of attribute needs_top_level_end.
11 12 13 |
# File 'lib/terse_ruby/keyword.rb', line 11 def needs_top_level_end @needs_top_level_end end |
#regex ⇒ Object
Returns the value of attribute regex.
7 8 9 |
# File 'lib/terse_ruby/keyword.rb', line 7 def regex @regex end |
#substitute ⇒ Object
Returns the value of attribute substitute.
6 7 8 |
# File 'lib/terse_ruby/keyword.rb', line 6 def substitute @substitute end |
#try_follow_on_regex ⇒ Object
Returns the value of attribute try_follow_on_regex.
10 11 12 |
# File 'lib/terse_ruby/keyword.rb', line 10 def try_follow_on_regex @try_follow_on_regex end |
Class Method Details
.generate_keywords ⇒ Object
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 99 100 101 102 103 104 105 106 |
# File 'lib/terse_ruby/keyword.rb', line 57 def self.generate_keywords keywords = [] keys = [:req, :reqr, :i, :c, :m, :d, :e, :a, :r, :w] keys.each do |key| k = Keyword.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
45 46 47 |
# File 'lib/terse_ruby/keyword.rb', line 45 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”’
53 54 55 |
# File 'lib/terse_ruby/keyword.rb', line 53 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
22 23 24 25 26 27 28 29 |
# File 'lib/terse_ruby/keyword.rb', line 22 def set_substitute case @keyword when "a", "r", "w" @substitute = ":" + @substitute when "req", "reqr" @substitute = "\"" + @substitute + "\"" unless ["\"", "'"].include? @substitute[0] end end |