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



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

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



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

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



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

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
37
# 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/#{file_name}_spec.rb"
    template "spec/fixtures/namespace/model.yml",
             "spec/fixtures/#{identifier}/#{file_name}.yml"
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