Module: SafetyModule

Defined in:
lib/safety/safety_module.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/safety/safety_module.rb', line 11

def method_missing(method, *args, &block)
  type = SafetyAttributeHelper.parse_attribute_type method
  if type
    # Extract the type name from the attribute declaration
    type_name = method.to_s.slice("attr_#{type.to_s}_".length...method.to_s.length)
    
    # If the type name is var, type safety will be disabled for this attribute
    should_enforce_type_safety = type_name != 'var'
    
    # The class to enforce type safety on
    type_safe_class = nil
    
    # Get the type, which could be deep in modules
    if should_enforce_type_safety == true
      type_safe_class = type_name.split('::').inject(Object.class) do |current_module, class_name|
        current_module.const_get(class_name)
      end
    end

    # Define enforce safety for this property
    define_method "__enforce_safety_#{args[0]}".to_sym do |value|
      # If enforcing type and value is not of type safe class, raise TypeError
      if should_enforce_type_safety == true and value.is_a?(type_safe_class) == false
        raise TypeError, "Type Safe Attribute \"#{args[0]}\", of type \"#{type_safe_class}\", set to value \"#{value}\" of class \"#{value.class}\"."
      end
      return value
    end

    # A way to get the default value
    define_method "__default_value_#{args[0]}".to_sym do
      if args[1] != nil
        return args[1]
      else
        return nil
      end
    end
    
    # Set default, if one was provided
    #set_value.call(args[1]) if args[1] != nil
    define_method "__lazy_instantiator_#{args[0]}".to_sym do
      return block
    end

    lazy_instantiation = <<EVAL
def __lazy_instantiation_#{args[0]}
return nil if __lazy_instantiator_#{args[0]}().nil?
instance_exec &__lazy_instantiator_#{args[0]}
end
EVAL

    getter = <<EVAL
def #{args[0]}
@#{args[0]} = __lazy_instantiation_#{args[0]} if @#{args[0]}.nil? && __lazy_instantiator_#{args[0]} != nil
@#{args[0]} = __enforce_safety_#{args[0]}(__default_value_#{args[0]}) if @#{args[0]}.nil? && __default_value_#{args[0]} != nil
__enforce_safety_#{args[0]}(@#{args[0]})
end
EVAL

    setter = <<EVAL
def #{args[0]}= value
@#{args[0]} = __enforce_safety_#{args[0]}(value)
end
EVAL

    to_evaluate = lazy_instantiation
    case type
    when :accessor
      to_evaluate << getter
      to_evaluate << setter
    when :reader
      to_evaluate << getter
    when :writer
      to_evaluate << setter
    end
    
    self.class_eval to_evaluate
  else
    super
  end
end

Instance Method Details

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
# File 'lib/safety/safety_module.rb', line 3

def respond_to?(method)
  if SafetyAttributeHelper.parse_attribute_type method
    true
  else
    super
  end
end