Class: Hippo::Command::UpdateModel

Inherits:
NamedCommand
  • Object
show all
Defined in:
lib/hippo/command/update_model.rb

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

#namespaced_nameObject (readonly)

Returns the value of attribute namespaced_name.



8
9
10
# File 'lib/hippo/command/update_model.rb', line 8

def namespaced_name
  @namespaced_name
end

Instance Method Details

#read_classObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/hippo/command/update_model.rb', line 15

def read_class
    if name=~/::/
        (@namespace,@name) = name.split("::")
    else
        ext = Command.load_current_extension(raise_on_fail:true)
        @namespace = ext.identifier
    end

    namespaced_name = "#{namespace.classify}::#{class_name}"
    @klass = namespaced_name.safe_constantize
    if !@klass
        raise Thor::Error.new("#{namespaced_name} was not found")
    end
    @file = Pathname.new(client_dir).join("models/#{class_name}.coffee")
    unless @file.exist?
        raise Thor::Error.new("Model #{@file} doesn't exist")
    end
    Hippo::DB.establish_connection
end

#replace_associationsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hippo/command/update_model.rb', line 47

def replace_associations
    associations = associations_for(@klass)
    return unless associations.any?
    (section,indent) = read_indent('associations',optional: true)
    maxlen = associations.map{|field| field.name.length }.max
    contents = section || (
                   single_indent = @file.read[/\n(\h+)(\w+):/,1] || '    '
                   indent = single_indent*2
                   "\n#{single_indent}associations:\n"
                 )
    associations.each do | assoc |
        type = assoc.collection? ? 'collection' : 'model'
        contents << sprintf("#{indent}%-#{maxlen+2}s",assoc.name.to_s+':') + \
                    "{ #{type}: \"#{assoc.class_name.demodulize}\" }\n"
    end
    if section
        gsub_file @file, /associations\s*:\s*\n.*?\n(?!#{indent})/m, contents
    else
        gsub_file @file, /\n\z/m, contents
    end
end

#replace_propsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hippo/command/update_model.rb', line 35

def replace_props
    columns = columns_for(@klass)
    return unless columns.any? # could happen I guess?
    (props,indent) = read_indent('props')
    maxlen = columns.map{|field| field.name.length }.max
    contents = props
    columns.each do | column |
        contents << sprintf("#{indent}%-#{maxlen+1}s",column.name+':') + type_specification(column) + "\n"
    end
    gsub_file @file, /props\s*:\s*\n.*?\n(?!#{indent})/m, contents
end

#set_variablesObject



10
11
12
13
# File 'lib/hippo/command/update_model.rb', line 10

def set_variables
    super
    @file_name  = name.underscore
end