Class: ClassKit::AttributeHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/class_kit/attribute_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAttributeHelper

Returns a new instance of AttributeHelper.



8
9
10
# File 'lib/class_kit/attribute_helper.rb', line 8

def initialize
  @attribute_store = {}
end

Class Method Details

.instanceObject



4
5
6
# File 'lib/class_kit/attribute_helper.rb', line 4

def self.instance
  @instance ||= ClassKit::AttributeHelper.new
end

Instance Method Details

#get_attribute(klass:, name:) ⇒ Hash

Get attribute for a given class and name

Parameters:

  • klass (ClassKit)

    a class that has been extended with ClassKit

  • name (Symbol)

    an attribute name

Returns:

  • (Hash)

    that describes the attribute

Raises:



43
44
45
46
# 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:) ⇒ Class

Get the type of a given attribute on a given class

Parameters:

  • klass (ClassKit)

    a class that has been extended with ClassKit

  • name (Symbol)

Returns:

  • (Class)


54
55
56
# 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) ⇒ Hash

Get attributes for a given class

Parameters:

  • klass (ClassKit)

    a class that has been extended with ClassKit

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 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