Class: Cleon::Services::CloneGuard

Inherits:
CloneConcept show all
Defined in:
lib/cleon/services/clone_guard.rb

Overview

Clone argument guard

Constant Summary

Constants included from ArGuards

ArGuards::GuardModel

Instance Method Summary collapse

Methods inherited from CloneConcept

#config, #do_require, #initialize, #renderer, #write_file

Methods inherited from Service

call

Constructor Details

This class inherits a constructor from Cleon::Services::CloneConcept

Instance Method Details

#arguards_sourceObject


36
37
38
# File 'lib/cleon/services/clone_guard.rb', line 36

def arguards_source
  "lib/#{@home.base}/arguards.rb"
end

#arguards_specObject


40
41
42
# File 'lib/cleon/services/clone_guard.rb', line 40

def arguards_spec
  "test/#{@home.base}/arguards_spec.rb"
end

#callObject

add new arguard into arguards.rb at the bottom of ArGuards module


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cleon/services/clone_guard.rb', line 10

def call
  # Cleon.error! unless @home.home?
  # @thing = Model.new(model.split(' '))
  # @model = Decor.new(@thing, @home.const)
  @log = []
  source = File.read(arguards_source).lines
  source.insert(-3, guard_content)
  # it will write also copy into source.rb~
  write_file(arguards_source, source.join)

  write_file(arguards_spec, shared_spec) unless File.exist?(arguards_spec)
  source = File.read(arguards_spec)
  write_file(arguards_spec, source + spec_content)
  @log
end

#guard_contentObject


26
27
28
29
30
31
32
33
34
# File 'lib/cleon/services/clone_guard.rb', line 26

def guard_content
  <<~EOF.lines.map{|l| ' '*4 + l}
    Guard#{@model.const} = #{@model.root_const}::ArGuard.new(
      '#{@model.name.downcase}', 'must be #{@model.const}',
      Proc.new {|v|
        raise "provide spec for Guard#{@model.const} file: \#{__FILE__} line: \#{__LINE__}"})

  EOF
end

#shared_specObject


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cleon/services/clone_guard.rb', line 57

def shared_spec
  <<~EOF
    require_relative '../spec_helper'
    include #{@model.root_const}::ArGuards

    module SharedGuardSpecs
      extend Minitest::Spec::DSL

      # spec must provided the following variables:
      #   let(:guard) { GuardName }
      #   let(:valid) { ["name", :name] }
      #   let(:wrong) { [nil, 1, Object.new]}

      it 'must return value' do
        valid.each{|v| assert_equal v, guard.(v)}
      end

      it 'must raise ArgumentError' do
        wrong.each{|w| assert_raises(ArgumentError) { guard.(w) }}
      end
    end
  EOF
end

#spec_contentObject


44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cleon/services/clone_guard.rb', line 44

def spec_content
  <<~EOF

    describe Guard#{@model.const} do
      include SharedGuardSpecs

      let(:guard) { Guard#{@model.const} }
      let(:valid) { [nil, -1, 0, 1, "", "str", Object.new] }
      let(:wrong) { [nil, -1, 0, 1, "", "str", Object.new] }
    end
  EOF
end