<commit_before># Copyright (c) 2010 XAOP bvba
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# 
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

module ActiveSP
  
  class List < Base
    
    include InSite
    extend Caching
    extend PersistentCaching
    include Util
    
    # The containing site
    # @return [Site]
    attr_reader :site
    # The ID of the list
    # @return [String]
    attr_reader :id
    
    persistent { |site, id, *a| [site.connection, [:list, id]] }
    # @private
    def initialize(site, id, title = nil, attributes_before_type_cast1 = nil, attributes_before_type_cast2 = nil)
      @site, @id = site, id
      @Title = title if title
      @attributes_before_type_cast1 = attributes_before_type_cast1 if attributes_before_type_cast1
      @attributes_before_type_cast2 = attributes_before_type_cast2 if attributes_before_type_cast2
    end
    
    # The URL of the list
    # @return [String]
    def url
      # Dirty. Used to use RootFolder, but if you get the data from the bulk calls, RootFolder is the empty
      # string rather than what it should be. That's what you get with web services as an afterthought I guess.
      view_url = ::File.dirname(attributes["DefaultViewUrl"])
      result = URL(@site.url).join(view_url).to_s
      if ::File.basename(result) == "Forms" and dir = ::File.dirname(result) and dir.length > @site.url.length
        result = dir
      end
      result
    end
    cache :url
    
    # @private
    def relative_url
      @site.relative_url(url)
    end
    
    # See {Base#key}
    # @return [String]
    def key
      encode_key("L", [@site.key, @id])
    end
    
    # @private
    def Title
      data1["Title"].to_s
    end
    cache :Title
    
    # Yields the items in this list according to the given options. Note that this method does not
    # recurse into folders. I believe specifying a folder of '' actually does recurse
    # @param [Hash] options Options
    # @option options [Folder, :all] :folder (nil) The folder to search in
    # @option options [String] :query (nil) The query to execute as an XML fragment
    # @option options [Boolean] :no_preload (nil) If set to true, the attributes are not preloaded. Can be more efficient if you only need the list of items and not their attributes
    # @yieldparam [Item] item
    def each_item(options = {})
      options = options.dup
      folder = options.delete(:folder)
      query = options.delete(:query)
      query = query ? { "query" => query } : {}
      no_preload = options.delete(:no_preload)
      options.empty? or raise ArgumentError, "unknown options #{options.keys.map { |k| k.inspect }.join(", ")}"
      query_options = Builder::XmlMarkup.new.QueryOptions do |xml|
        xml.Folder(folder == :all ? "" : folder.url) if folder
      end
      if no_preload
        view_fields = Builder::XmlMarkup.new.ViewFields do |xml|<commit_msg>Fix for the fact that SharePoint won't find certain data when a query isn't specified<commit_after># Copyright (c) 2010 XAOP bvba
# 
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# 
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# 
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

module ActiveSP
  
  class List < Base
    
    include InSite
    extend Caching
    extend PersistentCaching
    include Util
    
    # The containing site
    # @return [Site]
    attr_reader :site
    # The ID of the list
    # @return [String]
    attr_reader :id
    
    persistent { |site, id, *a| [site.connection, [:list, id]] }
    # @private
    def initialize(site, id, title = nil, attributes_before_type_cast1 = nil, attributes_before_type_cast2 = nil)
      @site, @id = site, id
      @Title = title if title
      @attributes_before_type_cast1 = attributes_before_type_cast1 if attributes_before_type_cast1
      @attributes_before_type_cast2 = attributes_before_type_cast2 if attributes_before_type_cast2
    end
    
    # The URL of the list
    # @return [String]
    def url
      # Dirty. Used to use RootFolder, but if you get the data from the bulk calls, RootFolder is the empty
      # string rather than what it should be. That's what you get with web services as an afterthought I guess.
      view_url = ::File.dirname(attributes["DefaultViewUrl"])
      result = URL(@site.url).join(view_url).to_s
      if ::File.basename(result) == "Forms" and dir = ::File.dirname(result) and dir.length > @site.url.length
        result = dir
      end
      result
    end
    cache :url
    
    # @private
    def relative_url
      @site.relative_url(url)
    end
    
    # See {Base#key}
    # @return [String]
    def key
      encode_key("L", [@site.key, @id])
    end
    
    # @private
    def Title
      data1["Title"].to_s
    end
    cache :Title
    
    # Yields the items in this list according to the given options. Note that this method does not
    # recurse into folders. I believe specifying a folder of '' actually does recurse
    # @param [Hash] options Options
    # @option options [Folder, :all] :folder (nil) The folder to search in
    # @option options [String] :query (nil) The query to execute as an XML fragment
    # @option options [Boolean] :no_preload (nil) If set to true, the attributes are not preloaded. Can be more efficient if you only need the list of items and not their attributes
    # @yieldparam [Item] item
    def each_item(options = {})
      options = options.dup
      folder = options.delete(:folder)
      # Always include a query because for some reason SP is capable of not finding certain
      # items otherwise.
      query = { "query" => options.delete(:query) || "<Query><Where></Where></Query>" }
      no_preload = options.delete(:no_preload)
      options.empty? or raise ArgumentError, "unknown options #{options.keys.map { |k| k.inspect }.join(", ")}"
      query_options = Builder::XmlMarkup.new.QueryOptions do |xml|
        xml.Folder(folder == :all ? "" : folder.url) if folder
      end
      if no_preload
        view_fields = Builder::XmlMarkup.new.ViewFields do |xml|
{'fraction_non_alphanumeric': 0.08155046564309086, 'fraction_numerical': 0.0032720865844450037, 'mean_word_length': 3.173844537815126, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6233169', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2227, 'n_words': 928}