Class: SpecI18n::Parser::NaturalLanguage

Inherits:
Object
  • Object
show all
Defined in:
lib/spec-i18n/parser/natural_language.rb

Constant Summary collapse

BASIC_KEYWORDS =
%w{ name native describe before after it pending subject 
shared_examples_for share_as it_should_behave_like its should should_not}
ADVANCED_KEYWORDS =
%w{ hooks matchers }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ NaturalLanguage

Returns a new instance of NaturalLanguage.



16
17
18
19
20
# File 'lib/spec-i18n/parser/natural_language.rb', line 16

def initialize(language)
  raise(LanguageNilNotFound, "Language -> nil Not Found") unless language
  @keywords = NaturalLanguage.find_language(language)
  raise(LanguageNotFound, "Language #{language.to_s} Not Supported") if @keywords.nil?
end

Instance Attribute Details

#keywordsObject (readonly)

Returns the value of attribute keywords.



14
15
16
# File 'lib/spec-i18n/parser/natural_language.rb', line 14

def keywords
  @keywords
end

Class Method Details

.find_language(language) ⇒ Object

Find the language in languages.yml and return all the keywords



24
25
26
# File 'lib/spec-i18n/parser/natural_language.rb', line 24

def self.find_language(language)
  SpecI18n::SPEC_LANGUAGES[language]
end

.get(language) ⇒ Object



10
11
12
# File 'lib/spec-i18n/parser/natural_language.rb', line 10

def self.get(language)
  new(language)
end

Instance Method Details

#advanced_keywordsObject



236
237
238
# File 'lib/spec-i18n/parser/natural_language.rb', line 236

def advanced_keywords
  keywords.reject { |keyword, translated| not_include_in_advanced_keywords?(keyword) }
end

#basic_keywordsObject



232
233
234
# File 'lib/spec-i18n/parser/natural_language.rb', line 232

def basic_keywords
  keywords.reject { |keyword, translated| include_in_advanced_keywords?(keyword) }
end

#before_and_after_keywordsObject

Return the before and after words in the languages.yml



55
56
57
# File 'lib/spec-i18n/parser/natural_language.rb', line 55

def before_and_after_keywords
  spec_keywords('before').merge(spec_keywords('after'))
end

#dsl_keywordsObject

Return the describe word in the languages.yml



43
44
45
# File 'lib/spec-i18n/parser/natural_language.rb', line 43

def dsl_keywords
  spec_keywords("describe")
end

#example_group_keywordsObject

Return the it word in the languages.yml



100
101
102
# File 'lib/spec-i18n/parser/natural_language.rb', line 100

def example_group_keywords
  spec_keywords("it")
end

#expectation_keywordsObject

Return the should and should_not words in the languages.yml



49
50
51
# File 'lib/spec-i18n/parser/natural_language.rb', line 49

def expectation_keywords
  spec_keywords("should").merge(spec_keywords("should_not"))
end

#find_matcher(matcher) ⇒ Object

Find the matcher and return the words in the languages.yml

pt: matchers: equal: igual|igual_a NaturalLanguage.new(‘pt’).find_matcher(‘equal’) # => { ‘equal’ => [‘igual’, ‘igual_a’]}



151
152
153
154
155
156
# File 'lib/spec-i18n/parser/natural_language.rb', line 151

def find_matcher(matcher)
  matcher = matcher.to_s
  matcher_and_values = {}        
  matcher_and_values[matcher] = split_word(matchers[matcher])
  matcher_and_values
end

#hooksObject

Return All the Hooks in the languages.yml If not have hooks return a empty Hash

The hooks in the rspec is the argument for before and after method

      hook                hook
        \                 /
before(:each)     after(:all)


68
69
70
# File 'lib/spec-i18n/parser/natural_language.rb', line 68

def hooks
  keywords['hooks'] || {}
end

#hooks_params_keywordsObject

Return all the hooks words in the languages.yml in a Hash of Arrays



74
75
76
77
78
79
80
81
# File 'lib/spec-i18n/parser/natural_language.rb', line 74

def hooks_params_keywords
  hooks_keywords = {}
  hooks.each do |hook, value|
    values = split_word(value)
    hooks_keywords[hook] = values
  end
  hooks_keywords
end

#hooks_permutationObject

Returns the combination of before and after with hooks keywords



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/spec-i18n/parser/natural_language.rb', line 86

def hooks_permutation
  permutation = {}
  before_and_after_keywords.each do |before_keyword, before_translated|
    hooks.each do |hook_keyword, hook_translated|
      words = split_word(hook_translated)
      translation = words.collect { |word| before_translated.collect { |keyword| "#{keyword}(:#{word})" } }.flatten
      permutation["#{before_keyword}(:#{hook_keyword})"] = translation
    end
  end
  permutation
end

#incomplete?Boolean

Return true if the language don’t have the basic keywords to work with rspec-i18n

portuguese = NaturalLanguage.new(‘pt’) # Assuming Have all the keywords

portuguese.incomplete? # => false

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/spec-i18n/parser/natural_language.rb', line 35

def incomplete?
  language_words = BASIC_KEYWORDS.collect { |key| keywords[key].nil? }
  return true if language_words.include?(true)
  false
end

#invert_or_not_ruby_type_mathers(matcher_be, ruby_type_matchers) ⇒ Object

PENDENCIA - SPEC THIS



205
206
207
208
209
210
211
212
213
# File 'lib/spec-i18n/parser/natural_language.rb', line 205

def invert_or_not_ruby_type_mathers(matcher_be, ruby_type_matchers)
  ruby_type_matchers.collect do |matcher|
    if invert_order_of_object_and_verbs?(matcher) 
      invert_order(:first => matcher, :second => matcher_be)
    else
      "#{matcher_be}_#{matcher}"
    end
  end
end

#invert_order(options = {}) ⇒ Object



215
216
217
218
# File 'lib/spec-i18n/parser/natural_language.rb', line 215

def invert_order(options={})
  options.each { |key, value| options[key] = value.delete('*') }
  "#{options[:first]}_#{options[:second]}"
end

#invert_order_of_object_and_verbs?(keyword) ⇒ Boolean

Return true if the keyword has the ‘*’ character in the last position

de:

matchers:
  true_word: wahr*

@germany.invert_order_of_object_and_verbs?(matchers) # => true

pt:

matchers:
  true_word: verdadeiro

@portuguese.invert_order_of_object_and_verbs?(matchers) # => false

Returns:

  • (Boolean)


198
199
200
201
# File 'lib/spec-i18n/parser/natural_language.rb', line 198

def invert_order_of_object_and_verbs?(keyword)
  return true if keyword.to_s.include?('*')
  false
end

#it_should_behave_like_keywordsObject

Return the it should behave like words in the languages.yml



136
137
138
# File 'lib/spec-i18n/parser/natural_language.rb', line 136

def it_should_behave_like_keywords
  spec_keywords('it_should_behave_like')
end

#its_keywordsObject

Return the its keywords in the languages.yml



112
113
114
# File 'lib/spec-i18n/parser/natural_language.rb', line 112

def its_keywords
  spec_keywords("its")
end

#keywords_of_be_wordObject

Return the be words in the languages.yml If not have any matchers return a empty Array



178
179
180
# File 'lib/spec-i18n/parser/natural_language.rb', line 178

def keywords_of_be_word
  return split_word(matchers['be']) if matchers
end

#matchersObject

Return All the Matchers in the languages.yml



118
119
120
# File 'lib/spec-i18n/parser/natural_language.rb', line 118

def matchers
  keywords["matchers"] || {}
end

#pending_keywordsObject

Return the pending words in the languages.yml



142
143
144
# File 'lib/spec-i18n/parser/natural_language.rb', line 142

def pending_keywords
  spec_keywords('pending')
end

#share_as_keywordsObject

Return the share_as words in the languages.yml



130
131
132
# File 'lib/spec-i18n/parser/natural_language.rb', line 130

def share_as_keywords
  spec_keywords('share_as')
end

#shared_examples_for_keywordsObject

Return the shared examples for words in the languages.yml



124
125
126
# File 'lib/spec-i18n/parser/natural_language.rb', line 124

def shared_examples_for_keywords
  spec_keywords('shared_examples_for')
end

#spec_keywords(key) ⇒ Object

Return the words of languages.yml in a Hash with Array values

pt:

describe: descreva|descrevendo

NaturalLanguage.new(‘pt’).spec_keywords(‘describe’) # => { ‘describe’ => [‘descreva’, ‘descrevendo’]}

fr: should:

NaturalLanguage.new(‘fr’).spec_keywords(‘should’) # => { ‘should’ => [] }

NaturalLanguage.new(‘es’).spec_keywords(‘key_not_found’) # => RuntimeError: “No key_not_found in ##keywords



255
256
257
258
259
# File 'lib/spec-i18n/parser/natural_language.rb', line 255

def spec_keywords(key)        
  return { key => [] } unless keywords[key]
  values = split_word(keywords[key])
  { key => values }
end

#subject_keywordsObject

Return the subject word in the languages.yml



106
107
108
# File 'lib/spec-i18n/parser/natural_language.rb', line 106

def subject_keywords
  adverbs = spec_keywords('subject')
end

#values_from_keywords(keyword) ⇒ Object

Return the values from keywords

@pt.values_from_keywords(‘hooks’) # => ‘all’=>‘todos’,‘each’=>‘cada’



224
225
226
227
228
229
230
# File 'lib/spec-i18n/parser/natural_language.rb', line 224

def values_from_keywords(keyword)
  unless keywords[keyword]
    {}
  else
    keywords[keyword]
  end
end

#word_be(ruby_type) ⇒ Object

Return the keywords of be_* matchers in the languages.yml

pt:

matchers: 
  be: ser
  true_word: verdadeiro|verdade

NaturalLanguage.new(‘pt’).word_be(‘true’) # => [‘ser_verdadeiro’, ‘ser_verdade’]



167
168
169
170
171
172
173
# File 'lib/spec-i18n/parser/natural_language.rb', line 167

def word_be(ruby_type)
  ruby_type_matchers = matchers ? split_word(matchers["#{ruby_type}_word"]) : []
  words = keywords_of_be_word.collect do |matcher_be|
    invert_or_not_ruby_type_mathers(matcher_be, ruby_type_matchers)
  end
  words.flatten
end