Class: Obvious::Generators::ApplicationGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/generators/application_generator.rb

Class Method Summary collapse

Class Method Details

.create_directories(directories) ⇒ Object

::generate



49
50
51
52
53
# File 'lib/generators/application_generator.rb', line 49

def create_directories directories
  directories.each do |dir|
    Dir.mkdir @app.dir + dir
  end
end

.generateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/application_generator.rb', line 10

def generate
  @app = Obvious::Generators::Application.instance

  puts 'Generating the files...'

  Obvious::Generators::ApplicationGenerator.create_directories ['/', '/actions', '/contracts', '/entities',
    '/spec', '/spec/actions', '/spec/contracts', '/spec/entities',
    '/spec/doubles']

  unless File.exist? "#{@app.target_path}/Rakefile" 
    puts 'Creating Rakefile...'
    `cp #{@app.lib_path}/obvious/files/Rakefile #{@app.target_path}/Rakefile`
  end

  unless File.exist? "#{@app.target_path}/external"
    puts 'Creating external directory'
    `cp -r #{@app.lib_path}/obvious/files/external #{@app.target_path}/external`
  end

  descriptors = Dir['descriptors/*.yml']

  puts 'Creating actions from descriptors... ' unless descriptors.length.zero?
  descriptors.each do |file|
    yaml = YAML.load_file(file)
    descriptor = Obvious::Generators::Descriptor.new yaml
    descriptor.to_file
  end

  @app.remove_duplicates

  puts 'Writing Entities scaffolds... '
  Obvious::Generators::ApplicationGenerator.write_entities

  puts 'Writing jacks scaffolds... '
  Obvious::Generators::ApplicationGenerator.write_jacks

  puts "Files are located in the `#{@app.dir}` directory."
end

.write_entitiesObject



55
56
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
# File 'lib/generators/application_generator.rb', line 55

def write_entities
  @app.entities.each do |k, v|
    name          = k
    method_specs  = ''
    method_defs   = ''

    method_defs << '
  def self.shape
    {}
  end
  '

    v.each do |method|
      method_defs << "
  def #{method} input
    nil
  end
    "

      method_specs << "
  describe '.#{method}' do
    it 'should #{method} with valid input'

    it 'should raise an error with invalid input'

  end
    "
    end

    output = %Q{class #{name}
  #{method_defs}
end
}
    snake_name = name.gsub(/(.)([A-Z])/,'\1_\2').downcase

    filename = "#{@app.dir}/entities/#{snake_name}.rb"
    File.open(filename, 'w') { |f| f.write(output) }

    output = %Q{require_relative '../../entities/#{snake_name}'

describe #{name} do
  #{method_specs}
end
}

    filename = "#{@app.dir}/spec/entities/#{snake_name}_spec.rb"
    File.open(filename, 'w') {|f| f.write(output) }
  end
end

.write_jacksObject

write_entities



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/generators/application_generator.rb', line 105

def write_jacks
  @app.jacks.each do |k, v|
    name         = k.chomp('Jack').downcase
    method_specs = ''
    contract_defs  = ''

    jack_double_default_methods = ''
    jack_double_badoutput_methods = ''

    v.each do |method|

      contract_defs << "
  contract_for :#{method}, {                                                                                                                                        
    :input  => {},                                                                                                                                   
    :output => {},                                                                                                                                   
  } 
    "

      method_specs << "
  describe '.#{method}_contract' do
    it 'should #{method} data with valid input'

    it 'should raise an error with invalid input'

    it 'should raise an error with invalid output'

  end
    "

    jack_double_default_methods << "
  def #{method} input
    {}
  end
    "

    jack_double_badoutput_methods << "
  def #{method} input
    nil
  end
    "
    end

    output = %Q{require 'obvious'

class #{k}Contract < Obvious::Contract
  #{contract_defs}
end
}

    snake_name = k.gsub(/(.)([A-Z])/,'\1_\2').downcase

    filename = "#{@app.dir}/contracts/#{snake_name}_contract.rb"
    File.open(filename, 'w') {|f| f.write(output) }

    output = %Q{require_relative '../../contracts/#{snake_name}_contract'

describe #{k}Contract do
  #{method_specs}
end
}

    filename = "#{@app.dir}/spec/contracts/#{snake_name}_spec.rb"
    File.open(filename, 'w') {|f| f.write(output) }

    output = %Q{require_relative '../../contracts/#{snake_name}_contract'

class #{k}Double
  def self.create behavior
    case behavior
    when :bad_output
      #{k}_BadOutput.new
    when :default
      #{k}_Default.new
    end
  end
end

class #{k}_Default < #{k}Contract
  #{jack_double_default_methods}
end

class #{k}_BadOutput < #{k}Contract
  #{jack_double_badoutput_methods}
end
}

    filename = "#{@app.dir}/spec/doubles/#{snake_name}_double.rb"
    File.open(filename, 'w') {|f| f.write(output) }

  end
end