Module: Graphiti::GeneratorMixin

Included in:
ApiTestGenerator, InstallGenerator, ResourceGenerator, ResourceTestGenerator
Defined in:
lib/generators/graphiti/generator_mixin.rb

Instance Method Summary collapse

Instance Method Details

#actionsObject



28
29
30
# File 'lib/generators/graphiti/generator_mixin.rb', line 28

def actions
  @options["actions"] || %w[index show create update destroy]
end

#actions?(*methods) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/generators/graphiti/generator_mixin.rb', line 32

def actions?(*methods)
  methods.any? { |m| actions.include?(m) }
end

#api_namespaceObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/generators/graphiti/generator_mixin.rb', line 12

def api_namespace
  @api_namespace ||= begin
    ns = graphiti_config["namespace"]

    if ns.blank?
      ns = prompt \
        header: "What is your API namespace?",
        description: "This will be used as a route prefix, e.g. if you want the route '/books_api/v1/authors' your namespace would be '/books_api/v1'",
        default: "/api/v1"
      update_config!("namespace" => ns)
    end

    ns
  end
end

#graphiti_configObject



36
37
38
# File 'lib/generators/graphiti/generator_mixin.rb', line 36

def graphiti_config
  File.exist?(".graphiticfg.yml") ? YAML.load_file(".graphiticfg.yml") : {}
end

#id_or_rawidObject



45
46
47
# File 'lib/generators/graphiti/generator_mixin.rb', line 45

def id_or_rawid
  @options["rawid"] ? "rawid" : "id"
end

#prompt(header: nil, description: nil, default: nil) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/generators/graphiti/generator_mixin.rb', line 3

def prompt(header: nil, description: nil, default: nil)
  say(set_color("\n#{header}", :magenta, :bold)) if header
  say("\n#{description}") if description
  answer = ask(set_color("\n(default: #{default}):", :magenta, :bold))
  answer = default if answer.blank? && default != "nil"
  say(set_color("\nGot it!\n", :white, :bold))
  answer
end

#sort_raw_idsObject



49
50
51
52
# File 'lib/generators/graphiti/generator_mixin.rb', line 49

def sort_raw_ids
  return unless @options["rawid"]
  ".sort"
end

#sort_raw_ids_descendingObject



54
55
56
57
# File 'lib/generators/graphiti/generator_mixin.rb', line 54

def sort_raw_ids_descending
  return unless @options["rawid"]
  ".sort.reverse"
end

#update_config!(attrs) ⇒ Object



40
41
42
43
# File 'lib/generators/graphiti/generator_mixin.rb', line 40

def update_config!(attrs)
  config = graphiti_config.merge(attrs)
  File.open(".graphiticfg.yml", "w") { |f| f.write(config.to_yaml) }
end