aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/useflag.rb')
-rw-r--r--app/models/useflag.rb125
1 files changed, 44 insertions, 81 deletions
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