Class: Bulldog::Reflection::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/bulldog/reflection.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reflection) ⇒ Configuration

Returns a new instance of Configuration.



111
112
113
# File 'lib/bulldog/reflection.rb', line 111

def initialize(reflection)
  @reflection = reflection
end

Class Method Details

.configure(reflection, &block) ⇒ Object



107
108
109
# File 'lib/bulldog/reflection.rb', line 107

def self.configure(reflection, &block)
  new(reflection).instance_eval(&block)
end

Instance Method Details

#default_style(name) ⇒ Object



127
128
129
# File 'lib/bulldog/reflection.rb', line 127

def default_style(name)
  @reflection.default_style = name
end

#detect_type_by(detector = nil, &block) ⇒ Object

Specify a procedure to run to determine the type of the attachment.

Pass either:

* A symbol argument, which names a named type detector.  Use
 +Bulldog.to_detect_type_by+ to register custom named type
 detectors.

* A block, which takes the record, attribute name, and
  Stream being assigned, and returns the attachment type to
  use as a Symbol.

* A callable object, e.g. a Proc or BoundMethod, which has
  the same signature as the block above.


202
203
204
205
206
207
# File 'lib/bulldog/reflection.rb', line 202

def detect_type_by(detector=nil, &block)
  detector && block and
    raise ArgumentError, "cannot pass argument and a block"
  @reflection.type = nil
  @reflection.type_detector = detector || block
end

#path(path_template) ⇒ Object



115
116
117
# File 'lib/bulldog/reflection.rb', line 115

def path(path_template)
  @reflection.path_template = path_template
end

#process(options = {}, &callback) ⇒ Object

Register the callback to fire for the given types of attachments.

Options:

* :types - A list of attachment types to process on.  If
  nil, process on any type.
* :on - The name of the event to run the processor on.
* :after - Same as prepending 'after_' to the given event.
* :before - Same as prepending 'before_' to the given event.
* :with - Use the given processor type.  If nil (the
  default), use the default type for the attachment.


145
146
147
148
149
150
151
152
# File 'lib/bulldog/reflection.rb', line 145

def process(options={}, &callback)
  event_name = event_name(options)
  types = Array(options[:types]) if options[:types]
  @reflection.events[event_name] << Event.new(:processor_type => options[:with],
                                              :attachment_types => types,
                                              :styles => options[:styles],
                                              :callback => callback)
end

#process_once(options = {}, &callback) ⇒ Object



154
155
156
157
158
159
160
161
# File 'lib/bulldog/reflection.rb', line 154

def process_once(options={}, &callback)
  options[:with] and
    raise ArgumentError, "cannot specify a processor (:with option) with #process_once"
  options[:styles] and
    raise ArgumentError, "no :styles available for #process_once"
  options[:with] = :one_shot
  process(options, &callback)
end

#store_attributes(*args) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/bulldog/reflection.rb', line 163

def store_attributes(*args)
  stored_attributes = args.extract_options!.symbolize_keys
  args.each do |attribute|
    stored_attributes[attribute] = :"#{@reflection.name}_#{attribute}"
  end
  @reflection.stored_attributes = stored_attributes
end

#style(name, attributes = {}) ⇒ Object



123
124
125
# File 'lib/bulldog/reflection.rb', line 123

def style(name, attributes={})
  @reflection.styles << Style.new(name, attributes)
end

#type(type) ⇒ Object

Always use the given attachment type for this attachment.

This is equivalent to:

detect_type_by do
  type if stream
end


180
181
182
183
# File 'lib/bulldog/reflection.rb', line 180

def type(type)
  @reflection.type = type
  @reflection.type_detector = nil
end

#url(url_template) ⇒ Object



119
120
121
# File 'lib/bulldog/reflection.rb', line 119

def url(url_template)
  @reflection.url_template = url_template
end