Class: Lanes::Command::GenerateModel

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

Constant Summary collapse

RESERVED_YAML_KEYWORDS =
%w(y yes n no true false on off 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

#file_nameObject (readonly)

Returns the value of attribute file_name.



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

def file_name
  @file_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#add_routeObject



48
49
50
51
52
# File 'lib/lanes/command/generate_model.rb', line 48

def add_route
    insert_into_file "config/routes.rb", after: /.*Lanes::API.routes.draw.*?\n/ do
        "    resources #{namespace}::#{class_name}\n"
    end
end

#create_clientObject



40
41
42
43
44
45
46
# File 'lib/lanes/command/generate_model.rb', line 40

def create_client
    self.fields.unshift ModelAttribute.parse("id:integer")
    template "client/models/Model.coffee",
             "#{client_dir}/models/#{class_name}.coffee"
    template "spec/client/models/ModelSpec.coffee",
             "#{spec_dir}/models/#{class_name}Spec.coffee"
end

#create_migrationObject



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

def create_migration
    migration = exising_migration ||
      migration_timestamp + "_create_#{table_name}.rb"
    self.fields = fields.map{ |field| ModelAttribute.parse(field) }
    template "db/create_table_migration.rb", "db/migrate/#{migration}"
end

#create_modelObject



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

def create_model
    template "lib/namespace/model.rb", "lib/#{identifier}/models/#{file_name}.rb"
    template "spec/server/model_spec.rb", "spec/server/#{file_name}_spec.rb"
    template "spec/fixtures/namespace/model.yml",
             "spec/fixtures/#{identifier}/#{file_name}.yml"
end

#set_variablesObject



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

def set_variables
    super
    @file_name  = name.underscore
    prefix = extension.db_table_prefix
    @table_name = prefix ? "#{prefix}_#{name.tableize}" : name.tableize
end