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, #namespace

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.



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

def file_name
  @file_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name.



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

def table_name
  @table_name
end

Instance Method Details

#add_routeObject



50
51
52
53
54
# File 'lib/lanes/command/generate_model.rb', line 50

def add_route
    insert_into_file "config/routes.rb", before: "end" do
        "    resources #{namespace.camelize}::#{class_name}\n"
    end
end

#create_clientObject



43
44
45
46
47
48
# File 'lib/lanes/command/generate_model.rb', line 43

def create_client
    template "client/data/Model.coffee", \
             "#{client_dir}/data/#{class_name}.coffee"
    template "spec/client/data/ModelSpec.coffee", \
             "spec/client/data/#{class_name}Spec.coffee"
end

#create_migrationObject



31
32
33
34
35
# File 'lib/lanes/command/generate_model.rb', line 31

def create_migration
    migration = exising_migration || Time.now.utc.strftime("%Y%m%d%H%M%S") + "_create_#{table_name}.rb"
    self.fields = fields.map{ |field| ModelAttribute.parse(field) }
    template "db/create_table_migration.rb", "db/migrate/#{migration}"
end

#create_modelObject



37
38
39
40
41
# File 'lib/lanes/command/generate_model.rb', line 37

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

#set_variablesObject



20
21
22
23
24
25
26
27
28
29
# File 'lib/lanes/command/generate_model.rb', line 20

def set_variables
    super
    if name=~/::/
        (@namespace,@name) = name.split("::")
    else
        @namespace = computed_namespace
    end
    @file_name  = name.underscore
    @table_name = namespace + "_" + name.tableize
end