Module: Candywrapper::ClassMethods

Defined in:
lib/candywrapper.rb

Instance Method Summary collapse

Instance Method Details

#candywrap_hash(h) ⇒ Object



89
90
91
92
93
# File 'lib/candywrapper.rb', line 89

def candywrap_hash(h)
  obj = self.new
  obj.payload_hash_set(h)
  obj
end

#deserialize_from_json(json) ⇒ Object



95
96
97
98
# File 'lib/candywrapper.rb', line 95

def deserialize_from_json(json)
  h = JSON.parse(json)
  candywrap_hash(h)
end

#serializable_attr(name, type = nil, opt = {}) ⇒ Object

Description

Add a serializable attribute to the current class.

Parameters

name

(Symbol) Attribute name

type

(Class) Class of the attribute value; nil to indicate “don’t care”

opt

(Hash) Additional options

  • opt

    do not allow nil assignment



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/candywrapper.rb', line 109

def serializable_attr(name, type = nil, opt = {})

  name_s = name.to_s
  if type.respond_to?(:ancestors) and type.ancestors.include?(Candywrapper)
    conversion = :candywrapper
  elsif type == Time
    conversion = :iso8601
  else
    conversion = :none
  end

  define_method name_s do
    candywrapper_attr_get(name_s, type, conversion, opt)
  end

  define_method "#{name_s}=" do |val|
    candywrapper_attr_set(name_s, type, conversion, val, opt)
  end
end