Class: Bulldog::Reflection
- Inherits:
-
Object
- Object
- Bulldog::Reflection
- Defined in:
- lib/bulldog/reflection.rb
Defined Under Namespace
Classes: Configuration, Event
Instance Attribute Summary collapse
- #default_style ⇒ Object
-
#events ⇒ Object
Returns the value of attribute events.
-
#model_class ⇒ Object
Returns the value of attribute model_class.
-
#name ⇒ Object
Returns the value of attribute name.
-
#path_template ⇒ Object
Returns the value of attribute path_template.
-
#stored_attributes ⇒ Object
Returns the value of attribute stored_attributes.
-
#styles ⇒ Object
Returns the value of attribute styles.
-
#type ⇒ Object
Returns the value of attribute type.
-
#type_detector ⇒ Object
Returns the value of attribute type_detector.
-
#url_template ⇒ Object
Returns the value of attribute url_template.
Class Method Summary collapse
- .named_type_detectors ⇒ Object
- .reset_type_detectors ⇒ Object
- .to_detect_type_by(name, &block) ⇒ Object
Instance Method Summary collapse
-
#column_name_for_stored_attribute(attribute) ⇒ Object
Return the column name to use for the named storable attribute.
-
#configure(&block) ⇒ Object
Append the given block to this attachment’s configuration.
-
#detect_attachment_type(record, stream) ⇒ Object
Return the attachment type to use for the given record and stream.
-
#initialize(model_class, name) ⇒ Reflection
constructor
A new instance of Reflection.
- #initialize_copy(other) ⇒ Object
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_style ⇒ Object
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 |
#events ⇒ Object
Returns the value of attribute events.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def events @events end |
#model_class ⇒ Object
Returns the value of attribute model_class.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def model_class @model_class end |
#name ⇒ Object
Returns the value of attribute name.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def name @name end |
#path_template ⇒ Object
Returns the value of attribute path_template.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def path_template @path_template end |
#stored_attributes ⇒ Object
Returns the value of attribute stored_attributes.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def stored_attributes @stored_attributes end |
#styles ⇒ Object
Returns the value of attribute styles.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def styles @styles end |
#type ⇒ Object
Returns the value of attribute type.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def type @type end |
#type_detector ⇒ Object
Returns the value of attribute type_detector.
26 27 28 |
# File 'lib/bulldog/reflection.rb', line 26 def type_detector @type_detector end |
#url_template ⇒ Object
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_detectors ⇒ Object
68 69 70 |
# File 'lib/bulldog/reflection.rb', line 68 def self.named_type_detectors @named_type_detectors ||= {} end |
.reset_type_detectors ⇒ Object
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 (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 |