Class: Interfacets::Shared::Entities::Specs::Association
- Inherits:
-
Object
- Object
- Interfacets::Shared::Entities::Specs::Association
- Includes:
- Acceptable
- Defined in:
- lib/interfacets/shared/entities/specs.rb
Instance Attribute Summary collapse
-
#accepted_by ⇒ Object
readonly
Returns the value of attribute accepted_by.
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#getter ⇒ Object
readonly
Returns the value of attribute getter.
-
#identifier ⇒ Object
readonly
Returns the value of attribute identifier.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#setter ⇒ Object
readonly
Returns the value of attribute setter.
Instance Method Summary collapse
- #apply(accepted_by: NOT_PASSED, getter: NOT_PASSED, setter: NOT_PASSED, builder: NOT_PASSED, identifier: NOT_PASSED, mod: nil, type: NOT_PASSED, &block) ⇒ Object
- #dup_for(new_parent) ⇒ Object
- #handler(entity) ⇒ Object
-
#initialize(parent:, name:) ⇒ Association
constructor
A new instance of Association.
Methods included from Acceptable
#accepted_by?, #standardized_accepted_by
Constructor Details
#initialize(parent:, name:) ⇒ Association
Returns a new instance of Association.
77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/interfacets/shared/entities/specs.rb', line 77 def initialize(parent:, name:) @parent = parent @name = name @accepted_by = Entities::Specs::ANY @getter = -> { store.send(name) } @setter = ->(val) { store.send("#{name}=", val) } @builder = ->() { store.association(name).build } @identifier = "id" @klass = Class.new(Entity) do define_singleton_method(:name) { "#{parent.name}.#{name}" } end end |
Instance Attribute Details
#accepted_by ⇒ Object (readonly)
Returns the value of attribute accepted_by.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def accepted_by @accepted_by end |
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def builder @builder end |
#getter ⇒ Object (readonly)
Returns the value of attribute getter.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def getter @getter end |
#identifier ⇒ Object (readonly)
Returns the value of attribute identifier.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def identifier @identifier end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def parent @parent end |
#setter ⇒ Object (readonly)
Returns the value of attribute setter.
66 67 68 |
# File 'lib/interfacets/shared/entities/specs.rb', line 66 def setter @setter end |
Instance Method Details
#apply(accepted_by: NOT_PASSED, getter: NOT_PASSED, setter: NOT_PASSED, builder: NOT_PASSED, identifier: NOT_PASSED, mod: nil, type: NOT_PASSED, &block) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/interfacets/shared/entities/specs.rb', line 91 def apply( accepted_by: NOT_PASSED, getter: NOT_PASSED, setter: NOT_PASSED, builder: NOT_PASSED, identifier: NOT_PASSED, mod: nil, type: NOT_PASSED, &block ) @accepted_by = Array(accepted_by).map(&:to_s) unless accepted_by == NOT_PASSED @getter = getter unless getter == NOT_PASSED @setter = setter unless setter == NOT_PASSED @builder = builder unless builder == NOT_PASSED @identifier = identifier.to_s unless identifier == NOT_PASSED if type && type != NOT_PASSED type = type.to_sym unless [:reference, :collection].include?(type) raise ArgumentError.new("invalid association type: #{type.inspect}") end if @type && type != @type raise ArgumentError.new("invalid association cannot change type") end @type = type end @klass.include(mod) if mod @klass.class_exec(&block) if block_given? end |
#dup_for(new_parent) ⇒ Object
125 126 127 128 129 130 |
# File 'lib/interfacets/shared/entities/specs.rb', line 125 def dup_for(new_parent) dup.tap do |new_spec| new_spec.instance_variable_set(:@parent, new_parent) new_spec.instance_variable_set(:@klass, Class.new(@klass)) end end |
#handler(entity) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/interfacets/shared/entities/specs.rb', line 132 def handler(entity) klass = ( if type == :reference Handlers::Reference elsif type == :collection Handlers::Collection else raise "Type was never set for association #{parent.name}.#{name}" end ) klass.new(entity:, spec: self) end |