Sha256: da09b4c9a0666baae1104a19468cb849e88710c30bb4acb3409550fae37eac4b

Contents?: true

Size: 536 Bytes

Versions: 1

Compression:

Stored size: 536 Bytes

Contents

module AttributeJsonizer
  require 'yajl'

  def add_jsonizable_attribute(json_name, jsonizeable_object)
    self.class.module_eval { attr_accessor json_name.to_sym}
    self.send("#{json_name.to_s}=", jsonizeable_object)
  end

  def to_json
    json_hash = {}
    self.instance_variables.each do |iv|
      key = iv
      value = self.instance_variable_get(iv)
      json_hash[key.to_s.gsub("@","")] = value unless value.kind_of?(Array) && value.length == 0 #Bail on empty arrays
    end
    Yajl::Encoder.encode(json_hash)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blitline-1.4.0 lib/blitline/attribute_jsonizer.rb