Class: Bricks::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/generator/bricks/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Base

Returns a new instance of Base.



12
13
14
15
# File 'lib/active_admin/generator/bricks/base.rb', line 12

def initialize(context)
  @context = context
  @base_path = base_path
end

Instance Attribute Details

#base_pathObject (readonly)

Returns the value of attribute base_path.



6
7
8
# File 'lib/active_admin/generator/bricks/base.rb', line 6

def base_path
  @base_path
end

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/active_admin/generator/bricks/base.rb', line 6

def context
  @context
end

Instance Method Details

#apply?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_admin/generator/bricks/base.rb', line 28

def apply?
  true
end

#ask(question) ⇒ Object



52
53
54
# File 'lib/active_admin/generator/bricks/base.rb', line 52

def ask(question)
  @context.ask format(question)
end

#choose(question, choices) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/active_admin/generator/bricks/base.rb', line 56

def choose(question, choices)
  say question
  values = {}
  choices.each_with_index do |choice,i|
    values[(i + 1).to_s] = choice[1]
    say "#{i.next.to_s}) #{choice[0]}"
  end
  answer = ask("Enter your selection:") while !values.keys.include?(answer)
  values[answer]
end

#commit_all(message) ⇒ Object



32
33
34
35
# File 'lib/active_admin/generator/bricks/base.rb', line 32

def commit_all(message)
  git add: "-A ."
  git commit: "-m '#{message}'"
end

#format(text) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/active_admin/generator/bricks/base.rb', line 37

def format(text)
  string = ""
  if title
    string << "\033[1m\033[36m"
    string << title.to_s.rjust(10)
    string << "\033[0m  "
  end
  string << text
  string
end

#say(text) ⇒ Object



48
49
50
# File 'lib/active_admin/generator/bricks/base.rb', line 48

def say(text)
  @context.say format(text)
end

#template(source) ⇒ Object



21
22
23
24
25
26
# File 'lib/active_admin/generator/bricks/base.rb', line 21

def template(source)
  content = open(File.expand_path(context.find_in_source_paths(source.to_s))) {|input| input.binmode.read }
  context.copy_file source do
    ERB.new(content).result(binding)
  end
end

#titleObject



17
18
19
# File 'lib/active_admin/generator/bricks/base.rb', line 17

def title
  self.class.name
end

#yes?(question) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/active_admin/generator/bricks/base.rb', line 67

def yes?(question)
  answer = ask(question + " \033[33m(y/n)\033[0m")
  case answer.downcase
    when "yes", "y"
      true
    when "no", "n"
      false
    else
      yes?(question)
  end
end