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



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

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



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 156

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



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 178

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



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 138

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



224
225
226
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 224

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



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

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



216
217
218
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 216

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



196
197
198
199
200
201
202
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 196

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
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 31

def from_array(value)
  _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



47
48
49
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 47

def from_boolean(value)
  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



63
64
65
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 63

def from_date(value)
  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



79
80
81
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 79

def from_datetime(value)
  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



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

def from_hash(value)
  _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



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

def from_time(value)
  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



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

def to_array(value)
  _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



55
56
57
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 55

def to_boolean(value)
  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



71
72
73
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 71

def to_date(value)
  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



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

def to_datetime(value)
  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



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

def to_hash(value)
  _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



119
120
121
# File 'lib/lotus/model/adapters/dynamodb/coercer.rb', line 119

def to_time(value)
  Time.at(value.to_f)
end