Class: Lotus::Model::Adapters::Dynamodb::Coercer Private

Inherits:
Mapping::Coercer
  • Object
show all
Defined in:
lib/lotus/model/adapters/dynamodb/coercer.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.

Translates values from/to the database with the corresponding Ruby type.

Since:

  • 0.1.0

Constant Summary collapse

SKIPPED_KLASSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

[Float, Integer, Set, String]
SUPPORTED_KLASSES =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

[AWS::DynamoDB::Binary, Array, Boolean, Date, DateTime, Hash, Time]

Instance Method Summary collapse

Instance Method Details

#_compile!Object (private)

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.

Compile itself for performance boost.

Since:

  • 0.1.0



140
141
142
143
144
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 140

def _compile!
  _compile_skipped!
  _compile_record!
  _compile_serialization!
end

#_compile_record!Object (private)

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.

Compile record methods.

Since:

  • 0.1.1



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 168

def _compile_record!
  instance_eval %{
    def to_record(entity)
      if entity.id
        Hash[*[#{ @collection.attributes.map{|name,(klass,mapped)| ":#{mapped},from_#{_method_name(klass)}(entity.#{name})"}.join(',') }]]
      else
        Hash[*[#{ @collection.attributes.reject{|name,_| name == @collection.identity }.map{|name,(klass,mapped)| ":#{mapped},from_#{_method_name(klass)}(entity.#{name})"}.join(',') }]]
      end
    end

    def from_record(record)
      #{ @collection.entity }.new(
        Hash[*[#{ @collection.attributes.map{|name,(klass,mapped)| ":#{name},#{coercions_wrap(klass) { "to_#{_method_name(klass)}(record[:#{mapped}])" }}"}.join(',') }]]
      )
    end
  }
end

#_compile_serialization!Object (private)

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.

Compile deserialise/serialize methods.

Since:

  • 0.1.1



190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 190

def _compile_serialization!
  instance_eval(@collection.attributes.map do |_,(klass,mapped)|
    %{
    def deserialize_#{ mapped }(value)
      #{coercions_wrap(klass) { "from_#{_method_name(klass)}(value)" }}
    end

    def serialize_#{ mapped }(value)
      from_#{_method_name(klass)}(value)
    end
    }
  end.join("\n"))
end

#_compile_skipped!Object (private)

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.

Compile skipped klasses methods.

Since:

  • 0.1.1



150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 150

def _compile_skipped!
  instance_eval(SKIPPED_KLASSES.map do |klass|
    %{
    def from_#{_method_name(klass)}(value)
      value
    end

    def to_#{_method_name(klass)}(value)
      value
    end
    }
  end.join("\n"))
end

#_deserialize(value) ⇒ Object (private)

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.

Deserializes value from string.

Since:

  • 0.1.0



236
237
238
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 236

def _deserialize(value)
  MultiJson.load(value)
end

#_method_name(klass) ⇒ Object (private)

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 method name from klass.

Since:

  • 0.1.0



220
221
222
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 220

def _method_name(klass)
  klass.to_s.downcase.gsub("::", "_")
end

#_serialize(value) ⇒ Object (private)

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.

Serializes value to string.

Since:

  • 0.1.0



228
229
230
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 228

def _serialize(value)
  MultiJson.dump(value)
end

#coercions_wrap(klass) ⇒ Object (private)

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.

Wraps string in Lotus::Model::Mapping::Coercions call if needed.

Since:

  • 0.1.0



208
209
210
211
212
213
214
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 208

def coercions_wrap(klass)
  if klass.to_s.include?("::")
    yield
  else
    "Lotus::Model::Mapping::Coercions.#{klass}(#{yield})"
  end
end

#from_array(value) ⇒ 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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



31
32
33
34
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 31

def from_array(value)
  return if value.nil?
  _serialize(value)
end

#from_aws_dynamodb_binary(value) ⇒ Object Also known as: to_aws_dynamodb_binary

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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



20
21
22
23
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 20

def from_aws_dynamodb_binary(value)
  return value if value.nil? || value.is_a?(AWS::DynamoDB::Binary)
  AWS::DynamoDB::Binary.new(value)
end

#from_boolean(value) ⇒ 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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



49
50
51
52
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 49

def from_boolean(value)
  return if value.nil?
  value ? 1 : 0
end

#from_date(value) ⇒ 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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



67
68
69
70
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 67

def from_date(value)
  return if value.nil?
  value.to_time.to_i
end

#from_datetime(value) ⇒ 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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



85
86
87
88
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 85

def from_datetime(value)
  return if value.nil?
  value.to_time.to_f
end

#from_hash(value) ⇒ 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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



103
104
105
106
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 103

def from_hash(value)
  return if value.nil?
  _serialize(value)
end

#from_time(value) ⇒ 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.

Converts value from given type to DynamoDB record value.

Since:

  • 0.1.0



121
122
123
124
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 121

def from_time(value)
  return if value.nil?
  value.to_f
end

#to_array(value) ⇒ 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.

Converts value from DynamoDB record value to given type.

Since:

  • 0.1.0



40
41
42
43
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 40

def to_array(value)
  return if value.nil?
  _deserialize(value)
end

#to_boolean(value) ⇒ 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.

Converts value from DynamoDB record value to given type.

Since:

  • 0.1.0



58
59
60
61
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 58

def to_boolean(value)
  return if value.nil?
  value.to_i == 1
end

#to_date(value) ⇒ 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.

Converts value from DynamoDB record value to given type.

Since:

  • 0.1.0



76
77
78
79
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 76

def to_date(value)
  return if value.nil?
  Time.at(value.to_i).to_date
end

#to_datetime(value) ⇒ 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.

Converts value from DynamoDB record value to given type.

Since:

  • 0.1.0



94
95
96
97
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 94

def to_datetime(value)
  return if value.nil?
  Time.at(value.to_f).to_datetime
end

#to_hash(value) ⇒ 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.

Converts value from DynamoDB record value to given type.

Since:

  • 0.1.0



112
113
114
115
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 112

def to_hash(value)
  return if value.nil?
  _deserialize(value)
end

#to_time(value) ⇒ 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.

Converts value from DynamoDB record value to given type.

Since:

  • 0.1.0



130
131
132
133
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 130

def to_time(value)
  return if value.nil?
  Time.at(value.to_f)
end