Class: Granite::Form::Model::Associations::Reflections::Base
- Inherits:
-
Object
- Object
- Granite::Form::Model::Associations::Reflections::Base
- Defined in:
- lib/granite/form/model/associations/reflections/base.rb
Direct Known Subclasses
Constant Summary collapse
- READ =
->(reflection, object) { object.read_attribute reflection.name }
- WRITE =
->(reflection, object, value) { object.write_attribute reflection.name, value }
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent_reflection ⇒ Object
AR compatibility.
Class Method Summary collapse
- .association_class ⇒ Object
- .build(target, generated_methods, name, options = {}, &_block) ⇒ Object
- .generate_methods(name, target) ⇒ Object
Instance Method Summary collapse
-
#belongs_to? ⇒ Boolean
AR compatibility.
- #build_association(object) ⇒ Object
- #collection? ⇒ Boolean
- #default(object) ⇒ Object
-
#initialize(name, options = {}) ⇒ Base
constructor
A new instance of Base.
- #klass ⇒ Object
- #macro ⇒ Object
- #read_source(object) ⇒ Object
- #write_source(object, value) ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Base
Returns a new instance of Base.
42 43 44 45 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 42 def initialize(name, = {}) @name = name.to_sym @options = end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
10 11 12 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 10 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
10 11 12 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 10 def @options end |
#parent_reflection ⇒ Object
AR compatibility
12 13 14 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 12 def parent_reflection @parent_reflection end |
Class Method Details
.association_class ⇒ Object
38 39 40 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 38 def self.association_class @association_class ||= "Granite::Form::Model::Associations::#{name.demodulize}".constantize end |
.build(target, generated_methods, name, options = {}, &_block) ⇒ Object
16 17 18 19 20 21 22 23 24 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 16 def self.build(target, generated_methods, name, = {}, &_block) generate_methods name, generated_methods if .delete(:validate) && target.respond_to?(:validates_nested) && !target.validates_nested?(name) target.validates_nested name end new(name, ) end |
.generate_methods(name, target) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 26 def self.generate_methods(name, target) target.class_eval <<-RUBY, __FILE__, __LINE__ + 1 def #{name} force_reload = false association(:#{name}).reader(force_reload) end def #{name}= value association(:#{name}).writer(value) end RUBY end |
Instance Method Details
#belongs_to? ⇒ Boolean
AR compatibility
56 57 58 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 56 def belongs_to? false end |
#build_association(object) ⇒ Object
60 61 62 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 60 def build_association(object) self.class.association_class.new object, self end |
#collection? ⇒ Boolean
85 86 87 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 85 def collection? true end |
#default(object) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 72 def default(object) defaultizer = [:default] if defaultizer.is_a?(Proc) if defaultizer.arity.nonzero? defaultizer.call(object) else object.instance_exec(&defaultizer) end else defaultizer end end |
#klass ⇒ Object
51 52 53 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 51 def klass @klass ||= ([:class_name].presence || name.to_s.classify).to_s.constantize end |
#macro ⇒ Object
47 48 49 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 47 def macro self.class.name.demodulize.underscore.to_sym end |
#read_source(object) ⇒ Object
64 65 66 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 64 def read_source(object) ([:read] || READ).call(self, object) end |
#write_source(object, value) ⇒ Object
68 69 70 |
# File 'lib/granite/form/model/associations/reflections/base.rb', line 68 def write_source(object, value) ([:write] || WRITE).call(self, object, value) end |