Class: Xunch::Codec

Inherits:
Object
  • Object
show all
Defined in:
lib/xunch/codec/codec.rb

Direct Known Subclasses

HashCodec, JsonCodec

Constant Summary collapse

DEFAULT_TYPE_MAP =
{
  "created_at" => :time,
  "updated_at" => :time,
  "approved_at" => :time
}
DEFAULT_DATE_FORMAT =
"%Y-%m-%dT%H:%M:%S%:z"

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Codec

Returns a new instance of Codec.

Raises:



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/xunch/codec/codec.rb', line 11

def initialize(klass)
  raise XunchCodecError.new("Codec class does not defined method '_accessible_attributes', maybe this klass is not a subclass of ActiveRecord::Base.") unless klass.method_defined?(:_accessible_attributes)
  @klass = klass
  @type_map = DEFAULT_TYPE_MAP
  if @klass.const_defined? :TYPE_MAP
    @type_map = @type_map.merge(@klass::TYPE_MAP)
  end
  @set_methods = {}
  @get_methods = {}
  @klass.attribute_names.each { |attribute|
    if attribute.length == 0
      next
    end
    field = Utils.format_field(attribute)
    @get_methods[field] = attribute.to_sym
    @set_methods[field] = (attribute + "=").to_sym
  }
end