Class: ExposeAssociation::AssociationExposer
- Inherits:
-
Object
- Object
- ExposeAssociation::AssociationExposer
- Defined in:
- lib/expose_association/base.rb
Instance Attribute Summary collapse
-
#association_class ⇒ Object
readonly
Returns the value of attribute association_class.
-
#association_name ⇒ Object
readonly
Returns the value of attribute association_name.
Instance Method Summary collapse
- #association_attributes ⇒ Object
- #association_setup_method ⇒ Object
- #attr_name_for(name, prefix) ⇒ Object
- #expose(*attributes) ⇒ Object
- #expose_attribute(name, options = {}) ⇒ Object
-
#initialize(klass, association_name, options = {}) ⇒ AssociationExposer
constructor
A new instance of AssociationExposer.
- #prefix_for(prefix_option) ⇒ Object
- #setup_method_prefix ⇒ Object
Constructor Details
#initialize(klass, association_name, options = {}) ⇒ AssociationExposer
Returns a new instance of AssociationExposer.
27 28 29 30 31 32 33 |
# File 'lib/expose_association/base.rb', line 27 def initialize klass, association_name, = {} @klass = klass [:class] ||= association_name.to_s.camelize.constantize @association_class = [:class] @association_name = association_name @options = end |
Instance Attribute Details
#association_class ⇒ Object (readonly)
Returns the value of attribute association_class.
25 26 27 |
# File 'lib/expose_association/base.rb', line 25 def association_class @association_class end |
#association_name ⇒ Object (readonly)
Returns the value of attribute association_name.
25 26 27 |
# File 'lib/expose_association/base.rb', line 25 def association_name @association_name end |
Instance Method Details
#association_attributes ⇒ Object
70 71 72 |
# File 'lib/expose_association/base.rb', line 70 def association_attributes "#{association_name}_attributes" end |
#association_setup_method ⇒ Object
74 75 76 |
# File 'lib/expose_association/base.rb', line 74 def association_setup_method "#{setup_method_prefix}_#{@association_name}" end |
#attr_name_for(name, prefix) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/expose_association/base.rb', line 50 def attr_name_for(name, prefix) case prefix when FalseClass then name.to_s when String, Symbol then "#{prefix.to_s}_#{name}" else "#{association_name}_#{name}" end end |
#expose(*attributes) ⇒ Object
35 36 37 38 39 40 |
# File 'lib/expose_association/base.rb', line 35 def expose *attributes = attributes.last.is_a?(Hash) ? attributes.pop : {} attributes.each do |attribute| expose_attribute attribute, end end |
#expose_attribute(name, options = {}) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/expose_association/base.rb', line 42 def expose_attribute name, = {} @klass.delegate name, "#{name}=", to: association_setup_method, prefix: prefix_for([:prefix]) @klass.attr_accessible attr_name_for(name, [:prefix]) end |
#prefix_for(prefix_option) ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'lib/expose_association/base.rb', line 60 def prefix_for(prefix_option) case prefix_option when TrueClass, NilClass then association_name.to_s when FalseClass then false when String, Symbol then prefix_option.to_s else # nil raise "Wrong value for prefix: #{prefix_option}" end end |
#setup_method_prefix ⇒ Object
78 79 80 |
# File 'lib/expose_association/base.rb', line 78 def setup_method_prefix @options.fetch(:setup_method_prefix, "setup") end |