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.constrained_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.constrained_sample) ⇒ Phrase
Returns a new instance of Phrase.
22 23 24 25 |
# File 'lib/dunmanifestin/phrase.rb', line 22 def initialize dsl_string = self.class.list.constrained_sample raise "Try again." unless dsl_string compile parse dsl_string end |
Class Method Details
.list(new_list = nil, multiline_document = false) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/dunmanifestin/phrase.rb', line 6 def self.list new_list = nil, multiline_document = false return (@list || List.new) 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
123 |
# File 'lib/dunmanifestin/phrase.rb', line 123 def article?; !!@article end |
#capitalize? ⇒ Boolean
124 |
# File 'lib/dunmanifestin/phrase.rb', line 124 def capitalize?; !!@capitalize end |
#compile(parsed_dsl) ⇒ Object
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 66 |
# File 'lib/dunmanifestin/phrase.rb', line 27 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
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/dunmanifestin/phrase.rb', line 132 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 rescue NoMethodError puts "==> Failed to inflect: .#{inflection}?" puts "==> Valid inflections are: plural, possessive, article, capitalize, titleize" self end |
#parse(dsl_string = @dsl_string) ⇒ Object
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 115 |
# File 'lib/dunmanifestin/phrase.rb', line 68 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
121 |
# File 'lib/dunmanifestin/phrase.rb', line 121 def plural?; !!@plural end |
#possessive? ⇒ Boolean
122 |
# File 'lib/dunmanifestin/phrase.rb', line 122 def possessive?; !!@possessive end |
#render_inflections(string) ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/dunmanifestin/phrase.rb', line 150 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
127 128 129 130 |
# File 'lib/dunmanifestin/phrase.rb', line 127 def titleize! delegates = @inflection_delegates[:capitalize] delegates.each { |delegate| variables[delegate].capitalize! } end |
#titleize? ⇒ Boolean
125 |
# File 'lib/dunmanifestin/phrase.rb', line 125 def titleize?; !!@titleize end |
#to_s ⇒ Object
146 147 148 |
# File 'lib/dunmanifestin/phrase.rb', line 146 def to_s render_inflections @to_s_proc.call end |
#variables ⇒ Object
117 118 119 |
# File 'lib/dunmanifestin/phrase.rb', line 117 def variables @variables ||= @variable_classes.map(&:new) end |