Class: Alba::Association
- Inherits:
-
Object
- Object
- Alba::Association
- Defined in:
- lib/alba/association.rb,
sig/alba/association.rbs
Overview
Representing association
Class Attribute Summary collapse
-
.const_cache ⇒ Hash[Symbol | String, Class]
readonly
private
cache for
const_get.
Instance Attribute Summary collapse
-
#name ⇒ Symbol, String
readonly
Instance variables and methods.
Instance Method Summary collapse
- #constantize(resource) ⇒ Class private
-
#initialize(name:, modifier: nil, resource: nil, source: nil, with_traits: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block) ⇒ Association
constructor
private
A new instance of Association.
-
#key_transformation=(type) ⇒ void
private
This is the same API in
NestedAttribute. - #object_from(target, params) ⇒ Object private
- #resource_from(nesting, key_transformation, block, helper) ⇒ Alba::resource_class private
-
#to_h(target, within: nil, params: {}) ⇒ Hash
private
Recursively converts an object into a Hash.
- #to_h_with_constantize_resource(object, within, params) ⇒ generic_hash private
- #to_h_with_each_resource(object, within, params) ⇒ Array[generic_hash] private
Constructor Details
#initialize(name:, modifier: nil, resource: nil, source: nil, with_traits: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block) ⇒ Association
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Association.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/alba/association.rb', line 26 def initialize( name:, modifier: nil, resource: nil, source: nil, with_traits: nil, params: {}, nesting: nil, key_transformation: :none, helper: nil, &block ) @name = name @modifier = modifier @resource = resource || resource_from(nesting, key_transformation, block, helper) @source = source @with_traits = with_traits @params = params end |
Class Attribute Details
.const_cache ⇒ Hash[Symbol | String, Class] (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
cache for const_get
10 11 12 |
# File 'lib/alba/association.rb', line 10 def const_cache @const_cache end |
Instance Attribute Details
#name ⇒ Symbol, String (readonly)
Instance variables and methods
16 17 18 |
# File 'sig/alba/association.rbs', line 16 def name @name end |
Instance Method Details
#constantize(resource) ⇒ Class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/alba/association.rb', line 86 def constantize(resource) case resource when Class resource when Symbol, String self.class.const_cache.fetch(resource) do self.class.const_cache[resource] = Object.const_get(resource) end else raise Error, "Unexpected resource type: #{resource.class}" end end |
#key_transformation=(type) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
This is the same API in NestedAttribute
50 51 52 |
# File 'lib/alba/association.rb', line 50 def key_transformation=(type) @resource.transform_keys(type) unless @resource.is_a?(Proc) end |
#object_from(target, params) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
76 77 78 79 80 81 82 83 84 |
# File 'lib/alba/association.rb', line 76 def object_from(target, params) o = if @source target.instance_exec(params, &@source) else target.is_a?(Hash) ? target.fetch(@name) : target.__send__(@name) end o = @modifier.call(o, params, target) if @modifier o end |
#resource_from(nesting, key_transformation, block, helper) ⇒ Alba::resource_class
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
99 100 101 102 103 104 105 106 107 |
# File 'lib/alba/association.rb', line 99 def resource_from(nesting, key_transformation, block, helper) if block Alba.resource_class(helper: helper, key_transformation: key_transformation, &block) elsif Alba.inflector Alba.infer_resource_class(@name, nesting: nesting) else raise ArgumentError, 'When Alba.inflector is nil, either resource or block is required' end end |
#to_h(target, within: nil, params: {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Recursively converts an object into a Hash
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/alba/association.rb', line 60 def to_h(target, within: nil, params: {}) params = params.merge(@params) object = object_from(target, params) return if object.nil? if @resource.is_a?(Proc) return to_h_with_each_resource(object, within, params) if object.is_a?(Enumerable) @resource.call(object).new(object, within: within, params: params, with_traits: @with_traits).to_h else to_h_with_constantize_resource(object, within, params) end end |
#to_h_with_constantize_resource(object, within, params) ⇒ generic_hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
115 116 117 118 |
# File 'lib/alba/association.rb', line 115 def to_h_with_constantize_resource(object, within, params) @resource = constantize(@resource) @resource.new(object, params: params, within: within, with_traits: @with_traits).to_h end |
#to_h_with_each_resource(object, within, params) ⇒ Array[generic_hash]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
109 110 111 112 113 |
# File 'lib/alba/association.rb', line 109 def to_h_with_each_resource(object, within, params) object.map do |item| @resource.call(item).new(item, within: within, params: params, with_traits: @with_traits).to_h end end |