Class: Lanes::Command::UpdateModel
- Inherits:
-
NamedCommand
- Object
- Thor::Group
- NamedCommand
- Lanes::Command::UpdateModel
- Defined in:
- lib/lanes/command/update_model.rb
Instance Attribute Summary collapse
-
#namespaced_name ⇒ Object
readonly
Returns the value of attribute namespaced_name.
Attributes inherited from NamedCommand
#class_name, #client_dir, #identifier, #namespace, #spec_dir
Instance Method Summary collapse
Methods inherited from NamedCommand
#load_namespace, #set_variables, source_root
Instance Attribute Details
#namespaced_name ⇒ Object (readonly)
Returns the value of attribute namespaced_name.
8 9 10 |
# File 'lib/lanes/command/update_model.rb', line 8 def namespaced_name @namespaced_name end |
Instance Method Details
#read_class ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/lanes/command/update_model.rb', line 10 def read_class if name=~/::/ (@namespace,@name) = name.split("::") else ext = Command.load_current_extension(raise_on_fail:true) @namespace = ext.identifier end class_name = name.camelize 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 Lanes::DB.establish_connection end |
#replace_associations ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/lanes/command/update_model.rb', line 42 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_props ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/lanes/command/update_model.rb', line 30 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 |