Class: Condo::Configuration

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/condo/configuration.rb

Constant Summary collapse

@@callbacks =
{
    #:resident_id        # Must be defined by the including class
    :bucket_name => proc {"#{Rails.application.class.parent_name}#{instance_eval @@callbacks[:resident_id]}"},
    :object_key => proc { |upload|
        if upload[:file_path]
            upload[:file_path] + upload[:file_name]
        else
            upload[:file_name]
        end
    },
    :object_options => proc { |upload|
        {:permissions => :private}
    },
    :pre_validation => proc { |upload|
        true
    },    # To respond with errors use: lambda {return false, {:errors => {:param_name => 'wtf are you doing?'}}}
    :sanitize_filename => proc { |filename|
        filename.gsub!(/^.*(\\|\/)/, '')    # get only the filename (just in case)
        filename.gsub!(/[^\w\.\-]/, '_')     # replace all non alphanumeric or periods with underscore
        filename
    },
    :sanitize_filepath => proc { |filepath|
        filepath.gsub!(/[^\w\.\-\/]/, '_')        # replace all non alphanumeric or periods with underscore
        filepath
    }
    #:upload_complete    # Must be defined by the including class
    #:destroy_upload    # the actual delete should be done by the application
    #:dynamic_residence    # If the data stores are dynamically stored by the application
}
@@dynamics =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_residence(name, options = {}) ⇒ Object

Allows for predefined storage providers (maybe you only use Amazon?)



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/condo/configuration.rb', line 76

def self.add_residence(name, options = {})
    @@residencies ||= []
    @@residencies << ("Condo::Strata::#{name.to_s.camelize}".constantize.new(options)).tap do |res|
        name = name.to_sym
        namespace = (options[:namespace] || :global).to_sym
        
        @@locations ||= {}
        @@locations[namespace] ||= {}
        @@locations[namespace][name] ||= {}
        
        if options[:location].present?
            @@locations[namespace][name][options[:location].to_sym] = res
        else
            @@locations[namespace][name][:default] = res
            @@locations[namespace][name][res.location.to_sym] = res
        end
    end
end

.callbacksObject



40
41
42
# File 'lib/condo/configuration.rb', line 40

def self.callbacks
    @@callbacks
end

.set_callback(name, callback = nil, &block) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/condo/configuration.rb', line 44

def self.set_callback(name, callback = nil, &block)
    callback ||= block
    if callback.respond_to?(:call)
        @@callbacks[name.to_sym] = callback
    else
        raise ArgumentError, 'No callback provided'
    end
end

.set_dynamic_provider(namespace, callback = nil, &block) ⇒ Object

Provides a callback whenever attempting to select a provider for the current request

> Allows multiple providers for different users / controllers or dynamic providers



58
59
60
61
62
63
64
65
# File 'lib/condo/configuration.rb', line 58

def self.set_dynamic_provider(namespace, callback = nil, &block)
    callback ||= block
    if callback.respond_to?(:call)
        @@callbacks[name.to_sym] = callback
    else
        raise ArgumentError, 'No callback provided'
    end
end

Instance Method Details

#dynamic_provider_present?(namespace) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
# File 'lib/condo/configuration.rb', line 67

def dynamic_provider_present?(namespace)
    return false if @@dynamics.nil? || @@dynamics[namespace.to_sym].nil?
    true
end

#residenciesObject



96
97
98
# File 'lib/condo/configuration.rb', line 96

def residencies
    @@residencies
end

#set_residence(name, options) ⇒ Object

Storage provider selection routine

> pass in :dynamic => true with :name and connection options to create a new instance



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/condo/configuration.rb', line 105

def set_residence(name, options)
    if options[:namespace].present? && dynamic_provider_present?(options[:namespace])
        if options[:upload].present?
            upload = options[:upload]
            params = {
                :user_id => upload.user_id,
                :file_name => upload.file_name,
                :file_size => upload.file_size,
                :provider_name => upload.provider_name,
                :provider_location => upload.provider_location,
                :provider_namespace => upload.provider_namespace
            }
            return instance_exec params, &@@dynamics[upload.provider_namespace]
        else
            params = {
                :user_id => options[:resident],
                :file_name => options[:params][:file_name],
                :file_size => options[:params][:file_size],
                :provider_namespace => options[:namespace]
            }
            return instance_exec params, &@@dynamics[options[:namespace]]
        end
    else
        if options[:dynamic]
            return "Condo::Strata::#{name.to_s.camelize}".constantize.new(options)
        else
            return options[:location].present? ? @@locations[:global][name.to_sym][options[:location].to_sym] : @@locations[:global][name.to_sym][:default]
        end
    end
end