Module: LunaPark::Extensions::DataMapper::ClassMethods
- Defined in:
- lib/luna_park/extensions/data_mapper.rb
Constant Summary collapse
- DEFAULT_PRIMARY_KEY =
:id
Instance Attribute Summary collapse
-
#__entity_coercion__ ⇒ Object
readonly
Returns the value of attribute entity_coercion.
-
#entity_class ⇒ Object
readonly
Returns the value of attribute entity_class.
-
#mapper_class ⇒ Object
readonly
Returns the value of attribute mapper_class.
Instance Method Summary collapse
- #__build_entity_coercion__(coercion) ⇒ Object
- #__define_class__(name, parent) ⇒ Object
- #__define_constants__(not_found: LunaPark::Extensions::DataMapper::NotFound) ⇒ Object
- #base_anonymous_mapper ⇒ Object abstract
-
#entity(entity, coercion = nil) ⇒ Object
Configure tagret entity class and coercion for it.
- #infer_entity_coercion ⇒ Object
- #inherited(klass) ⇒ Object
-
#mapper(mapper = Undefined, &block) ⇒ Object
Configure Mapper.
- #primary_key(attr) ⇒ Object
- #row_primary_key ⇒ Object
Instance Attribute Details
#__entity_coercion__ ⇒ Object (readonly)
Returns the value of attribute entity_coercion.
64 65 66 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 64 def __entity_coercion__ @__entity_coercion__ end |
#entity_class ⇒ Object (readonly)
Returns the value of attribute entity_class.
64 65 66 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 64 def entity_class @entity_class end |
#mapper_class ⇒ Object (readonly)
Returns the value of attribute mapper_class.
64 65 66 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 64 def mapper_class @mapper_class end |
Instance Method Details
#__build_entity_coercion__(coercion) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 148 def __build_entity_coercion__(coercion) return entity_class.method(coercion) if coercion.is_a? Symbol return coercion if coercion.respond_to?(:call) raise ArgumentError, 'coercion MUST be call\'able, Symbol or nil' unless coercion.nil? infer_entity_coercion end |
#__define_class__(name, parent) ⇒ Object
195 196 197 198 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 195 def __define_class__(name, parent) klass = Class.new(parent) const_set name, klass end |
#__define_constants__(not_found: LunaPark::Extensions::DataMapper::NotFound) ⇒ Object
191 192 193 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 191 def __define_constants__(not_found: LunaPark::Extensions::DataMapper::NotFound) __define_class__ 'NotFound', not_found end |
#base_anonymous_mapper ⇒ Object
This method is abstract.
177 178 179 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 177 def base_anonymous_mapper LunaPark::Mappers::Codirectional end |
#entity(entity, coercion = nil) ⇒ Object
Configure tagret entity class and coercion for it
101 102 103 104 105 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 101 def entity(entity, coercion = nil) @entity_class = entity @__entity_coercion__ = __build_entity_coercion__(coercion) @entity_class end |
#infer_entity_coercion ⇒ Object
157 158 159 160 161 162 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 157 def infer_entity_coercion return entity_class.method(:call) if entity_class.respond_to?(:call) return entity_class.method(:wrap) if entity_class.respond_to?(:wrap) ->(input) { entity_class.new(input.to_h) } end |
#inherited(klass) ⇒ Object
200 201 202 203 204 205 206 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 200 def inherited(klass) klass.__define_constants__(not_found: NotFound) klass.entity entity_class, __entity_coercion__ klass.mapper mapper_class klass.primary_key row_primary_key super end |
#mapper(mapper = Undefined, &block) ⇒ Object
Configure Mapper
138 139 140 141 142 143 144 145 146 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 138 def mapper(mapper = Undefined, &block) raise ArgumentError, 'Expected mapper xOR block' unless (mapper == Undefined) ^ block.nil? return @mapper_class = mapper if block.nil? @mapper_class = Class.new(base_anonymous_mapper) @mapper_class.class_eval(&block) @mapper_class end |
#primary_key(attr) ⇒ Object
181 182 183 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 181 def primary_key(attr) @row_primary_key = attr end |
#row_primary_key ⇒ Object
187 188 189 |
# File 'lib/luna_park/extensions/data_mapper.rb', line 187 def row_primary_key @row_primary_key || DEFAULT_PRIMARY_KEY end |