module PublicAttributes

Public Class Methods

add(class_name, attr_list) click to toggle source
# File lib/public_attributes.rb, line 17
def self.add(class_name, attr_list)
  @_public_attributes[class_name] = *attr_list
  @_public_attributes[class_name].uniq!
end
for(klass) click to toggle source
# File lib/public_attributes.rb, line 22
def self.for(klass)
  self.instance_variable_get(:@_public_attributes)[klass.name]
end
included(klass) click to toggle source
# File lib/public_attributes.rb, line 13
def self.included(klass)
  klass.extend(ClassMethods)
end
reset() click to toggle source
# File lib/public_attributes.rb, line 26
def self.reset
  self.instance_variable_set(:@_public_attributes, {})
end

Public Instance Methods

to_json() click to toggle source
# File lib/public_attributes.rb, line 61
def to_json
  JSON.generate self.to_public
end
to_public() click to toggle source
# File lib/public_attributes.rb, line 48
def to_public
  hash = {}
  all_public_attributes.each do |attr|
    value = self.send(attr)
    if value.respond_to? :to_public
      hash[attr] = value.to_public
    else
      hash[attr] = value
    end
  end
  return hash
end

Private Instance Methods

all_public_attributes() click to toggle source
# File lib/public_attributes.rb, line 67
def all_public_attributes
  attributes = PublicAttributes.for(self.class)
  if defined?(ActiveRecord) && self.is_a?(ActiveRecord::Base)
    attributes += [:created_at, :updated_at]
    attributes.push(:errors) unless self.errors.keys.empty?
  end
  return attributes
end