Class: Xunch::HashCodec

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

Constant Summary

Constants inherited from Codec

Codec::DEFAULT_DATE_FORMAT, Codec::DEFAULT_TYPE_MAP

Instance Method Summary collapse

Constructor Details

#initialize(klass, fields) ⇒ HashCodec

Returns a new instance of HashCodec.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/xunch/codec/hash_codec.rb', line 4

def initialize(klass,fields)
  super(klass)
  temp_set_methods = {}
  temp_get_methods = {}
  @fields = fields
  @fields.each{ |field|
    temp_get_method = @get_methods[field]
    temp_set_method = @set_methods[field]
    if temp_set_method == nil || temp_get_method == nil
      raise XunchConfigError.new("field #{field} doesn't have set or get method")
    end
    temp_get_methods[field] = temp_get_method
    temp_set_methods[field] = temp_set_method
  }
  @set_methods = temp_set_methods
  @get_methods = temp_get_methods
end

Instance Method Details

#decode(hash) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/xunch/codec/hash_codec.rb', line 43

def decode(hash)
  value = @klass.new
  count = 0
  hash.each { |k, v|
    if v == nil
      count+=1
    else
      value.send(@set_methods[k],v)
    end
  }
  if count == hash.length
    return nil
  else
    return value
  end
end

#decode_fields(hash, fields) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/xunch/codec/hash_codec.rb', line 79

def decode_fields(hash,fields)
  value = @klass.new
  count = 0
  fields.each { |field|
    v = hash[field]
    if v == nil
      count+=1
    else
      value.send(@set_methods[field],v)
    end
  }
  if count == hash.length
    return nil
  else
    return value
  end
end

#encode(value) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/xunch/codec/hash_codec.rb', line 22

def encode(value)
  unless value.class.name.start_with? @klass.name
    raise XunchCodecError.new("Codec error. Codec class is #{@klass}," <<
      "object_id is #{@klass.object_id}, but value class is #{value.class}," <<
      "object_id is #{value.class.object_id}.
      value class = #{value.class.inspect} vs config class = #{@klass.inspect} ")
  end
  hash = Hash.new
  @get_methods.each do | k , v |
    var_value = value.send(v)
    if var_value != nil
      if var_value.class.name == Time.name or var_value.class.name == DateTime.name
        hash[k] = var_value.strftime(DEFAULT_DATE_FORMAT)
      else
        hash[k] = var_value
      end
    end
  end
  return hash
end

#encode_fields(value, fields) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/xunch/codec/hash_codec.rb', line 60

def encode_fields(value,fields)
  hash = Hash.new
  fields.each { |field|
    get_method = @get_methods[field]
    if get_method == nil
      raise XunchCodecError.new("Undefined field #{field}")
    end
    var_value = value.send(get_method)
    if var_value != nil
      if var_value.class.name == Time.name or var_value.class.name == DateTime.name
        hash[field] = var_value.strftime(DEFAULT_DATE_FORMAT)
      else
        hash[field] = var_value
      end
    end
  }
  return hash
end