class ClassKit::AttributeHelper

Public Class Methods

instance() click to toggle source
# File lib/class_kit/attribute_helper.rb, line 4
def self.instance
  @instance ||= ClassKit::AttributeHelper.new
end
new() click to toggle source
# File lib/class_kit/attribute_helper.rb, line 8
def initialize
  @attribute_store = {}
end

Public Instance Methods

get_attribute(klass:, name:) click to toggle source

Get attribute for a given class and name

@param klass [ClassKit] a class that has been extended with ClassKit @param name [Symbol] an attribute name

@raise [ClassKit::Exceptions::AttributeNotFoundError] if the given attribute could not be found

@return [Hash] that describes the attribute

# File lib/class_kit/attribute_helper.rb, line 43
def get_attribute(klass:, name:)
  get_attributes(klass).detect { |a| a[:name] == name } ||
    raise(ClassKit::Exceptions::AttributeNotFoundError, "Attribute: #{name}, could not be found.")
end
get_attribute_type(klass:, name:) click to toggle source

Get the type of a given attribute on a given class

@param klass [ClassKit] a class that has been extended with ClassKit @param name [Symbol]

@return [Class]

# File lib/class_kit/attribute_helper.rb, line 54
def get_attribute_type(klass:, name:)
  get_attribute(klass: klass, name: name)[:type]
end
get_attributes(klass) click to toggle source

Get attributes for a given class

@param klass [ClassKit] a class that has been extended with ClassKit

@return [Hash]

# File lib/class_kit/attribute_helper.rb, line 17
def get_attributes(klass)
  return @attribute_store[klass] if @attribute_store.key?(klass)

  attributes = []
  klass.ancestors.map do |k|
    hash = k.instance_variable_get(:@class_kit_attributes)
    if hash != nil
      hash.values.each do |a|
        attributes.push(a)
      end
    end
  end
  attributes.compact!

  @attribute_store[klass] = attributes
  attributes
end