eman 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d47a00fe58b4d96d4dabeb13a427f912d554239
4
- data.tar.gz: 08ffc135a0e4d33a20bc6bb836664323b8d56d97
3
+ metadata.gz: 98d11c7b517bdbe6a7f443288f62802865f25e35
4
+ data.tar.gz: 896d98abfd84544711e47d89a924fe902cc2bf42
5
5
  SHA512:
6
- metadata.gz: ee1b7507e854d0f04676742aaa9628e57be9bcc17d570cd39acfe795619fd40c1ff0190232f646a81a2375f8b5eb71740156b69790feb32503f906f6ea4f704f
7
- data.tar.gz: 476c2d48c7966269715ff3032a6cc8c22fb5903c0e1dfe7e701556be2eab012d126eeaa1a5b240071fbe535b07ecd0e063afbc639567f2fe4e6b7e1b6cc3fca8
6
+ metadata.gz: 48d48ebd57d664440421020381d9635f521e0d2e223c38066ab33c4a3369fa77465da9e3bae7762b4f6ebf0187bd90614a75367d01e13efdb2ebef0d7106c701
7
+ data.tar.gz: 1c174d36e6fd8f8cf2586c6ef989f940ac16c8951bff3a6f55edd62cfa03c7e64d08cb244833cdc8248ec284f5f7fa68baf5e93ef28fbe9946ea0d93c6a5a29d
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 2.2.0
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Eman
2
2
 
3
+ [![Build Status](https://travis-ci.org/sungwoncho/eman.svg?branch=master)](https://travis-ci.org/sungwoncho/eman)
4
+ [![Code Climate](https://codeclimate.com/github/sungwoncho/eman/badges/gpa.svg)](https://codeclimate.com/github/sungwoncho/eman)
5
+
3
6
  Easily name your controller, model, and service. Let Eman recommend you good names.
4
7
 
5
8
  ## Why?
@@ -21,8 +24,10 @@ Simply run the following commands as you see fit:
21
24
  $ emac model
22
25
  $ emac service
23
26
 
27
+ Eman will ask you a question or two, and spit out a name, with a further recommendation.
28
+
24
29
  ## Contributing
25
30
 
26
31
  ### Recommendation system
27
32
 
28
- Help us improve our recommendation system. Simply add new, relevant words in the dictionary YAML files.
33
+ Help us improve our recommendation system by adding new entries in the internal dictionary. See [this wiki](https://github.com/sungwoncho/eman/wiki/Internal-Dictionary) for more information.
data/Rakefile CHANGED
@@ -1,2 +1,9 @@
1
1
  require "bundler/gem_tasks"
2
2
 
3
+ begin
4
+ require "rspec/core/rake_task"
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ rescue LoadError
7
+ end
8
+
9
+ task default: :spec
data/bin/eman CHANGED
@@ -1,5 +1,3 @@
1
- #!/usr/bin/env ruby
2
-
3
1
  require "thor"
4
2
  require "eman"
5
3
 
@@ -8,19 +6,19 @@ module Eman
8
6
 
9
7
  desc "controller", "name a controller"
10
8
  def controller
11
- generator = Eman::NameGenerator.new('Controller')
9
+ generator = Eman::Generator.new('Controller')
12
10
  generator.run
13
11
  end
14
12
 
15
13
  desc "service", "name a service"
16
14
  def service
17
- generator = Eman::NameGenerator.new('Service')
15
+ generator = Eman::Generator.new('Service')
18
16
  generator.run
19
17
  end
20
18
 
21
19
  desc "model", "name a model"
22
20
  def model
23
- generator = Eman::NameGenerator.new('Model')
21
+ generator = Eman::Generator.new('Model')
24
22
  generator.run
25
23
  end
26
24
 
@@ -3,6 +3,8 @@ $:.unshift File.expand_path('../eman', __FILE__)
3
3
  require "version"
4
4
 
5
5
  module Eman
6
- autoload :NameGenerator, 'name_generator'
7
- autoload :NameFormatter, 'name_formatter'
6
+ autoload :Generator, 'generator'
7
+ autoload :Formatter, 'formatter'
8
+ autoload :Recommender, 'recommender'
9
+ autoload :Dictionary, 'dictionary'
8
10
  end
@@ -0,0 +1,27 @@
1
+ require "yaml"
2
+
3
+ module Eman
4
+ class Dictionary
5
+
6
+ INDICES_FILE = File.expand_path('../dictionary/indices.yml', __FILE__)
7
+ WORDS_FILE = File.expand_path('../dictionary/words.yml', __FILE__)
8
+
9
+ def self.find_similar(word)
10
+ index = fetch_index(word)
11
+ fetch_words(index)
12
+ end
13
+
14
+ private
15
+
16
+ def self.fetch_index(key)
17
+ indices = YAML.load_file(INDICES_FILE)
18
+ indices[key]
19
+ end
20
+
21
+ def self.fetch_words(index)
22
+ words_list = YAML.load_file(WORDS_FILE)
23
+ words_list[index] || []
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,21 @@
1
+ acquisition: 5
2
+ benchmark: 2
3
+ buy: 5
4
+ buyer: 1
5
+ booking: 4
6
+ calculate: 2
7
+ chat: 3
8
+ count: 2
9
+ convesation: 3
10
+ customer: 1
11
+ client: 1
12
+ enrollment: 4
13
+ measure: 2
14
+ message: 3
15
+ notification: 3
16
+ registration: 4
17
+ post: 6
18
+ purchase: 5
19
+ shopping: 5
20
+ thread: 6
21
+ user: 1
@@ -0,0 +1,6 @@
1
+ 1: ['user', 'customer', 'client', 'buyer']
2
+ 2: ['measure', 'benchmark', 'calculate', 'count']
3
+ 3: ['message', 'conversation', 'notification', 'chat']
4
+ 4: ['registration', 'enrollment', 'booking']
5
+ 5: ['purchase', 'buy', 'acquisition', 'shopping']
6
+ 6: ['post', 'thread']
@@ -0,0 +1,48 @@
1
+ require "active_support/inflector"
2
+
3
+ module Eman
4
+ class Formatter
5
+
6
+ def initialize(generator)
7
+ @generator = generator
8
+ end
9
+
10
+ def camel_case!
11
+ if is_model_name?
12
+ "#{inputs_camelized}"
13
+ else
14
+ "#{inputs_camelized}#{type.capitalize}"
15
+ end
16
+ end
17
+
18
+ def snake_case!
19
+ if is_model_name?
20
+ "#{inputs_snakified}"
21
+ else
22
+ "#{inputs_snakified}_#{type.downcase}"
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def inputs
29
+ @generator.inputs
30
+ end
31
+
32
+ def type
33
+ @generator.type
34
+ end
35
+
36
+ def inputs_camelized
37
+ inputs.collect(&:capitalize).join
38
+ end
39
+
40
+ def inputs_snakified
41
+ inputs.collect(&:downcase).join('_')
42
+ end
43
+
44
+ def is_model_name?
45
+ type == 'Model'
46
+ end
47
+ end
48
+ end
@@ -1,14 +1,11 @@
1
1
  module Eman
2
- class NameGenerator
2
+ class Generator
3
3
 
4
- attr_accessor :resource, :verb, :name
4
+ attr_accessor :resource, :verb, :name, :recommended_name
5
5
  attr_reader :type
6
6
 
7
7
  def initialize(type)
8
8
  @type = type
9
- @resource = ''
10
- @verb = ''
11
- @name = ''
12
9
  end
13
10
 
14
11
  def run
@@ -18,24 +15,30 @@ module Eman
18
15
  print_name
19
16
  end
20
17
 
18
+ def inputs
19
+ [resource, verb].compact.flat_map(&:split)
20
+ end
21
+
21
22
  private
22
23
 
23
24
  def ask_resource
24
25
  puts "What is the resource that you are dealing with? (e.g. User, Session, Order, etc.)"
25
- @resource = $stdin.gets.chomp
26
+ @resource = $stdin.gets.chomp.downcase
26
27
  end
27
28
 
28
29
  def ask_verb
29
30
  puts "What is the primary action you are performing on '#{resource}'?"
30
- @verb = $stdin.gets.chomp
31
+ @verb = $stdin.gets.chomp.downcase
31
32
  end
32
33
 
33
34
  def generate_name
34
- @name = ::Eman::NameFormatter.new(@resource, @verb, @type).camel_case!
35
+ @name = Eman::Formatter.new(self).camel_case!
36
+ @recommended_name = Eman::Recommender.new(self).recommend_name
35
37
  end
36
38
 
37
39
  def print_name
38
40
  puts "#{type} name : '#{name}'"
41
+ puts "Further suggestion: '#{recommended_name}'" if recommended_name
39
42
  end
40
43
  end
41
44
  end
@@ -0,0 +1,56 @@
1
+ module Eman
2
+ class Recommender
3
+
4
+ attr_reader :generator
5
+
6
+ def initialize(generator)
7
+ @generator = generator
8
+ end
9
+
10
+ def inputs
11
+ @inputs ||= @generator.inputs
12
+ end
13
+
14
+ def type
15
+ @type ||= @generator.type
16
+ end
17
+
18
+ def words_hash
19
+ @words_hash ||= Hash[ inputs.collect { |x| [x, []] } ]
20
+ end
21
+
22
+ def recommend_name
23
+ fetch_similar_words
24
+ assemble_words
25
+ end
26
+
27
+ private
28
+
29
+ def fetch_similar_words
30
+ inputs.each do |i|
31
+ synonyms = Dictionary.find_similar(i)
32
+
33
+ synonyms.each { |s| words_hash[i] << s } if synonyms.any?
34
+ end
35
+ end
36
+
37
+ def assemble_words
38
+ recommended_name = []
39
+
40
+ if words_hash.values.flatten.length != inputs.length
41
+
42
+ words_hash.each do |key, val|
43
+ recommended_name << val.sample
44
+ end
45
+
46
+ if recommended_name.any?
47
+ recommended = Eman::Generator.new(type)
48
+ recommended.resource = recommended_name.join(' ')
49
+
50
+ recommended
51
+ end
52
+
53
+ end
54
+ end
55
+ end
56
+ end
@@ -1,3 +1,3 @@
1
1
  module Eman
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,13 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Eman::Dictionary do
4
+
5
+ describe '.find_similar' do
6
+ it 'returns an array of similar words' do
7
+ set_up_custom_dictionary
8
+
9
+ array = Eman::Dictionary.find_similar('user')
10
+ expect(array).to eq ['user', 'customer']
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,50 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Eman::Formatter do
4
+
5
+ describe '#camel_case!' do
6
+ it 'outsputs a camel cased name' do
7
+ generator = Eman::Generator.new('Service')
8
+ generator.resource = 'Cheese Cake'
9
+ generator.verb = 'Grinding'
10
+ formatter = Eman::Formatter.new(generator)
11
+ outcome = formatter.camel_case!
12
+
13
+ expect(outcome).to eq 'CheeseCakeGrindingService'
14
+ end
15
+
16
+ context 'when type is model' do
17
+ it 'does not append the type at the end' do
18
+ generator = Eman::Generator.new('Model')
19
+ generator.resource = 'Shopping Cart'
20
+ formatter = Eman::Formatter.new(generator)
21
+ outcome = formatter.camel_case!
22
+
23
+ expect(outcome).to eq 'ShoppingCart'
24
+ end
25
+ end
26
+ end
27
+
28
+ describe '#snake_case!' do
29
+ it 'outputs a snake cased name' do
30
+ generator = Eman::Generator.new('Service')
31
+ generator.resource = 'Cheese Cake'
32
+ generator.verb = 'Grinding'
33
+ formatter = Eman::Formatter.new(generator)
34
+ outcome = formatter.snake_case!
35
+
36
+ expect(outcome).to eq 'cheese_cake_grinding_service'
37
+ end
38
+
39
+ context 'when type is model' do
40
+ it 'does not append the type at the end' do
41
+ generator = Eman::Generator.new('Model')
42
+ generator.resource = 'Shopping Cart'
43
+ formatter = Eman::Formatter.new(generator)
44
+ outcome = formatter.snake_case!
45
+
46
+ expect(outcome).to eq 'shopping_cart'
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,35 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Eman::Generator do
4
+ describe '#run' do
5
+ context 'when type is service' do
6
+ it 'should ask two questions' do
7
+ generator = Eman::Generator.new('Service')
8
+ allow($stdin).to receive(:gets) { 'Measure' }
9
+
10
+ expect($stdin).to receive(:gets).twice
11
+ generator.run
12
+ end
13
+ end
14
+
15
+ context 'when type is not service' do
16
+ it 'should ask one question' do
17
+ generator = Eman::Generator.new('Controller')
18
+ allow($stdin).to receive(:gets) { 'User' }
19
+
20
+ expect($stdin).to receive(:gets).once
21
+ generator.run
22
+ end
23
+ end
24
+ end
25
+
26
+ describe '#inputs' do
27
+ it 'returns a flat array splited at spaces' do
28
+ generator = Eman::Generator.new('Service')
29
+ generator.resource = 'Alarm clock'
30
+ generator.verb = 'reset'
31
+
32
+ expect(generator.inputs).to eq ['Alarm', 'clock', 'reset']
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,42 @@
1
+ require "spec_helper"
2
+
3
+ RSpec.describe Eman::Recommender do
4
+
5
+ before(:each) do
6
+ @generator = Eman::Generator.new('Service')
7
+ @generator.resource = 'Train Conductor'
8
+ @generator.verb = 'Whistling'
9
+
10
+ @formatter = Eman::Recommender.new(@generator)
11
+ end
12
+
13
+ describe '#inputs' do
14
+ it 'returns the generators inputs' do
15
+ expect(@formatter.inputs).to eq ['Train', 'Conductor', 'Whistling']
16
+ end
17
+ end
18
+
19
+ describe '#words_hash' do
20
+ it 'returns hash of words as keys and empty arrays as values' do
21
+ expect(@formatter.words_hash).to eq({ 'Train' => [], 'Conductor' => [], 'Whistling' => [] })
22
+ end
23
+ end
24
+
25
+ describe '#recommend_name' do
26
+ it 'recommends a name' do
27
+ set_up_custom_dictionary
28
+
29
+ generator = Eman::Generator.new('Controller')
30
+ generator.resource = 'deep'
31
+
32
+ recommender = Eman::Recommender.new(generator)
33
+
34
+ puts recommender.words_hash
35
+
36
+ recommended_name = recommender.recommend_name
37
+
38
+ puts recommender.words_hash
39
+ expect(recommended_name.resource).to eq 'house'
40
+ end
41
+ end
42
+ end
@@ -1,6 +1,9 @@
1
1
  # Require all files under /lib
2
2
  Dir[File.expand_path('../../lib/**/*.rb', __FILE__)].each { |f| require f }
3
3
 
4
+ # Require all support files
5
+ Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
6
+
4
7
  # This file was generated by the `rspec --init` command. Conventionally, all
5
8
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
9
  # The generated `.rspec` file contains `--require spec_helper` which will cause
@@ -43,6 +46,8 @@ RSpec.configure do |config|
43
46
  mocks.verify_partial_doubles = true
44
47
  end
45
48
 
49
+ config.include DictionaryMacros
50
+
46
51
  # The settings below are suggested to provide a good initial experience
47
52
  # with RSpec, but feel free to customize to your heart's content.
48
53
  =begin
@@ -0,0 +1,3 @@
1
+ user: 1
2
+ customer: 1
3
+ deep: 2
@@ -0,0 +1,2 @@
1
+ 1: ['user', 'customer']
2
+ 2: ['house', 'house']
@@ -0,0 +1,11 @@
1
+ module DictionaryMacros
2
+ def set_up_custom_dictionary
3
+ # Custom dictionary files defined in spec/support/dictionary
4
+ indices_file = YAML.load_file(File.expand_path('../dictionary/indices.yml', __FILE__))
5
+ words_file = YAML.load_file(File.expand_path('../dictionary/words.yml', __FILE__))
6
+
7
+ # Use a custom dictionary files for tests.
8
+ allow(YAML).to receive(:load_file).with(File.expand_path('../../../lib/eman/dictionary/indices.yml', __FILE__)).and_return(indices_file)
9
+ allow(YAML).to receive(:load_file).with(File.expand_path('../../../lib/eman/dictionary/words.yml', __FILE__)).and_return(words_file)
10
+ end
11
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sung Won Cho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-21 00:00:00.000000000 Z
11
+ date: 2015-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -90,6 +90,7 @@ extra_rdoc_files: []
90
90
  files:
91
91
  - ".gitignore"
92
92
  - ".rspec"
93
+ - ".travis.yml"
93
94
  - Gemfile
94
95
  - LICENSE.txt
95
96
  - README.md
@@ -97,13 +98,22 @@ files:
97
98
  - bin/eman
98
99
  - eman.gemspec
99
100
  - lib/eman.rb
100
- - lib/eman/name_formatter.rb
101
- - lib/eman/name_generator.rb
101
+ - lib/eman/dictionary.rb
102
+ - lib/eman/dictionary/indices.yml
103
+ - lib/eman/dictionary/words.yml
104
+ - lib/eman/formatter.rb
105
+ - lib/eman/generator.rb
106
+ - lib/eman/recommender.rb
102
107
  - lib/eman/version.rb
108
+ - spec/dictionary_spec.rb
103
109
  - spec/eman_spec.rb
104
- - spec/name_formatter_spec.rb
105
- - spec/name_generator_spec.rb
110
+ - spec/formatter_spec.rb
111
+ - spec/generator_spec.rb
112
+ - spec/recommender_spec.rb
106
113
  - spec/spec_helper.rb
114
+ - spec/support/dictionary/indices.yml
115
+ - spec/support/dictionary/words.yml
116
+ - spec/support/dictionary_macros.rb
107
117
  homepage: https://github.com/sungwoncho/eman
108
118
  licenses:
109
119
  - MIT
@@ -129,7 +139,12 @@ signing_key:
129
139
  specification_version: 4
130
140
  summary: Easily name your controller, model, and service.
131
141
  test_files:
142
+ - spec/dictionary_spec.rb
132
143
  - spec/eman_spec.rb
133
- - spec/name_formatter_spec.rb
134
- - spec/name_generator_spec.rb
144
+ - spec/formatter_spec.rb
145
+ - spec/generator_spec.rb
146
+ - spec/recommender_spec.rb
135
147
  - spec/spec_helper.rb
148
+ - spec/support/dictionary/indices.yml
149
+ - spec/support/dictionary/words.yml
150
+ - spec/support/dictionary_macros.rb
@@ -1,52 +0,0 @@
1
- require "active_support/inflector"
2
-
3
- module Eman
4
- class NameFormatter
5
-
6
- attr_accessor :type, :resource, :verb
7
-
8
- def initialize(resource, verb, type)
9
- @type = type
10
- @resource = is_controller_name? ? resource.pluralize : resource
11
- @verb = verb
12
- end
13
-
14
- def camel_case!
15
- if is_model_name?
16
- "#{components_camelized}"
17
- else
18
- "#{components_camelized}#{type.capitalize}"
19
- end
20
- end
21
-
22
- def snake_case!
23
- if is_model_name?
24
- "#{components_snakified}"
25
- else
26
- "#{components_snakified}_#{type.downcase}"
27
- end
28
- end
29
-
30
- private
31
-
32
- def components
33
- [resource, verb].flat_map(&:split)
34
- end
35
-
36
- def components_camelized
37
- components.collect(&:capitalize).join
38
- end
39
-
40
- def components_snakified
41
- components.collect(&:downcase).join('_')
42
- end
43
-
44
- def is_controller_name?
45
- type == 'Controller'
46
- end
47
-
48
- def is_model_name?
49
- type == 'Model'
50
- end
51
- end
52
- end
@@ -1,56 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe Eman::NameFormatter do
4
-
5
- describe '#initialize' do
6
- context 'when type is controller' do
7
- it 'pluralizes the resource' do
8
- formatter = Eman::NameFormatter.new('Alarm clock', 'Operating', 'Controller')
9
- expect(formatter.resource).to eq 'Alarm clocks'
10
- end
11
- end
12
-
13
- context 'when type is not controller' do
14
- it 'does not pluralize the resource' do
15
- formatter = Eman::NameFormatter.new('Alarm clock', 'Operating', 'Service')
16
- expect(formatter.resource).to eq 'Alarm clock'
17
- end
18
- end
19
- end
20
-
21
- describe '#camel_case!' do
22
- it 'outsputs a camel cased name' do
23
- formatter = Eman::NameFormatter.new('cheese', 'grinding', 'Service')
24
- outcome = formatter.camel_case!
25
-
26
- expect(outcome).to eq 'CheeseGrindingService'
27
- end
28
-
29
- context 'when type is model' do
30
- it 'does not append the type at the end' do
31
- formatter = Eman::NameFormatter.new('Shopping', 'Cart', 'Model')
32
- outcome = formatter.camel_case!
33
-
34
- expect(outcome).to eq 'ShoppingCart'
35
- end
36
- end
37
- end
38
-
39
- describe '#snake_case!' do
40
- it 'outputs a snake cased name' do
41
- formatter = Eman::NameFormatter.new('guitar', 'smashing', 'Service')
42
- outcome = formatter.snake_case!
43
-
44
- expect(outcome).to eq 'guitar_smashing_service'
45
- end
46
-
47
- context 'when type is model' do
48
- it 'does not append the type at the end' do
49
- formatter = Eman::NameFormatter.new('Shopping', 'Cart', 'Model')
50
- outcome = formatter.snake_case!
51
-
52
- expect(outcome).to eq 'shopping_cart'
53
- end
54
- end
55
- end
56
- end
@@ -1,5 +0,0 @@
1
- require "spec_helper"
2
-
3
- RSpec.describe Eman::NameGenerator do
4
-
5
- end