class Hippo::Command::GenerateModel

Constants

RESERVED_YAML_KEYWORDS

Attributes

file_name[R]
table_name[R]

Public Instance Methods

add_autoload() click to toggle source
# 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_route() click to toggle source
# 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_client() click to toggle source
# 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_model() click to toggle source
# 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_migration() click to toggle source
# File lib/hippo/command/generate_model.rb, line 28
def generate_migration
    create_migration
end
set_variables() click to toggle source
Calls superclass method
# 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

Private Instance Methods

computed_namespace() click to toggle source
# File lib/hippo/command/generate_model.rb, line 60
def computed_namespace
    # find a file and directory with the same basename
    entries = Dir.glob(self.destination_root + "/lib/*")
    name = entries.detect{ |directory|
        FileTest.directory?(directory) && entries.detect{ |file|
            file == directory+".rb"
        }
    }
    name ? File.basename(name).to_s : nil
end
max_field_length() click to toggle source
# File lib/hippo/command/generate_model.rb, line 79
def max_field_length
    @max_field_length ||= fields.map{|field|
        field.name.length + ( field.reference? ? 3 : 0 )
    }.max
end
reference_fields() click to toggle source
# File lib/hippo/command/generate_model.rb, line 85
def reference_fields
    fields.select { |a| a.reference? }
end
yaml_key_value(key, value) click to toggle source
# File lib/hippo/command/generate_model.rb, line 71
def yaml_key_value(key, value)
    if RESERVED_YAML_KEYWORDS.include?(key.downcase)
        "'#{key}': #{value}"
    else
        "#{key}: #{value}"
    end
end