Class: KissShot::Spec

Inherits:
Object
  • Object
show all
Defined in:
lib/kiss_shot/spec.rb

Overview

This is the main class for code generation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata = {}) ⇒ KissShot::Spec

Create a new Spec

Parameters:

  • metadata (Hash) (defaults to: {})

    metadata



29
30
31
32
33
# File 'lib/kiss_shot/spec.rb', line 29

def initialize( = {})
  self. = 
  self.content  = ""
  self.line_prefix = ""
end

Instance Attribute Details

#contentString

Returns content.

Returns:

  • (String)

    content



10
11
12
# File 'lib/kiss_shot/spec.rb', line 10

def content
  @content
end

#line_prefixString

Returns line prefix, such as indent, comment etc.

Returns:

  • (String)

    line prefix, such as indent, comment etc.



12
13
14
# File 'lib/kiss_shot/spec.rb', line 12

def line_prefix
  @line_prefix
end

#metadataHash

Returns metadata.

Returns:

  • (Hash)

    metadata



8
9
10
# File 'lib/kiss_shot/spec.rb', line 8

def 
  
end

Class Method Details

.run(metadata = {}, script = nil, &block) ⇒ Object

Create a instance of anonymous subclass, and eval instance

Parameters:

  • metadata (Hash) (defaults to: {})

    metadata

  • script (String) (defaults to: nil)

    script to run

  • block (Block)

    block to invoke



18
19
20
21
22
23
# File 'lib/kiss_shot/spec.rb', line 18

def self.run( = {}, script = nil, &block)
  spec = Class.new(self).new()
  spec.instance_eval(script) unless script.nil?
  spec.instance_eval(&block) unless block.nil?
  spec.content
end

Instance Method Details

#indent_downKissShot::Spec

Indent left 2 whitespaces

Returns:



74
75
76
# File 'lib/kiss_shot/spec.rb', line 74

def indent_down
  pop_line_prefix "  "
end

#indent_upKissShot::Spec

Indent right 2 whitespaces

Returns:



67
68
69
# File 'lib/kiss_shot/spec.rb', line 67

def indent_up
  push_line_prefix "  "
end

#line(input = "") ⇒ KissShot::Spec

Apend line to content, indent prepended

Parameters:

  • input (String) (defaults to: "")

    content to append

Returns:



94
95
96
97
# File 'lib/kiss_shot/spec.rb', line 94

def line(input = "")
  raw input, true
  raw "\n"
end

#pop_line_prefix(prefix) ⇒ Object

Pop line prefix

Parameters:

  • prefix (String)

    line prefix to pop

Returns:

  • self



57
58
59
60
61
62
# File 'lib/kiss_shot/spec.rb', line 57

def pop_line_prefix(prefix)
  if self.line_prefix.end_with? prefix
    self.line_prefix = self.line_prefix[0, self.line_prefix.length - prefix.length]
  end
  self
end

#push_line_prefix(prefix) ⇒ Object

Push line prefix

Parameters:

  • prefix (String)

    line prefix to push

Returns:

  • self



48
49
50
51
# File 'lib/kiss_shot/spec.rb', line 48

def push_line_prefix(prefix)
  self.line_prefix += prefix
  self
end

#raw(input = "", use_line_prefix = false) ⇒ KissShot::Spec

Append raw string to content

Parameters:

  • input (String) (defaults to: "")

    content to append

  • use_line_prefix (true, false) (defaults to: false)

    whether prepend indent

Returns:



83
84
85
86
87
88
# File 'lib/kiss_shot/spec.rb', line 83

def raw(input = "", use_line_prefix = false)
  return unless input.length > 0
  self.content += self.line_prefix if use_line_prefix
  self.content += input.to_s
  self
end

#use(mod) ⇒ KissShot::Spec

Use a module, alias for ‘Class#extend`

Parameters:

  • mod (Module)

    module to extend

Returns:



39
40
41
42
# File 'lib/kiss_shot/spec.rb', line 39

def use(mod)
  self.extend mod
  self
end