Class: Specify::Service::ViewLoader
- Defined in:
- lib/specify/services/view_loader.rb
Overview
ViewLoaders will upload .views.xml files to a Specify::Database.
Instance Attribute Summary collapse
-
#target ⇒ Object
The target to which the ViewLoader uploads .views.xml files; an instance of Specify::Model::Discipline, Specify::Model::Collection, Specify::UserType, or Specify::Model::User.
Attributes inherited from Service
#agent, #collection, #discipline, #division, #session
Class Method Summary collapse
-
.from_branch(config:, path: nil, name: nil) ⇒ Object
Creates a new instance from a branch
name, ifnameconforms to the naming convention database_name/collection_name/level, where level is discipline, collection, manager, fullaccess, limitedaccess, or guest), or user/username. -
.user_target(hash) ⇒ Object
Returns the Specify::Model::User instance for
hashifhashhas the key:userand the value for that key is an existing Specify::Model::User#name. -
.user_type_target(hash) ⇒ Object
Returns the Specify::UserType for
hashif hash has the keyuser_type, and the value for that key is a valid Specify::UserType#name.
Instance Method Summary collapse
-
#import(file) ⇒ Object
Loads the views.xml
fileand stores it as a SQL blob in the Specify::Model::ViewSetObject for the #target. -
#initialize(host:, database:, collection:, specify_user: nil, level: nil, config:) ⇒ ViewLoader
constructor
Returns a new ViewLoader.
-
#inspect ⇒ Object
Creates a string representation of
self. -
#personal? ⇒ Boolean
Returns
trueif the view is personal,falseotherwise. -
#view_collection ⇒ Object
Returns the Specify::Model::Collection that views.xml files will be uploaded to (all supported upload levels except
:disciplinare scoped to a collection. Will be #collection ornil.. -
#view_discipline ⇒ Object
Returns the Specify::Model::Discipline that views.xml files will be uploaded to.
-
#view_level ⇒ Object
Returns
2if the view is for a collection,0otherwise. -
#view_type ⇒ Object
Returns the Specify::Model::Discipline#name, which is the Specify::Model::AppResourceDir#discipline_type.
-
#view_user ⇒ Object
Returns the Specify::Model::User that views.xml files will be uploaded to.
-
#view_user_type ⇒ Object
Returns a String with a valid Specify::UserType#name for #target.
Methods inherited from Service
Constructor Details
#initialize(host:, database:, collection:, specify_user: nil, level: nil, config:) ⇒ ViewLoader
Returns a new ViewLoader.
level is the level to which the .views.xml file will be uploaded. valid values are :discipline, :collection, { :user_type => String } (String Symbol must be a valid Specify::UserType#name) or { :user => String } (where String must be an existing Specify::Model::User#user name).
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/specify/services/view_loader.rb', line 36 def initialize(host:, database:, collection:, specify_user: nil, level: nil, config:) super(host: host, database: database, collection: collection, specify_user: specify_user, config: config) @target = nil self.target = level end |
Instance Attribute Details
#target ⇒ Object
The target to which the ViewLoader uploads .views.xml files; an instance of Specify::Model::Discipline, Specify::Model::Collection, Specify::UserType, or Specify::Model::User
10 11 12 |
# File 'lib/specify/services/view_loader.rb', line 10 def target @target end |
Class Method Details
.from_branch(config:, path: nil, name: nil) ⇒ Object
Creates a new instance from a branch name, if name conforms to the naming convention database_name/collection_name/level, where level is discipline, collection, manager, fullaccess, limitedaccess, or guest), or user/username.
path is the filepath for the view file to be uploaded.
config is the YAML file containing the database configuration.
20 21 22 23 24 25 26 27 |
# File 'lib/specify/services/view_loader.rb', line 20 def self.from_branch(config:, path: nil, name: nil) parser = if name BranchParser.new(path, name, config) else BranchParser.current_branch config end new parser.to_h.merge(config: config) end |
.user_target(hash) ⇒ Object
Returns the Specify::Model::User instance for hash if hash has the key :user and the value for that key is an existing Specify::Model::User#name.
54 55 56 57 |
# File 'lib/specify/services/view_loader.rb', line 54 def self.user_target(hash) return unless hash.key? :user Model::User.first(Name: hash[:user]) end |
.user_type_target(hash) ⇒ Object
Returns the Specify::UserType for hash if hash has the key user_type, and the value for that key is a valid Specify::UserType#name.
62 63 64 65 |
# File 'lib/specify/services/view_loader.rb', line 62 def self.user_type_target(hash) return unless hash.key? :user_type UserType.new(hash[:user_type]) end |
Instance Method Details
#import(file) ⇒ Object
Loads the views.xml file and stores it as a SQL blob in the Specify::Model::ViewSetObject for the #target.
74 75 76 77 78 |
# File 'lib/specify/services/view_loader.rb', line 74 def import(file) view_set = @target.view_set(collection) || create_view_set(file) view_set.import(views_file(file)) end |
#inspect ⇒ Object
Creates a string representation of self.
68 69 70 |
# File 'lib/specify/services/view_loader.rb', line 68 def inspect "#{self} database: #{@db.database}, target: #{@target}" end |
#personal? ⇒ Boolean
Returns true if the view is personal, false otherwise.
81 82 83 |
# File 'lib/specify/services/view_loader.rb', line 81 def personal? @target.is_a?(Model::User) ? true : false end |
#view_collection ⇒ Object
Returns the Specify::Model::Collection that views.xml files will be uploaded to (all supported upload levels except :disciplin are scoped to a collection. Will be #collection or nil.
105 106 107 |
# File 'lib/specify/services/view_loader.rb', line 105 def view_collection collection unless @target.is_a? Model::Discipline end |
#view_discipline ⇒ Object
Returns the Specify::Model::Discipline that views.xml files will be uploaded to.
111 112 113 |
# File 'lib/specify/services/view_loader.rb', line 111 def view_discipline discipline end |
#view_level ⇒ Object
Returns 2 if the view is for a collection, 0 otherwise.
116 117 118 |
# File 'lib/specify/services/view_loader.rb', line 116 def view_level @target.is_a?(Model::Collection) ? 2 : 0 end |
#view_type ⇒ Object
Returns the Specify::Model::Discipline#name, which is the Specify::Model::AppResourceDir#discipline_type.
122 123 124 |
# File 'lib/specify/services/view_loader.rb', line 122 def view_type view_discipline.name end |
#view_user ⇒ Object
Returns the Specify::Model::User that views.xml files will be uploaded to. Same as #session Specify::Session#user unless the view is for a different user.
129 130 131 |
# File 'lib/specify/services/view_loader.rb', line 129 def view_user @target.is_a?(Model::User) ? @target : @session.user end |
#view_user_type ⇒ Object
Returns a String with a valid Specify::UserType#name for #target.
134 135 136 137 138 139 140 141 |
# File 'lib/specify/services/view_loader.rb', line 134 def view_user_type case @target when UserType @target.name.to_s when Model::User @target.UserType.downcase end end |