Class: Phrase
- Inherits:
-
Object
- Object
- Phrase
- Defined in:
- lib/dunmanifestin/phrase.rb
Class Method Summary collapse
Instance Method Summary collapse
- #article? ⇒ Boolean
- #capitalize? ⇒ Boolean
- #compile(parsed_dsl) ⇒ Object
- #inflect(inflection) ⇒ Object
-
#initialize(dsl_string = self.class.list.sample) ⇒ Phrase
constructor
A new instance of Phrase.
- #parse(dsl_string = @dsl_string) ⇒ Object
- #plural? ⇒ Boolean
- #possessive? ⇒ Boolean
- #render_inflections(string) ⇒ Object
- #titleize! ⇒ Object
- #titleize? ⇒ Boolean
- #to_s ⇒ Object
- #variables ⇒ Object
Constructor Details
#initialize(dsl_string = self.class.list.sample) ⇒ Phrase
Returns a new instance of Phrase.
21 22 23 24 |
# File 'lib/dunmanifestin/phrase.rb', line 21 def initialize dsl_string = self.class.list.sample raise "Try again." unless dsl_string compile parse dsl_string end |
Class Method Details
.list(new_list = nil, multiline_document = false) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dunmanifestin/phrase.rb', line 5 def self.list new_list = nil, multiline_document = false return (@list || []) unless new_list new_list = new_list.split_on_newlines_and_strip if new_list.is_a?(String) unless multiline_document new_list = [new_list] if multiline_document new_list.map! do |line| multiplier_regex = /^\d+@/ multiplier = line.match(multiplier_regex).to_s.to_i multiplier = 1 if multiplier < 1 [line.gsub(multiplier_regex, '')] * multiplier end.flatten!.reject! { |i| i.nil? || i.empty? } @list = new_list end |
Instance Method Details
#article? ⇒ Boolean
122 |
# File 'lib/dunmanifestin/phrase.rb', line 122 def article?; !!@article end |
#capitalize? ⇒ Boolean
123 |
# File 'lib/dunmanifestin/phrase.rb', line 123 def capitalize?; !!@capitalize end |
#compile(parsed_dsl) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dunmanifestin/phrase.rb', line 26 def compile parsed_dsl template = parsed_dsl[:template] @variable_classes = [] @inflection_delegates = { :plural => [], :possessive => [], :article => [], :capitalize => [], } inflections = [] parsed_dsl[:variables].each_with_index do |variable, i| rough_var_class = variable[:rough_variable_class] variable[:inflections_to_delegate].each do |inflection| @inflection_delegates[inflection.to_sym] << i end inflections[i] = [] variable[:inflections_to_apply].each do |inflection| inflections[i] << inflection.to_sym end begin @variable_classes << "Phrase::#{rough_var_class.camelize}".constantize rescue NameError @variable_classes << empty_phrase_class(rough_var_class) end end @to_s_proc = -> { self.variables.each_with_index do |variable, i| inflections[i].each { |inflection| variable.inflect inflection } end template.zip(self.variables).flatten.map(&:to_s).join('') } end |
#inflect(inflection) ⇒ Object
131 132 133 134 135 136 137 138 139 140 |
# File 'lib/dunmanifestin/phrase.rb', line 131 def inflect inflection return self unless @inflection_delegates self.instance_variable_set("@#{inflection}", true) return (titleize! && self) if inflection == :titleize delegates = @inflection_delegates[inflection] delegates.each { |delegate| variables[delegate].inflect inflection } self end |
#parse(dsl_string = @dsl_string) ⇒ Object
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 107 108 109 110 111 112 113 114 |
# File 'lib/dunmanifestin/phrase.rb', line 67 def parse dsl_string = @dsl_string # A dsl string like "[adjective] [noun.possessive#plural]" will be parsed into # { :variables => # [ { :rough_variable_class => 'adjective', # :inflections_to_delegate => [], # :inflections_to_apply => [] # }, # { :rough_variable_class => 'noun', # :inflections_to_delegate => [:plural], # :inflections_to_apply => [:possessive] # } # ], # :template => ["", " "] # } # tokens = dsl_string.split(/[\[\]]/) template = []; variables = []; tokens.each_with_index do |token, i| if i % 2 == 0 template << token else variables << token end end hash = {:variables => [], :template => template} variables.each_with_index do |variable, i| components = variable.split(/\b/) rough_var_class = components.shift inflections_to_delegate = [] inflections_to_apply = [] components.each_with_index do |v, k| inflections_to_delegate << v if components[k-1] == '#' inflections_to_apply << v if components[k-1] == '.' end hash[:variables] << { :rough_variable_class => rough_var_class, :inflections_to_delegate => inflections_to_delegate, :inflections_to_apply => inflections_to_apply, } end hash end |
#plural? ⇒ Boolean
120 |
# File 'lib/dunmanifestin/phrase.rb', line 120 def plural?; !!@plural end |
#possessive? ⇒ Boolean
121 |
# File 'lib/dunmanifestin/phrase.rb', line 121 def possessive?; !!@possessive end |
#render_inflections(string) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/dunmanifestin/phrase.rb', line 146 def render_inflections string # Good, now turn this into a stateless function. string = string.pluralize if plural? && @inflection_delegates[:plural].empty? string = (string =~ /s$/) ? "#{string}'" : "#{string}'s" if plural? && possessive? && @inflection_delegates[:possessive].empty? string = "#{string}'s" if !plural? && possessive? && @inflection_delegates[:possessive].empty? string = (string =~ /^[aeiou]/i) ? "an #{string}" : "a #{string}" if !plural? && article? && @inflection_delegates[:article].empty? string = string[0].capitalize + string[1 .. -1] if capitalize? string = string.titleize if titleize? string end |
#titleize! ⇒ Object
126 127 128 129 |
# File 'lib/dunmanifestin/phrase.rb', line 126 def titleize! delegates = @inflection_delegates[:capitalize] delegates.each { |delegate| variables[delegate].capitalize! } end |
#titleize? ⇒ Boolean
124 |
# File 'lib/dunmanifestin/phrase.rb', line 124 def titleize?; !!@titleize end |
#to_s ⇒ Object
142 143 144 |
# File 'lib/dunmanifestin/phrase.rb', line 142 def to_s render_inflections @to_s_proc.call end |
#variables ⇒ Object
116 117 118 |
# File 'lib/dunmanifestin/phrase.rb', line 116 def variables @variables ||= @variable_classes.map(&:new) end |