Class: Bulldog::Reflection

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

Defined Under Namespace

Classes: Configuration, Event

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class, name) ⇒ Reflection

Returns a new instance of Reflection.



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/bulldog/reflection.rb', line 3

def initialize(model_class, name)
  @model_class = model_class
  @name = name

  @path_template = nil
  @url_template = nil
  @styles = StyleSet.new
  @default_style = :original
  @stored_attributes = {}
  @events = Hash.new{|h,k| h[k] = []}
  @type = nil
  @type_detector = nil
end

Instance Attribute Details

#default_styleObject



39
40
41
42
43
# File 'lib/bulldog/reflection.rb', line 39

def default_style
  styles[@default_style] or
    raise Error, "invalid default_style: #{@default_style.inspect}"
  @default_style
end

#eventsObject

Returns the value of attribute events.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def events
  @events
end

#model_classObject

Returns the value of attribute model_class.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def model_class
  @model_class
end

#nameObject

Returns the value of attribute name.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def name
  @name
end

#path_templateObject

Returns the value of attribute path_template.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def path_template
  @path_template
end

#stored_attributesObject

Returns the value of attribute stored_attributes.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def stored_attributes
  @stored_attributes
end

#stylesObject

Returns the value of attribute styles.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def styles
  @styles
end

#typeObject

Returns the value of attribute type.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def type
  @type
end

#type_detectorObject

Returns the value of attribute type_detector.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def type_detector
  @type_detector
end

#url_templateObject

Returns the value of attribute url_template.



26
27
28
# File 'lib/bulldog/reflection.rb', line 26

def url_template
  @url_template
end

Class Method Details

.named_type_detectorsObject



68
69
70
# File 'lib/bulldog/reflection.rb', line 68

def self.named_type_detectors
  @named_type_detectors ||= {}
end

.reset_type_detectorsObject



76
77
78
# File 'lib/bulldog/reflection.rb', line 76

def self.reset_type_detectors
  named_type_detectors.clear
end

.to_detect_type_by(name, &block) ⇒ Object



72
73
74
# File 'lib/bulldog/reflection.rb', line 72

def self.to_detect_type_by(name, &block)
  named_type_detectors[name] = block
end

Instance Method Details

#column_name_for_stored_attribute(attribute) ⇒ Object

Return the column name to use for the named storable attribute.



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/bulldog/reflection.rb', line 56

def column_name_for_stored_attribute(attribute)
  if stored_attributes.fetch(attribute, :not_nil).nil?
    nil
  elsif (value = stored_attributes[attribute])
    value
  else
    default_column = "#{name}_#{attribute}"
    column_exists = model_class.columns_hash.key?(default_column)
    column_exists ? default_column.to_sym : nil
  end
end

#configure(&block) ⇒ Object

Append the given block to this attachment’s configuration.

Using this, you may specialize an attachment’s configuration in a piecemeal fashion; for subclassing, for example.



35
36
37
# File 'lib/bulldog/reflection.rb', line 35

def configure(&block)
  Configuration.configure(self, &block) if block
end

#detect_attachment_type(record, stream) ⇒ Object

Return the attachment type to use for the given record and stream.



97
98
99
100
101
102
103
104
# File 'lib/bulldog/reflection.rb', line 97

def detect_attachment_type(record, stream)
  return type if type
  detector = type_detector || :default
  if detector.is_a?(Symbol)
    detector = self.class.named_type_detectors[detector]
  end
  detector.call(record, name, stream)
end

#initialize_copy(other) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/bulldog/reflection.rb', line 17

def initialize_copy(other)
  super
  instance_variables.each do |name|
    value = instance_variable_get(name)
    value = value.clone if value.duplicable?
    instance_variable_set(name, value)
  end
end