Class: Cleon::Services::CloneConcept

Inherits:
Service
  • Object
show all
Defined in:
lib/cleon/services/clone_concept.rb

Overview

Clone one concept

Direct Known Subclasses

CloneEntity, CloneGuard, CloneService

Constant Summary

Constants included from ArGuards

ArGuards::GuardModel

Instance Method Summary collapse

Methods inherited from Service

call

Constructor Details

#initialize(model) ⇒ CloneConcept

Returns a new instance of CloneConcept.

Parameters:

  • model (String)

    'thing para para:guard'



13
14
15
16
17
18
# File 'lib/cleon/services/clone_concept.rb', line 13

def initialize(model)
  @home = Home.new
  Cleon.error!("It cannot be done outside a Cleon's gem") unless @home.home?
  @thing = Model.new(model.split(' '))
  @model = Decor.new(@thing, @home.const)
end

Instance Method Details

#callObject



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cleon/services/clone_concept.rb', line 20

def call
  @log = []
  builder = renderer(config[:code_erb])
  content = builder.result(binding)
  write_file(File.join(config[:code_dir], @model.source), content)
  do_require(@model.source, config[:include]) if config[:include]

  builder = renderer(config[:spec_erb])
  content = builder.result(binding)
  write_file(File.join(config[:spec_dir], @model.spec), content)

  @log
end

#configHash

return configuration, must be provided in subclasses

Returns:

  • (Hash)

    of keys code_erb, code_dir, spec_erb, spec_dir, inlcude



60
61
62
63
64
65
66
67
68
# File 'lib/cleon/services/clone_concept.rb', line 60

def config
  {
    # code_erb: '',
    # code_dir: '',
    # spec_erb: '',
    # spec_dir: '',
    # include:  ''
  }
end

#do_require(name, include) ⇒ Object

require :include into :target



35
36
37
38
39
40
41
# File 'lib/cleon/services/clone_concept.rb', line 35

def do_require(name, include)
  incl = name.sub(/.rb\z/, '')
  prfx = include =~ /services/ ? 'services' : 'entities'
  excerpt = "require_relative '#{prfx}/#{incl}'"
  body = File.read(include) + "\n#{excerpt}"
  write_file(include, body)
end

#renderer(source) ⇒ Object

return renederer



53
54
55
56
# File 'lib/cleon/services/clone_concept.rb', line 53

def renderer(source)
  erb = File.read(source)
  ERB.new(erb, trim_mode: '-')
end

#write_file(name, content) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/cleon/services/clone_concept.rb', line 43

def write_file(name, content)
  if File.exist?(name)
    FileUtils.cp name, name + '~'
    @log << name + '~'
  end
  File.write(name, content)
  @log << name
end