Class: Lanes::Command::GenerateScreen

Inherits:
NamedCommand
  • Object
show all
Defined in:
lib/lanes/command/generate_screen.rb

Constant Summary collapse

OPTIONS =
{
    title:       '',
    description: '',
    icon:        '',
    group_id:    'system',
    model_class: '',
    namespace: nil
}
ROOT_EL_FUNC =
/rootComponent: \(viewport\) -> null/

Instance Attribute Summary collapse

Attributes inherited from NamedCommand

#class_name, #client_dir, #identifier, #namespace, #spec_dir

Instance Method Summary collapse

Methods inherited from NamedCommand

#load_namespace, source_root

Instance Attribute Details

#screen_classObject (readonly)

Returns the value of attribute screen_class.



17
18
19
# File 'lib/lanes/command/generate_screen.rb', line 17

def screen_class
  @screen_class
end

#screen_idObject (readonly)

Returns the value of attribute screen_id.



17
18
19
# File 'lib/lanes/command/generate_screen.rb', line 17

def screen_id
  @screen_id
end

Instance Method Details

#add_definitionObject



33
34
35
36
37
38
# File 'lib/lanes/command/generate_screen.rb', line 33

def add_definition
    insert_into_file "config/screens.rb", :after => /Lanes::Screen.for_extension.*?\n/ do
        source = File.expand_path(find_in_source_paths("config/screen.rb"))
        ERB.new(::File.binread(source), nil, "-","@output_buffer").result(binding)
    end
end

#create_screenObject



26
27
28
29
30
31
# File 'lib/lanes/command/generate_screen.rb', line 26

def create_screen
    template "client/screens/index.js",    "#{client_dir}/screens/#{screen_id}/index.js"
    template "client/screens/styles.scss", "#{client_dir}/screens/#{screen_id}/index.scss"
    template "client/screens/Screen.cjsx", "#{client_dir}/screens/#{screen_id}/#{class_name}.cjsx"
    template "spec/client/Screen.coffee",  "#{spec_dir}/screens/#{screen_id}/#{class_name}Spec.coffee"
end

#maybe_set_as_defaultObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/lanes/command/generate_screen.rb', line 42

def maybe_set_as_default
    path = "#{client_dir}/Extension.coffee"
    content = File.binread(path)
    return unless content.match(ROOT_EL_FUNC)
    content = "##=require ./screens/#{screen_id}\n\n" + content
    content.gsub! ROOT_EL_FUNC, <<-LOADFN.strip_heredoc.chomp
                  rootComponent: (viewport) ->
                          # render #{screen_class} by default.  If this is changed the
                          # ##=require ./screens/#{screen_id} at the top of file must also be updated
                          # to ensure that the correct screen's definition will be available at boot
                          #{screen_class}
        LOADFN
        File.open(path, "wb") { |file| file.write(content) }
end

#set_variablesObject



19
20
21
22
23
24
# File 'lib/lanes/command/generate_screen.rb', line 19

def set_variables
    super
    options[:title] = name.titleize if options[:title].blank?
    @screen_id = class_name.underscore.dasherize
    @screen_class = "#{namespace}.Screens.#{class_name}"
end