Permalink
Browse files

New model files for Automate GIT

  • Loading branch information...
mkanoor committed Aug 10, 2015
1 parent c4b5d04 commit 9c889c0269f25e3823dbae2b93b1120ea3e70538
@@ -0,0 +1,5 @@
class MiqAeBase
def self.abstract_class?
false
end
end
@@ -0,0 +1,14 @@
module MiqAeConstants
DOMAIN_YAML_FILE = '__domain__.yaml'
NAMESPACE_YAML_FILE = '__namespace__.yaml'
CLASS_YAML_FILE = '__class__.yaml'
METHODS_DIRECTORY = '__methods__'
CLASS_DIR_SUFFIX = '.class'
OBJ_YAML_VERSION = '1.0'
DOMAIN_OBJ_TYPE = 'domain'
NAMESPACE_OBJ_TYPE = 'namespace'
CLASS_OBJ_TYPE = 'class'
INSTANCE_OBJ_TYPE = 'instance'
METHOD_OBJ_TYPE = 'method'
CLASS_SCOPE_PREFIX = "$class$"
end
@@ -0,0 +1,114 @@
module MiqAeCount
extend ActiveSupport::Concern
module ClassMethods
def count
counts = Hash.new { |h, k| h[k] = 0 }
domain_list.each { |domain| stats(domain, counts) }
counts[self.to_s]
end
def stats(domain, counts )
repo = git_repository(domain)
list = repo.list_files
send("process_#{self.to_s.underscore}_count", repo, list, counts)
end
def process_miq_ae_field_count(repo, list, counts)
method_and_input_count(repo, list, counts, 'MiqAeField')
class_and_field_count(repo, list, counts, 'MiqAeField')
end
def process_miq_ae_value_count(repo, list, counts)
instance_and_value_count(repo, list, counts, 'MiqAeValue')
end
def process_miq_ae_class_count(repo, list, counts)
class_and_field_count(repo, list, counts, 'MiqAeClass')
end
def process_miq_ae_instance_count(repo, list, counts)
instance_and_value_count(repo, list, counts, 'MiqAeInstance')
end
def process_miq_ae_method_count(repo, list, counts)
method_and_input_count(repo, list, counts, 'MiqAeMethod')
end
def process_miq_ae_namespace_count(_repo, list, counts)
counts['MiqAeNamespace'] += namespace_list(list).count
end
def process_miq_ae_domain_count(_repo, list, counts)
counts['MiqAeDomain'] += list.select { |f| File.basename(f) == MiqAeFsStore::DOMAIN_YAML_FILE }.count
end
def domain_list(name = '*')
Dir.glob(File.join(MiqAeDatastore::DATASTORE_DIRECTORY, '*')).collect do |entry|
domain = File.basename(entry)
next unless File.fnmatch(name, domain, File::FNM_CASEFOLD)
domain
end.compact
end
def namespace_list(list, name = '*')
list.select do |f|
File.basename(f) == MiqAeFsStore::NAMESPACE_YAML_FILE &&
File.fnmatch(name, File.basename(File.dirname(f)), File::FNM_CASEFOLD)
end
end
def method_list(list, name = '*')
list.select do |f|
File.basename(File.dirname(f)) == MiqAeFsStore::METHODS_DIRECTORY &&
File.extname(f) == '.yaml' &&
File.fnmatch(name, File.basename(f).split('.')[0], File::FNM_CASEFOLD)
end
end
def class_list(list, name = '*')
list.select do |f|
File.basename(f) == MiqAeFsStore::CLASS_YAML_FILE &&
File.fnmatch(name, File.basename(File.dirname(f)).split('.')[0], File::FNM_CASEFOLD)
end
end
def instance_list(list, name = '*')
list.select do |f|
File.basename(File.dirname(f)).ends_with?(MiqAeFsStore::CLASS_DIR_SUFFIX) &&
File.extname(f) == '.yaml' && File.basename(f) != MiqAeFsStore::CLASS_YAML_FILE &&
File.fnmatch(name, File.basename(f).split('.')[0], File::FNM_CASEFOLD)
end
end
def method_and_input_count(repo, list, counts, class_type)
methods = method_list(list)
counts['MiqAeMethod'] += methods.count
return if class_type == 'MiqAeMethod'
methods.each do |m|
yaml_hash = YAML.load(repo.read_file(m))
counts['MiqAeField'] += yaml_hash['object']['inputs'].count
end
end
def instance_and_value_count(repo, list, counts, class_type)
instances = instance_list(list)
counts['MiqAeInstance'] += instances.count
return if class_type == 'MiqAeInstance'
instances.each do |i|
yaml_hash = YAML.load(repo.read_file(i))
counts['MiqAeValue'] += yaml_hash['object']['fields'].count
end
end
def class_and_field_count(repo, list, counts, class_type)
classes = class_list(list)
counts['MiqAeClass'] += classes.count
return if class_type == 'MiqAeClass'
classes.each do |c|
yaml_hash = YAML.load(repo.read_file(c))
counts['MiqAeField'] += yaml_hash['object']['schema'].count
end
end
end
end
@@ -0,0 +1,33 @@
module MiqAeDefault
extend ActiveSupport::Concern
module ClassMethods
def ae_default_value_for(name, hash)
@default_hash ||= {}
hash = {:value => hash} if hash.class != Hash
@default_hash[name] = hash
end
def default_attributes
@default_hash ||= {}
@default_hash.keys
end
def value(key)
@default_hash ||= {}
return nil unless @default_hash.key?(key)
@default_hash[key]
end
end
def ae_defaults
@attributes ||= {}
self.class.default_attributes.each do |k|
next if @attributes.key?(k)
hash = self.class.value(k)
value = hash[:value]
value = value.call if value.class == Proc
@attributes[k] = value
end
end
end
@@ -0,0 +1,87 @@
require 'parallel'
module MiqAeFindByName
extend ActiveSupport::Concern
module ClassMethods
def first(commit_sha_hash = {})
find_all_by_name('*', commit_sha_hash).first
end
def find_all_by_name(name, commit_sha_hash = {})
Parallel.map(domain_list, :in_threads => 0) do |domain|
send("find_all_#{self.to_s.underscore}_by_name", domain, name, commit_sha_hash[domain])
end.compact.flatten
end
def find_by_name(name, commit_sha_hash = {})
Parallel.map(domain_list, :in_threads => 0) do |domain|
send("find_#{self.to_s.underscore}_by_name", domain, name, commit_sha_hash[domain])
end.compact.flatten.first
end
def find_all_miq_ae_domain_by_name(domain, name, commit_sha = nil)
return [] unless File.fnmatch(name, domain, File::FNM_CASEFOLD)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
list.select { |f| File.basename(f) == MiqAeFsStore::DOMAIN_YAML_FILE }.collect do |f|
load_ae_entry(repo, repo.find_entry(f), MiqAeDomain)
end
end
def find_miq_ae_domain_by_name(domain, name, commit_sha = nil)
return nil unless File.fnmatch(name, domain, File::FNM_CASEFOLD)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
domain_file = list.detect { |f| f == "/#{MiqAeFsStore::DOMAIN_YAML_FILE}" }
load_ae_entry(repo, repo.find_entry(domain_file), MiqAeDomain) if domain_file
end
def find_all_miq_ae_namespace_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
namespace_list(list, name).collect { |f| load_ae_entry(repo, repo.find_entry(f), MiqAeNamespace) }
end
def find_miq_ae_namespace_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
namespace_file = namespace_list(list, name).first
load_ae_entry(repo, repo.find_entry(namespace_file), MiqAeNamespace) if namespace_file
end
def find_all_miq_ae_class_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
class_list(list, name).collect { |f| load_ae_entry(repo, repo.find_entry(f), MiqAeClass) }
end
def find_miq_ae_class_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
class_file = class_list(list, name).first
load_ae_entry(repo, repo.find_entry(class_file), MiqAeClass) if class_file
end
def find_all_miq_ae_instance_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
instance_list(list, name).collect { |f| load_ae_entry(repo, repo.find_entry(f), MiqAeInstance) }
end
def find_miq_ae_instance_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
instance_file = instance_list(list, name).first
load_ae_entry(repo, repo.find_entry(instance_file), MiqAeInstance) if instance_file
end
def find_all_miq_ae_method_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
method_list(list, name).collect { |f| load_ae_entry(repo, repo.find_entry(f), MiqAeMethod) }
end
def find_miq_ae_method_by_name(domain, name, commit_sha)
repo, list = fetch_file_list_from_repo(domain, commit_sha)
method_file = method_list(list, name).first
load_ae_entry(repo, repo.find_entry(method_file), MiqAeMethod) if method_file
end
def fetch_file_list_from_repo(domain, commit_sha)
repo = git_repository(domain)
return repo, repo.list_files(commit_sha)
end
end
end
Oops, something went wrong.

0 comments on commit 9c889c0

Please sign in to comment.