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
Instance Attribute Summary collapse
- #attributes ⇒ Object readonly private
- #attributes ⇒ Object included from ModelDSL readonly
- #builder ⇒ Object included from ModelDSL readonly
- #copy_keys ⇒ Object readonly private
- #klass ⇒ Object included from ModelDSL readonly
- #options ⇒ Object readonly private
- #reject_keys ⇒ Object readonly private
- #steps ⇒ Object readonly private
- #symbolize_keys ⇒ Object readonly private
Instance Method Summary collapse
-
#attribute(name, options = EMPTY_HASH, &block) ⇒ Object
Define a mapping attribute with its options and/or block.
-
#combine(name, options, &block) ⇒ Object
Define an embedded combined attribute that requires "combine" transformation.
-
#embedded(name, options, &block) ⇒ Object
Define an embedded attribute.
- #exclude(name) ⇒ Object private
-
#fold(*args, &block) ⇒ Object
Define an embedded hash attribute that requires "fold" transformation.
-
#group(*args, &block) ⇒ 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.
-
#model(options = nil) ⇒ Object
included
from ModelDSL
Set or generate a model.
-
#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, &block) ⇒ Object
Perform transformations sequentially.
-
#unfold(name, options = EMPTY_HASH) ⇒ Object
Define an embedded hash attribute that requires "unfold" transformation.
-
#ungroup(*args, &block) ⇒ Object
Define an embedded array attribute that requires "ungrouping" transformation.
-
#unwrap(*args, &block) ⇒ Object
Define an embedded hash attribute that requires "unwrapping" transformation.
-
#wrap(*args, &block) ⇒ Object
Define an embedded hash attribute that requires "wrapping" transformation.
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.
25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 25 def initialize(attributes, ) @attributes = attributes @options = @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.
19 20 21 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 19 def attributes @attributes end |
#attributes ⇒ Object (readonly) Originally defined in module ModelDSL
#builder ⇒ Object (readonly) Originally defined in module ModelDSL
#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.
19 20 21 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 19 def copy_keys @copy_keys end |
#klass ⇒ Object (readonly) Originally defined in module ModelDSL
#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.
19 20 21 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 19 def @options 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.
19 20 21 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 19 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.
19 20 21 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 19 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.
19 20 21 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 19 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
79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 79 def attribute(name, = EMPTY_HASH, &block) (name, ) do || if [:type] && block raise ArgumentError, "can't specify type and block at the same time" end [:coercer] = block if block add_attribute(name, ) end end |
#combine(name, options, &block) ⇒ Object
Define an embedded combined attribute that requires "combine" transformation
Typically this can be used to process results of eager-loading
333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 333 def combine(name, , &block) dsl = new(, &block) attr_opts = { type: .fetch(:type, :array), keys: .fetch(:on), combine: true, header: dsl.header } add_attribute(name, attr_opts) end |
#embedded(name, options, &block) ⇒ Object
Define an embedded attribute
Block exposes the attribute dsl too
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 132 def (name, , &block) (name) do || mapper = [:mapper] if mapper = {type: :array}.update() attributes_from_mapper( mapper, name, .update() ) else dsl = new(, &block) .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.
90 91 92 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 90 def exclude(name) attributes << [name, {exclude: true}] end |
#fold(*args, &block) ⇒ 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.
277 278 279 280 281 282 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 277 def fold(*args, &block) (*args) do |name, *| = {type: :array, fold: true} dsl(name, , &block) end end |
#group(*args, &block) ⇒ Object
Define an embedded hash attribute that requires "grouping" transformation
Typically this is used in sql context when relation is a join.
232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 232 def group(*args, &block) 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, , &block) 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
351 352 353 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 351 def header Header.coerce(attributes, copy_keys: copy_keys, model: model, reject_keys: reject_keys) end |
#model(options = nil) ⇒ Object Originally defined in module ModelDSL
Set or generate a model
#prefix(value = Undefined) ⇒ Object
Redefine the prefix for the following attributes
44 45 46 47 48 49 50 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 44 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
60 61 62 63 64 65 66 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 60 def prefix_separator(value = Undefined) if value.equal?(Undefined) @prefix_separator else @prefix_separator = value end end |
#step(options = EMPTY_HASH, &block) ⇒ Object
Perform transformations sequentially
104 105 106 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 104 def step( = EMPTY_HASH, &block) steps << new(, &block) 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.
304 305 306 307 308 309 310 311 312 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 304 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, &block) ⇒ Object
Define an embedded array attribute that requires "ungrouping" transformation
Typically this is used in non-sql context being prepared for import to sql.
257 258 259 260 261 262 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 257 def ungroup(*args, &block) (*args) do |name, , *| = {type: :array, ungroup: true}.update() dsl(name, , &block) end end |
#unwrap(*args, &block) ⇒ 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.
203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 203 def unwrap(*args, &block) (*args) do |name, , mapper| = {type: :hash, unwrap: true}.update() if mapper attributes_from_mapper(mapper, name, ) else dsl(name, , &block) end end end |
#wrap(*args, &block) ⇒ Object
Define an embedded hash attribute that requires "wrapping" transformation
Typically this is used in sql context when relation is a join.
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rom/mapper/attribute_dsl.rb', line 170 def wrap(*args, &block) 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, , &block) end end end |