Class: ROM::Mapper::AttributeDSL Private
- Inherits:
-
Object
- Object
- ROM::Mapper::AttributeDSL
- Includes:
- ModelDSL
- Defined in:
- lib/rom/mapper/attribute_dsl.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
Mapper attribute DSL exposed by mapper subclasses
This class is private even though its methods are exposed by mappers. Typically it’s not meant to be used directly.
TODO: break this madness down into smaller pieces
rubocop:disable Metrics/ClassLength
Constant Summary
Constants included from ModelDSL
Instance Attribute Summary collapse
- #attributes ⇒ Object readonly private
- #copy_keys ⇒ Object readonly private
- #options ⇒ Object readonly private
- #reject_keys ⇒ Object readonly private
- #steps ⇒ Object readonly private
- #symbolize_keys ⇒ Object readonly private
Attributes included from ModelDSL
Instance Method Summary collapse
-
#attribute(name, options = EMPTY_HASH, &block) ⇒ Object
Define a mapping attribute with its options and/or block.
-
#combine(name, options) ⇒ Object
Define an embedded combined attribute that requires “combine” transformation.
-
#embedded(name, options) ⇒ Object
Define an embedded attribute.
- #exclude(name) ⇒ Object private
-
#fold(*args) ⇒ Object
Define an embedded hash attribute that requires “fold” transformation.
-
#group(*args) ⇒ Object
Define an embedded hash attribute that requires “grouping” transformation.
-
#header ⇒ Header
private
Generate a header from attribute definitions.
-
#initialize(attributes, options) ⇒ AttributeDSL
constructor
private
A new instance of AttributeDSL.
-
#prefix(value = Undefined) ⇒ Object
Redefine the prefix for the following attributes.
-
#prefix_separator(value = Undefined) ⇒ Object
Redefine the prefix separator for the following attributes.
-
#step(options = EMPTY_HASH) ⇒ Object
Perform transformations sequentially.
-
#unfold(name, options = EMPTY_HASH) ⇒ Object
Define an embedded hash attribute that requires “unfold” transformation.
-
#ungroup(*args) ⇒ Object
Define an embedded array attribute that requires “ungrouping” transformation.
-
#unwrap(*args) ⇒ Object
Define an embedded hash attribute that requires “unwrapping” transformation.
-
#wrap(*args) ⇒ Object
Define an embedded hash attribute that requires “wrapping” transformation.
Methods included from ModelDSL
Constructor Details
#initialize(attributes, options) ⇒ AttributeDSL
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 AttributeDSL.
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 36 def initialize(attributes, ) @attributes = attributes = @copy_keys = .fetch(:copy_keys) @symbolize_keys = .fetch(:symbolize_keys) @prefix = .fetch(:prefix) @prefix_separator = .fetch(:prefix_separator) @reject_keys = .fetch(:reject_keys) @steps = [] end |
Instance Attribute Details
#attributes ⇒ Object (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.
20 21 22 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 20 def attributes @attributes end |
#copy_keys ⇒ Object (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.
24 25 26 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 24 def copy_keys @copy_keys end |
#options ⇒ Object (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.
22 23 24 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 22 def end |
#reject_keys ⇒ Object (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.
28 29 30 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 28 def reject_keys @reject_keys end |
#steps ⇒ Object (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.
30 31 32 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 30 def steps @steps end |
#symbolize_keys ⇒ Object (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.
26 27 28 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 26 def symbolize_keys @symbolize_keys end |
Instance Method Details
#attribute(name, options = EMPTY_HASH, &block) ⇒ Object
Define a mapping attribute with its options and/or block
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 90 def attribute(name, = EMPTY_HASH, &block) (name, ) do || if [:type] && block_given? raise ArgumentError, "can't specify type and block at the same time" end [:coercer] = block if block_given? add_attribute(name, ) end end |
#combine(name, options) ⇒ Object
Define an embedded combined attribute that requires “combine” transformation
Typically this can be used to process results of eager-loading
344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 344 def combine(name, , &) dsl = new(, &) attr_opts = { type: .fetch(:type, :array), keys: .fetch(:on), combine: true, header: dsl.header } add_attribute(name, attr_opts) end |
#embedded(name, options) ⇒ Object
Define an embedded attribute
Block exposes the attribute dsl too
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 143 def (name, , &) (name) do || mapper = [:mapper] if mapper = { type: :array }.update() attributes_from_mapper( mapper, name, .update() ) else dsl = new(, &) .update() add_attribute( name, { header: dsl.header, type: :array }.update() ) end end end |
#exclude(name) ⇒ 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.
101 102 103 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 101 def exclude(name) attributes << [name, exclude: true] end |
#fold(*args) ⇒ Object
Define an embedded hash attribute that requires “fold” transformation
Typically this is used in sql context to fold single joined field to the array of values.
288 289 290 291 292 293 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 288 def fold(*args, &) (*args) do |name, *| = { type: :array, fold: true } dsl(name, , &) end end |
#group(*args) ⇒ Object
Define an embedded hash attribute that requires “grouping” transformation
Typically this is used in sql context when relation is a join.
243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 243 def group(*args, &) ensure_mapper_configuration('group', args, block_given?) (*args) do |name, , mapper| = { type: :array, group: true }.update() if mapper attributes_from_mapper(mapper, name, ) else dsl(name, , &) end end end |
#header ⇒ Header
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.
Generate a header from attribute definitions
362 363 364 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 362 def header Header.coerce(attributes, copy_keys: copy_keys, model: model, reject_keys: reject_keys) end |
#prefix(value = Undefined) ⇒ Object
Redefine the prefix for the following attributes
55 56 57 58 59 60 61 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 55 def prefix(value = Undefined) if value.equal?(Undefined) @prefix else @prefix = value end end |
#prefix_separator(value = Undefined) ⇒ Object
Redefine the prefix separator for the following attributes
71 72 73 74 75 76 77 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 71 def prefix_separator(value = Undefined) if value.equal?(Undefined) @prefix_separator else @prefix_separator = value end end |
#step(options = EMPTY_HASH) ⇒ Object
Perform transformations sequentially
115 116 117 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 115 def step( = EMPTY_HASH, &) steps << new(, &) end |
#unfold(name, options = EMPTY_HASH) ⇒ Object
Define an embedded hash attribute that requires “unfold” transformation
Typically this is used in non-sql context to convert array of values (like in Cassandra ‘SET’ or ‘LIST’ types) to array of tuples.
Source values are assigned to the first key, the other keys being left blank.
315 316 317 318 319 320 321 322 323 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 315 def unfold(name, = EMPTY_HASH) (name, ) do || old_name = .fetch(:from, name) dsl(old_name, type: :array, unfold: true) do attribute name, yield if block_given? end end end |
#ungroup(*args) ⇒ Object
Define an embedded array attribute that requires “ungrouping” transformation
Typically this is used in non-sql context being prepared for import to sql.
268 269 270 271 272 273 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 268 def ungroup(*args, &) (*args) do |name, , *| = { type: :array, ungroup: true }.update() dsl(name, , &) end end |
#unwrap(*args) ⇒ Object
Define an embedded hash attribute that requires “unwrapping” transformation
Typically this is used in no-sql context to normalize data before inserting to sql gateway.
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 214 def unwrap(*args, &) (*args) do |name, , mapper| = { type: :hash, unwrap: true }.update() if mapper attributes_from_mapper(mapper, name, ) else dsl(name, , &) end end end |
#wrap(*args) ⇒ Object
Define an embedded hash attribute that requires “wrapping” transformation
Typically this is used in sql context when relation is a join.
181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 181 def wrap(*args, &) ensure_mapper_configuration('wrap', args, block_given?) (*args) do |name, , mapper| = { type: :hash, wrap: true }.update() if mapper attributes_from_mapper(mapper, name, ) else dsl(name, , &) end end end |