Class: NoBrainer::Reference
- Inherits:
-
SimpleDelegator
- Object
- SimpleDelegator
- NoBrainer::Reference
- Defined in:
- lib/no_brainer/types/reference.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Class Method Summary collapse
- .name ⇒ Object
- .nobrainer_cast_db_to_model(value) ⇒ Object
- .nobrainer_cast_model_to_db(value) ⇒ Object
- .nobrainer_cast_user_to_model(value) ⇒ Object
- .to(model_type = nil, &model_type_proc) ⇒ Object
Instance Method Summary collapse
- #__getobj__ ⇒ Object
- #__hasobj__ ⇒ Object
- #dup ⇒ Object (also: #deep_dup)
-
#initialize(id, object = nil) ⇒ Reference
constructor
A new instance of Reference.
- #inspect ⇒ Object
Constructor Details
#initialize(id, object = nil) ⇒ Reference
Returns a new instance of Reference.
52 53 54 55 |
# File 'lib/no_brainer/types/reference.rb', line 52 def initialize(id, object = nil) @id = id __setobj__(object) unless object.nil? end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
50 51 52 |
# File 'lib/no_brainer/types/reference.rb', line 50 def id @id end |
Class Method Details
.name ⇒ Object
44 45 46 47 48 |
# File 'lib/no_brainer/types/reference.rb', line 44 def self.name str = "Reference" str += "(#{model_type.name})" if respond_to?(:model_type) str end |
.nobrainer_cast_db_to_model(value) ⇒ Object
111 112 113 |
# File 'lib/no_brainer/types/reference.rb', line 111 def self.nobrainer_cast_db_to_model(value) value && new(value) end |
.nobrainer_cast_model_to_db(value) ⇒ Object
106 107 108 109 |
# File 'lib/no_brainer/types/reference.rb', line 106 def self.nobrainer_cast_model_to_db(value) value&.save! if value&.new_record? && self.autosave value&.id end |
.nobrainer_cast_user_to_model(value) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/no_brainer/types/reference.rb', line 90 def self.nobrainer_cast_user_to_model(value) case value when Reference unless value.class.model_type == model_type raise NoBrainer::Error::InvalidType, "Expected Reference to #{model_type}, got #{value}" end value when model_type new(value.id, value) when nil nil else raise NoBrainer::Error::InvalidType end end |
.to(model_type = nil, &model_type_proc) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/no_brainer/types/reference.rb', line 10 def self.to(model_type = nil, &model_type_proc) raise ArgumentError, "wrong number of arguments (given 2, expect 1)" if model_type && model_type_proc raise ArgumentError, "wrong number of arguments (given 0, expect 1)" unless model_type || model_type_proc if model_type.respond_to?(:call) model_type_proc = model_type model_type = nil end if model_type.const_defined?('Reference', !:inherited) return model_type.const_get('Reference', !:inherited) end ref_type = if model_type model_type = resolve_model_type(model_type) ::Class.new(Reference) { define_singleton_method(:model_type) { model_type } } else # lazy-load model class ::Class.new(Reference) { define_singleton_method(:model_type) { @model_type ||= resolve_model_type(model_type_proc) } } end model_type.const_set('Reference', ref_type) end |
Instance Method Details
#__getobj__ ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/no_brainer/types/reference.rb', line 69 def __getobj__ super do if @missing nil elsif @id model = self.class.model_type unless (obj = model.find?(@id)) @missing = true raise NoBrainer::Error::MissingAttribute, "#{model} :#{model.pk_name}=>#{@id.inspect} not found" end __setobj__(obj) elsif block_given? yield end end end |
#__hasobj__ ⇒ Object
86 87 88 |
# File 'lib/no_brainer/types/reference.rb', line 86 def __hasobj__ defined? @delegate_sd_obj end |
#dup ⇒ Object Also known as: deep_dup
64 65 66 |
# File 'lib/no_brainer/types/reference.rb', line 64 def dup self.class.new(@id) end |
#inspect ⇒ Object
57 58 59 60 61 62 |
# File 'lib/no_brainer/types/reference.rb', line 57 def inspect "#<*#{self.class.model_type} " + ( __hasobj__ ? __getobj__.inspectable_attributes.map { |k,v| "#{k}: #{v.inspect}" }.join(', ') : "#{self.class.model_type.table_config.primary_key}: #{id.inspect}" ) + ">" end |