Class: Hippo::Command::GenerateModel

Inherits:
NamedCommand
  • Object
show all
Includes:
MigrationSupport
Defined in:
lib/hippo/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 included from MigrationSupport

#create_migration

Methods inherited from NamedCommand

#load_namespace, source_root

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name


19
20
21
# File 'lib/hippo/command/generate_model.rb', line 19

def file_name
  @file_name
end

#table_nameObject (readonly)

Returns the value of attribute table_name


19
20
21
# File 'lib/hippo/command/generate_model.rb', line 19

def table_name
  @table_name
end

Instance Method Details

#add_autoloadObject


52
53
54
55
56
# File 'lib/hippo/command/generate_model.rb', line 52

def add_autoload
    insert_into_file "lib/#{identifier}/model.rb", before: /^end\n/ do
        "    autoload :#{class_name}, \"#{identifier}/models/#{file_name}\"\n"
    end
end

#add_routeObject


46
47
48
49
50
# File 'lib/hippo/command/generate_model.rb', line 46

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

#create_clientObject


38
39
40
41
42
43
44
# File 'lib/hippo/command/generate_model.rb', line 38

def create_client
    self.fields.unshift ModelAttribute.parse("id:integer")
    template "client/models/model.js",
             "#{client_dir}/models/#{file_name}.js"
    template "spec/client/models/model.spec.js",
             "#{spec_dir}/models/#{file_name}.spec.js"
end

#create_modelObject


32
33
34
35
36
# File 'lib/hippo/command/generate_model.rb', line 32

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

#generate_migrationObject


28
29
30
# File 'lib/hippo/command/generate_model.rb', line 28

def generate_migration
    create_migration
end

#set_variablesObject


21
22
23
24
25
26
# File 'lib/hippo/command/generate_model.rb', line 21

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