class Lanes::Command::GenerateModel
Constants
- RESERVED_YAML_KEYWORDS
Attributes
file_name[R]
table_name[R]
Public Instance Methods
add_autoload()
click to toggle source
# File lib/lanes/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_route()
click to toggle source
# File lib/lanes/command/generate_model.rb, line 47 def add_route insert_into_file "config/routes.rb", after: /.*Lanes::API.routes.draw.*?\n/ do " resources #{namespace}::#{class_name}\n" end end
create_client()
click to toggle source
# File lib/lanes/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_model()
click to toggle source
# File lib/lanes/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_migration()
click to toggle source
# File lib/lanes/command/generate_model.rb, line 28 def generate_migration create_migration end
set_variables()
click to toggle source
Calls superclass method
# File lib/lanes/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/lanes/command/generate_model.rb, line 61 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/lanes/command/generate_model.rb, line 80 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/lanes/command/generate_model.rb, line 86 def reference_fields fields.select { |a| a.reference? } end
yaml_key_value(key, value)
click to toggle source
# File lib/lanes/command/generate_model.rb, line 72 def yaml_key_value(key, value) if RESERVED_YAML_KEYWORDS.include?(key.downcase) "'#{key}': #{value}" else "#{key}: #{value}" end end