Module: ClassKit

Defined in:
lib/class_kit/version.rb,
lib/class_kit/helper.rb,
lib/class_kit/custom_type.rb,
lib/class_kit/value_helper.rb,
lib/class_kit/class_methods.rb,
lib/class_kit/attribute_helper.rb,
lib/class_kit/exceptions/invalid_class_error.rb,
lib/class_kit/exceptions/attribute_not_found_error.rb,
lib/class_kit/exceptions/invalid_parse_value_error.rb,
lib/class_kit/exceptions/invalid_attribute_value_error.rb

Overview

Namespace

Defined Under Namespace

Modules: CustomType, Exceptions Classes: AttributeHelper, Helper, ValueHelper

Constant Summary collapse

VERSION =

:nodoc:

'0.8.0'

Instance Method Summary collapse

Instance Method Details

#attr_accessor_type(name, type: nil, collection_type: nil, allow_nil: true, default: nil, auto_init: false, alias_name: nil, meta: {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/class_kit/class_methods.rb', line 2

def attr_accessor_type(
  name,
  type: nil,
  collection_type: nil,
  allow_nil: true,
  default: nil,
  auto_init: false,
  alias_name: nil,
  meta: {})
  unless instance_variable_defined?(:@class_kit_attributes)
    instance_variable_set(:@class_kit_attributes, {})
  end

  attributes = instance_variable_get(:@class_kit_attributes)

  attributes[name] = {
    name: name,
    type: type,
    collection_type: collection_type,
    allow_nil: allow_nil,
    default: default,
    auto_init: auto_init,
    alias: alias_name,
    meta: meta
  }

  class_eval do
    define_method name do

      cka = ClassKit::AttributeHelper.instance.get_attribute(klass: self.class, name: name)

      current_value = instance_variable_get(:"@#{name}")

      if current_value.nil?
        if !cka[:default].nil?
          current_value = instance_variable_set(:"@#{name}", cka[:default])
        elsif cka[:auto_init]
          current_value = instance_variable_set(:"@#{name}", cka[:type].new)
        end
      end

      current_value
    end
  end

  class_eval do
    define_method "#{name}=" do |value|
      # get the attribute meta data
      cka = ClassKit::AttributeHelper.instance.get_attribute(klass: self.class, name: name)

      # verify if the attribute is allowed to be set to nil
      if value.nil? && cka[:allow_nil] == false
        raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must not be nil."
      end

      # check if the value being set is not of the specified type and should attempt to parse the value
      if !cka[:type].nil? && !value.nil? && (cka[:type] == :bool || !value.is_a?(cka[:type]))
        begin
          value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value)
        rescue => e
          raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must be of type: #{cka[:type]}. Error: #{e}"
        end
      end

      instance_variable_set(:"@#{name}", value)
    end
  end
end