Class: Crudboy::Bundle

Inherits:
Object show all
Defined in:
lib/crudboy/bundle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options, destination, context) ⇒ Bundle

Returns a new instance of Bundle.



48
49
50
51
52
53
54
55
# File 'lib/crudboy/bundle.rb', line 48

def initialize(path, options, destination, context)
  @path = initialize_path(path)
  @destination = destination
  @options = parse_options(options)
  @context = context
  @context.bundle_options = @options
  @templates = initialize_templates
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



7
8
9
# File 'lib/crudboy/bundle.rb', line 7

def config
  @config
end

#contextObject

Returns the value of attribute context.



7
8
9
# File 'lib/crudboy/bundle.rb', line 7

def context
  @context
end

#destinationObject

Returns the value of attribute destination.



7
8
9
# File 'lib/crudboy/bundle.rb', line 7

def destination
  @destination
end

#pathObject

Returns the value of attribute path.



7
8
9
# File 'lib/crudboy/bundle.rb', line 7

def path
  @path
end

#resultObject

Returns the value of attribute result.



7
8
9
# File 'lib/crudboy/bundle.rb', line 7

def result
  @result
end

#templatesObject

Returns the value of attribute templates.



7
8
9
# File 'lib/crudboy/bundle.rb', line 7

def templates
  @templates
end

Instance Method Details

#initialize_path(path) ⇒ Object



9
10
11
12
# File 'lib/crudboy/bundle.rb', line 9

def initialize_path(path)
  # TODO
  path
end

#initialize_templatesObject



63
64
65
66
67
68
69
# File 'lib/crudboy/bundle.rb', line 63

def initialize_templates
  templates_path= File.join(@path, 'templates')
  Find.find(templates_path).map do |file_path|
    base_path = file_path.delete_prefix(templates_path)
    Template.new(file_path, base_path, @context)
  end
end

#load_initializer!Object



57
58
59
60
61
# File 'lib/crudboy/bundle.rb', line 57

def load_initializer!
  "#{@path}/init.rb".tap do |initializer_file|
    load(initializer_file) if File.exist?(initializer_file)
  end
end

#load_option_definitionsObject



42
43
44
45
46
# File 'lib/crudboy/bundle.rb', line 42

def load_option_definitions
  File.join(@path, "options.rb").try do |option_definitions_file|
    load(option_definitions_file)
  end
end

#parse_options(options) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/crudboy/bundle.rb', line 14

def parse_options(options)
  load_option_definitions
  return unless Object::const_defined?('TEMPLATE_OPTIONS')
  OpenStruct.new.tap do |result|

    TEMPLATE_OPTIONS.each do |definition|
      definition[:default].try do |default|
        result[definition[:name]] = default
      end
    end

    OptionParser.new do |opts|

      opts.banner = "Template Options:\n\n"

      TEMPLATE_OPTIONS.each do |definition|
        opts.on(*definition.values_at(:short, :long, :description)) do |value|
          result[definition[:name]] = value
        end
      end
      opts.on('', '--help', 'Prints this help') do
        puts opts
        exit
      end
    end.parse!(options)
  end
end

#render!Object



71
72
73
74
75
76
# File 'lib/crudboy/bundle.rb', line 71

def render!
  load_initializer!
  templates.each do |template|
    template.render!(@destination)
  end
end