Class: Condo::Configuration

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

Constant Summary collapse

@@callbacks =
{
    # Must be defined by the including class:
    #:resident_id => proc
    #:bucket_name => proc
    #:upload_complete => proc
    #:destroy_upload => proc
    #
    :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 = filename.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
        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 = filepath.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
        filepath.gsub!(/[^\w\.\-\/]/, '_')        # replace all non alphanumeric or periods with underscore
        filepath
    },
    :select_residence => proc { |config, resident_id, upload|
        # Where config === ::Condo::Configuration
        # and resident_id is the result of the resident_id callback
        # upload will only be present if it already exists
        config.residencies[0]
    }
}

Class Method Summary collapse

Class Method Details

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

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/condo/configuration.rb', line 58

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



43
44
45
# File 'lib/condo/configuration.rb', line 43

def self.callbacks
    @@callbacks
end

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



89
90
91
# File 'lib/condo/configuration.rb', line 89

def self.dynamic_residence(name, options = {})
    return "Condo::Strata::#{name.to_s.camelize}".constantize.new(options)
end

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



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/condo/configuration.rb', line 77

def self.get_residence(name, options = {})
    name = name.to_sym
    namespace = (options[:namespace] || :global).to_sym
    location = (options[:location] || :default).to_sym

    if @@locations && @@locations[namespace] && @@locations[namespace][name] && @@locations[namespace][name][location]
        return @@locations[namespace][name][location]
    end

    nil
end

.residenciesObject



93
94
95
# File 'lib/condo/configuration.rb', line 93

def self.residencies
    @@residencies
end

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

Allows you to override default callback behaviour



48
49
50
51
52
53
54
55
# File 'lib/condo/configuration.rb', line 48

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