Module: GenericGrep::ClassMethods

Included in:
Common::ClassMethods, Generic_Table::ClassMethods
Defined in:
app/models/generic_grep.rb

Constant Summary collapse

ASSOCIATION_MACRO_LETTERS =

association_grep_command

'[has_manyoneblgtd]'
ASSOCIATION_MACRO_PATTERN =
"^[hb]#{ASSOCIATION_MACRO_LETTERS}*\s\s*"

Instance Method Summary collapse

Instance Method Details

#all_associationsObject

grep_all_associations_command


68
69
70
71
72
73
# File 'app/models/generic_grep.rb', line 68

def all_associations
	regexp='([a-z_.]*):('+ASSOCIATION_MACRO_PATTERN[1..-1]+')(.*)'
	return `#{grep_all_associations_command}`.split("\n").map do |l| 
		Regexp.new(regexp).match(l)[1..-1]
		end #map
end

#association_grep(model_regexp_string, association_name) ⇒ Object

association_macro_type


82
83
84
# File 'app/models/generic_grep.rb', line 82

def association_grep(model_regexp_string,association_name)
	return model_grep(association_grep_pattern(model_regexp_string,association_name))
end

#association_grep_pattern(model_regexp_string, association_name) ⇒ Object

model_grep


60
61
62
# File 'app/models/generic_grep.rb', line 60

def association_grep_pattern(model_regexp_string,association_name)
	return "#{model_regexp_string}:#{association_name}" # no end of line $, so that polymorphic associations are found.
end

#association_macro_type(association_name) ⇒ Object

all_associations


74
75
76
77
78
79
80
81
# File 'app/models/generic_grep.rb', line 74

def association_macro_type(association_name)
	hits=association_grep(ASSOCIATION_MACRO_PATTERN, association_name)
	if hits.empty? then
		return nil
	else
		return hits.match("(#{ASSOCIATION_MACRO_PATTERN})")[1].sub(/\s*$/, '').to_sym
	end #if
end

#association_type(association_name) ⇒ Object

concatenate association_arity and association_macro_type


96
97
98
# File 'app/models/generic_grep.rb', line 96

def association_type(association_name)
	return (association_arity(association_name).to_s+'_'+association_macro_type(association_name).to_s).to_sym
end

#belongs_to_association?(association_name) ⇒ Boolean

expects a singular association name

Returns:

  • (Boolean)

89
90
91
# File 'app/models/generic_grep.rb', line 89

def belongs_to_association?(association_name)
	return association_grep('^belongs_to ',association_name)!=''
end

#grep_all_associations_commandObject


65
66
67
# File 'app/models/generic_grep.rb', line 65

def grep_all_associations_command
	return grep_command(ASSOCIATION_MACRO_PATTERN, 'app/models/*.rb')
end

#grep_command(content_regexp_string, filename_regexp_string = '-r {app/models/,test/unit/}*.rb', redirection = '') ⇒ Object


44
45
46
47
48
49
50
# File 'app/models/generic_grep.rb', line 44

def grep_command(content_regexp_string, filename_regexp_string='-r {app/models/,test/unit/}*.rb', redirection='')
	if redirection.empty? then
		return "grep \"#{content_regexp_string}\" #{filename_regexp_string}"
	else
		return "grep \"#{content_regexp_string}\" #{filename_regexp_string} #{redirection}"
	end #if
end

#has_many_association?(association_name) ⇒ Boolean

association_grep

Returns:

  • (Boolean)

85
86
87
# File 'app/models/generic_grep.rb', line 85

def has_many_association?(association_name)
	return association_grep('has_many',association_name)
end

#has_one_association?(association_name) ⇒ Boolean

belongs_to_association

Returns:

  • (Boolean)

92
93
94
# File 'app/models/generic_grep.rb', line 92

def has_one_association?(association_name)
	return association_grep('^has_one',association_name)
end

#model_grep(model_regexp_string) ⇒ Object

model_grep_command


57
58
59
# File 'app/models/generic_grep.rb', line 57

def model_grep(model_regexp_string)
	return `#{model_grep_command(model_regexp_string)}`
end

#model_grep_command(model_regexp_string) ⇒ Object

grep_command


51
52
53
54
55
56
# File 'app/models/generic_grep.rb', line 51

def model_grep_command(model_regexp_string)
	if !Generic_Table.rails_MVC_class?(self.name) then
		raise "#{self.name}.model_grep only works on Rails MVC."
	end #if
	return "grep \"#{model_regexp_string}\" #{model_file_name} &>/dev/null"
end