Class: Model

Inherits:
DomainElement show all
Includes:
ActiveSupport::CoreExtensions::String::Inflections, FileBase
Defined in:
lib/rails_elements.rb

Constant Summary

Constants included from FileBase

FileBase::Home

Instance Attribute Summary collapse

Attributes inherited from DomainElement

#file_loc, #name

Instance Method Summary collapse

Methods included from FileBase

#copy, #destroy, #exists?, #file_path, #has_location?, #home, #location, #make_dir, #mkdir, #move, #path, #reset_location, #set_home, #set_location, #write

Methods inherited from DomainElement

#insert_after

Constructor Details

#initialize(name) ⇒ Model

Returns a new instance of Model.



34
35
36
37
38
39
40
# File 'lib/rails_elements.rb', line 34

def initialize(name)
  @file_loc = "app/models/#{name.underscore}.rb"
  @associations=[]
  @attributes=[]
  @plugin_methods=[]
  super
end

Instance Attribute Details

#associationsObject

Returns the value of attribute associations.



32
33
34
# File 'lib/rails_elements.rb', line 32

def associations
  @associations
end

#attributesObject

Returns the value of attribute attributes.



32
33
34
# File 'lib/rails_elements.rb', line 32

def attributes
  @attributes
end

#methodsObject

Returns the value of attribute methods.



32
33
34
# File 'lib/rails_elements.rb', line 32

def methods
  @methods
end

#plugin_methodsObject

Returns the value of attribute plugin_methods.



32
33
34
# File 'lib/rails_elements.rb', line 32

def plugin_methods
  @plugin_methods
end

Instance Method Details

#attribute_stringObject



47
48
49
# File 'lib/rails_elements.rb', line 47

def attribute_string
  attributes.join ' '
end

#attrs(args = {}) ⇒ Object

defining types and names of attributes for a model



43
44
45
# File 'lib/rails_elements.rb', line 43

def attrs(args={}) 
  args.each { |ident, type| attributes << "#{ident}:#{type}" }
end

#belongs_to(what, args = nil) ⇒ Object



65
66
67
68
# File 'lib/rails_elements.rb', line 65

def belongs_to what, args=nil
  associations << "\tbelongs_to :#{what}"+ (args ?  ", #{args.inspect.gsub(/[{}]/, '')}" : '')
  attrs "#{what}_id".to_sym => 'integer'
end

#generateObject



87
88
89
90
# File 'lib/rails_elements.rb', line 87

def generate
  puts "\ngenerating #{name} model with specs"
  puts(`script/generate rspec_model #{name} #{attribute_string}\n`)
end

#has_many(what, args = nil) ⇒ Object



56
57
58
59
# File 'lib/rails_elements.rb', line 56

def has_many what, args=nil
  has_many(args[:through]) if args && args[:through]
  associations << "\thas_many :#{what}"+ (args ? ", #{args.inspect.gsub(/[{}]/, '')}" : '')
end

#has_one(what, args = nil) ⇒ Object



61
62
63
# File 'lib/rails_elements.rb', line 61

def has_one what, args=nil
  associations << "\thas_one :#{what}"+(args ? ", #{args.inspect.gsub(/[{}]/, '')}" : '')
end


70
71
72
# File 'lib/rails_elements.rb', line 70

def print_associations
  "#{associations.join("\n")}\n\n"
end


73
74
75
# File 'lib/rails_elements.rb', line 73

def print_plugin_methods
  "#{plugin_methods.join("\n")}\n\n"
end

#uses(what, *opts) ⇒ Object



51
52
53
54
# File 'lib/rails_elements.rb', line 51

def uses(what, *opts)
  opts = opts.first
  plugin_methods << "\t#{what.to_s} :#{opts[:on]}\n"
end

#write_associationsObject



82
83
84
85
# File 'lib/rails_elements.rb', line 82

def write_associations
  puts "#{name}: recording associations" unless associations.empty?
  insert_after(file_loc, /^class/, print_associations)
end

#write_plugin_methodsObject



77
78
79
80
# File 'lib/rails_elements.rb', line 77

def write_plugin_methods
  puts "#{name}: preparing plugins for use" unless plugin_methods.empty?
  insert_after(file_loc, /^$/, print_plugin_methods)
end