diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/category.rb | 41 | ||||
-rw-r--r-- | app/models/change.rb | 53 | ||||
-rw-r--r-- | app/models/package.rb | 80 | ||||
-rw-r--r-- | app/models/useflag.rb | 125 | ||||
-rw-r--r-- | app/models/version.rb | 67 |
5 files changed, 225 insertions, 141 deletions
diff --git a/app/models/category.rb b/app/models/category.rb index f629bde..4e361c1 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,12 +1,39 @@ class Category - include Elasticsearch::Persistence::Model - include Kkuleomi::Store::Model + include ActiveModel::Model + include ActiveModel::Validations - index_name "categories-#{Rails.env}" + ATTRIBUTES = [:id, + :created_at, + :updated_at, + :name, + :description, + :metadata_hash] + attr_accessor(*ATTRIBUTES) + attr_reader :attributes + + validates :name, presence: true + + def initialize(attr={}) + attr.each do |k,v| + if ATTRIBUTES.include?(k.to_sym) + send("#{k}=", v) + end + end + end + + def attributes + @id = @name + @created_at ||= DateTime.now + @updated_at = DateTime.now + ATTRIBUTES.inject({}) do |hash, attr| + if value = send(attr) + hash[attr] = value + end + hash + end + end + alias :to_hash :attributes - attribute :name, String, mapping: { type: 'keyword' } - attribute :description, String, mapping: { type: 'text' } - attribute :metadata_hash, String, mapping: { type: 'text' } # Determines if the document model needs an update from the repository model # @@ -29,7 +56,7 @@ class Category # @param [Portage::Repository::Category] category_model Input category model def import!(category_model) import(category_model) - save + CategoryRepository.save(self) end # Returns the URL parameter for referencing this package (Rails internal stuff) diff --git a/app/models/change.rb b/app/models/change.rb index 6eaf00c..1793da4 100644 --- a/app/models/change.rb +++ b/app/models/change.rb @@ -1,13 +1,48 @@ class Change - include Elasticsearch::Persistence::Model - include Kkuleomi::Store::Model + include ActiveModel::Model + include ActiveModel::Validations - index_name "change-#{Rails.env}" + ATTRIBUTES = [:_id, + :created_at, + :updated_at, + :package, + :category, + :change_type, + :version, + :arches, + :commit] + attr_accessor(*ATTRIBUTES) + attr_reader :attributes + + validates :package, presence: true + + def initialize(attr={}) + attr.each do |k,v| + if ATTRIBUTES.include?(k.to_sym) + send("#{k}=", v) + end + end + end + + def attributes + @created_at ||= DateTime.now + @updated_at = DateTime.now + ATTRIBUTES.inject({}) do |hash, attr| + if value = send(attr) + hash[attr] = value + end + hash + end + end + alias :to_hash :attributes + + # Converts the model to an OpenStruct instance + # + # @param [Array<Symbol>] fields Fields to export into the OpenStruct, or all fields if nil + # @return [OpenStruct] OpenStruct containing the selected fields + def to_os(*fields) + fields = all_fields if fields.empty? + OpenStruct.new(Hash[fields.map { |field| [field, send(field)] }]) + end - attribute :package, String, mapping: { type: 'keyword' } - attribute :category, String, mapping: { type: 'keyword' } - attribute :change_type, String, mapping: { type: 'keyword' } - attribute :version, String, mapping: { type: 'keyword' } - attribute :arches, String, mapping: { type: 'keyword' } - attribute :commit, Hash, default: {}, mapping: { type: 'object' } end diff --git a/app/models/package.rb b/app/models/package.rb index 7ad3cbe..11ef135 100644 --- a/app/models/package.rb +++ b/app/models/package.rb @@ -1,31 +1,52 @@ class Package - include Elasticsearch::Persistence::Model - include Kkuleomi::Store::Model + include ActiveModel::Model + include ActiveModel::Validations include Kkuleomi::Store::Models::PackageImport - include Kkuleomi::Store::Models::PackageSearch - - index_name "packages-#{Rails.env}" - - raw_fields = { - type: 'keyword' - } - - attribute :category, String, mapping: raw_fields - attribute :name, String, mapping: raw_fields - attribute :name_sort, String, mapping: raw_fields - attribute :atom, String, mapping: raw_fields - attribute :description, String, mapping: { type: 'text' } - attribute :longdescription, String, mapping: { type: 'text' } - attribute :homepage, String, default: [], mapping: raw_fields - attribute :license, String, mapping: raw_fields - attribute :licenses, String, default: [], mapping: raw_fields - attribute :herds, String, default: [], mapping: raw_fields - attribute :maintainers, Array, default: [], mapping: { type: 'object' } - attribute :useflags, Hash, default: {}, mapping: { type: 'object' } - attribute :metadata_hash, String, mapping: raw_fields + + ATTRIBUTES = [:id, + :created_at, + :updated_at, + :category, + :name, + :name_sort, + :atom, + :description, + :longdescription, + :homepage, + :license, + :licenses, + :herds, + :maintainers, + :useflags, + :metadata_hash] + attr_accessor(*ATTRIBUTES) + attr_reader :attributes + + validates :name, presence: true + + def initialize(attr={}) + attr.each do |k,v| + if ATTRIBUTES.include?(k.to_sym) + send("#{k}=", v) + end + end + end + + def attributes + @id = @atom + @created_at ||= DateTime.now + @updated_at = DateTime.now + ATTRIBUTES.inject({}) do |hash, attr| + if value = send(attr) + hash[attr] = value + end + hash + end + end + alias :to_hash :attributes def category_model - @category_model ||= Category.find_by(:name, category) + @category_model ||= CategoryRepository.find_by(:name, category) end def to_param @@ -44,7 +65,7 @@ class Package end def versions - @versions ||= Version.find_all_by(:package, atom, sort: { sort_key: { order: 'asc' } }) + @versions ||= VersionRepository.find_all_by(:package, atom, sort: { sort_key: { order: 'asc' } }) end def latest_version @@ -65,6 +86,15 @@ class Package maintainers.empty? && herds.empty? end + # Converts the model to an OpenStruct instance + # + # @param [Array<Symbol>] fields Fields to export into the OpenStruct, or all fields if nil + # @return [OpenStruct] OpenStruct containing the selected fields + def to_os(*fields) + fields = all_fields if fields.empty? + OpenStruct.new(Hash[fields.map { |field| [field, send(field)] }]) + end + private # Splits a license string into single licenses, stripping the permitted logic constructs diff --git a/app/models/useflag.rb b/app/models/useflag.rb index 131a89c..12758cb 100644 --- a/app/models/useflag.rb +++ b/app/models/useflag.rb @@ -1,14 +1,41 @@ class Useflag - include Elasticsearch::Persistence::Model - include Kkuleomi::Store::Model - - index_name "useflags-#{Rails.env}" + include ActiveModel::Model + include ActiveModel::Validations + + ATTRIBUTES = [:id, + :created_at, + :updated_at, + :name, + :description, + :atom, + :scope, + :use_expand_prefix] + attr_accessor(*ATTRIBUTES) + attr_reader :attributes + + validates :name, presence: true + + + def initialize(attr={}) + attr.each do |k,v| + if ATTRIBUTES.include?(k.to_sym) + send("#{k}=", v) + end + end + end - attribute :name, String, mapping: { type: 'keyword' } - attribute :description, String, mapping: { type: 'text' } - attribute :atom, String, mapping: { type: 'keyword' } - attribute :scope, String, mapping: { type: 'keyword' } - attribute :use_expand_prefix, String, mapping: { type: 'keyword' } + def attributes + @id = @name + '-' + (@atom || 'global' ) + '-' + @scope + @created_at ||= DateTime.now + @updated_at = DateTime.now + ATTRIBUTES.inject({}) do |hash, attr| + if value = send(attr) + hash[attr] = value + end + hash + end + end + alias :to_hash :attributes def all_fields [:name, :description, :atom, :scope, :use_expand_prefix] @@ -22,78 +49,14 @@ class Useflag name.gsub(use_expand_prefix + '_', '') end - class << self - # Retrieves all flags sorted by their state - def get_flags(name) - result = { local: {}, global: [], use_expand: [] } - - find_all_by(:name, name).each do |flag| - case flag.scope - when 'local' - result[:local][flag.atom] = flag - when 'global' - result[:global] << flag - when 'use_expand' - result[:use_expand] << flag - end - end - - result - end - - def suggest(q) - results = Useflag.search( - size: 20, - query: { match_phrase_prefix: { name: q } } - ) - - processed_results = {} - results.each do |result| - if processed_results.key? result.name - processed_results[result.name] = { - name: result.name, - description: '(multiple definitions)', - scope: 'multi' - } - else - processed_results[result.name] = result - end - end - - processed_results.values.sort { |a, b| a[:name].length <=> b[:name].length } - end - - # Loads the local USE flags for a given package in a name -> model hash - # - # @param [String] atom Package to find flags for - # @return [Hash] - def local_for(atom) - map_by_name find_all_by(:atom, atom) - end - - # Maps the global USE flags in the index by their name - # This is expensive! - # - def global - map_by_name find_all_by(:scope, 'global') - end - - # Maps the USE_EXPAND variables in the index by their name - # - def use_expand - map_by_name find_all_by(:scope, 'use_expand') - end - - private + # Converts the model to a Hash + # + # @param [Array<Symbol>] fields Fields to export into the Hash, or all fields if nil + # @return [Hash] Hash containing the selected fields + def to_hsh(*fields) + fields = all_fields if fields.empty? + Hash[fields.map { |field| [field, send(field)] }] + end - def map_by_name(collection) - map = {} - collection.each do |item| - map[item.name] = item - end - - map - end - end end diff --git a/app/models/version.rb b/app/models/version.rb index 62c72f8..3629b98 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -1,23 +1,52 @@ +require 'date' + class Version - include Elasticsearch::Persistence::Model - include Kkuleomi::Store::Model + include ActiveModel::Model + include ActiveModel::Validations include Kkuleomi::Store::Models::VersionImport - index_name "versions-#{Rails.env}" - - attribute :version, String, mapping: { type: 'keyword' } - attribute :package, String, mapping: { type: 'keyword' } - attribute :atom, String, mapping: { type: 'keyword' } - attribute :sort_key, Integer, mapping: { type: 'integer' } - attribute :slot, String, mapping: { type: 'keyword' } - attribute :subslot, String, mapping: { type: 'keyword' } - attribute :eapi, String, mapping: { type: 'keyword' } - attribute :keywords, String, mapping: { type: 'keyword' } - attribute :masks, Array, default: [], mapping: { type: 'object' } - attribute :use, String, default: [], mapping: { type: 'keyword' } - attribute :restrict, String, default: [], mapping: { type: 'keyword' } - attribute :properties, String, default: [], mapping: { type: 'keyword' } - attribute :metadata_hash, String, mapping: { type: 'keyword' } + ATTRIBUTES = [:id, + :created_at, + :updated_at, + :version, + :package, + :atom, + :sort_key, + :slot, + :subslot, + :eapi, + :keywords, + :masks, + :use, + :restrict, + :properties, + :metadata_hash] + attr_accessor(*ATTRIBUTES) + attr_reader :attributes + + validates :version, presence: true + + def initialize(attr={}) + attr.each do |k,v| + if ATTRIBUTES.include?(k.to_sym) + send("#{k}=", v) + end + end + end + + def attributes + @id = @atom + @created_at ||= DateTime.now + @updated_at = DateTime.now + + ATTRIBUTES.inject({}) do |hash, attr| + if value = send(attr) + hash[attr] = value + end + hash + end + end + alias :to_hash :attributes # Returns the keywording state on a given architecture # @@ -138,14 +167,14 @@ class Version def calc_useflags result = { local: {}, global: {}, use_expand: {} } - local_flag_map = Useflag.local_for(atom.gsub("-#{version}", '')) + local_flag_map = UseflagRepository.local_for(atom.gsub("-#{version}", '')) local_flags = local_flag_map.keys use.sort.each do |flag| if local_flags.include? flag result[:local][flag] = local_flag_map[flag].to_hsh else - useflag = Useflag.find_by(:name, flag) + useflag = UseflagRepository.find_by(:name, flag) # This should not happen, but let's be sure next unless useflag |