Class: Fog::Attributes::Enum

Inherits:
Default
  • Object
show all
Defined in:
lib/fog/hyperv/fog_extensions/enum.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ Enum

Returns a new instance of Enum.



6
7
8
9
10
11
12
13
# File 'lib/fog/hyperv/fog_extensions/enum.rb', line 6

def initialize(model, name, options)
  @values = options.fetch(:values, [])

  raise Fog::Hyperv::Errors::ServiceError, "#{values} is not a valid array or hash" \
    unless values.class.to_s == 'Array' || values.class.to_s == 'Hash'

  super
end

Instance Attribute Details

#valuesObject (readonly)

Returns the value of attribute values.



4
5
6
# File 'lib/fog/hyperv/fog_extensions/enum.rb', line 4

def values
  @values
end

Instance Method Details

#create_getterObject



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
# File 'lib/fog/hyperv/fog_extensions/enum.rb', line 58

def create_getter
  ensure_value_getter

  # Add a getter for <enum>_num to get the numeric value
  model.class_eval "      def \#{name}_num\n        _values = \#{name}_values\n        _value = attributes[:\#{name}]\n\n        return nil if _value.nil?\n        if _value.is_a?(Numeric)\n          _value\n        else\n          if _values.is_a?(Hash)\n            _values.send(:[], _value)\n          else\n            _values.index(_value)\n          end\n        end\n      end\n  EOS\n  \n  # Add the default getter for the symbol value\n  super\nend\n", __FILE__, __LINE__

#create_setterObject



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
# File 'lib/fog/hyperv/fog_extensions/enum.rb', line 26

def create_setter
  ensure_value_getter

  # Add a setter that always stores a symbol value
  model.class_eval "      def \#{name}=(new_value)\n        _values = \#{name}_values\n        if new_value.is_a?(Numeric)\n          # TODO: Better way to do class comparison in generated code\n          if _values.class.to_s == 'Array'\n            raise Fog::Hyperv::Errors::ServiceError, \"\\\#{new_value} is not in the range (0..\\\#{_values.length - 1})\" \\\n              unless new_value >= 0 && new_value < _values.length\n            attributes[:\#{name}] = _values[new_value]\n          elsif _values.class.to_s == 'Hash'\n            raise Fog::Hyperv::Errors::ServiceError, \"\\\#{new_value} is not one of \\\#{_values.values})\" \\\n              unless _values.values.include? new_value\n            attributes[:\#{name}] = _values.key(new_value)\n          end\n        elsif new_value.nil?\n          attributes[:\#{name}] = nil\n        else\n          new_value = new_value.to_s.to_sym unless new_value.is_a? Symbol\n          # Ensure values is the array of enum symbols\n          _values = (_values.is_a?(Hash) ? _values.keys : _values)\n          raise Fog::Hyperv::Errors::ServiceError, \"\\\#{new_value.inspect} is not one of \\\#{_values})\" \\\n            unless _values.include? new_value\n          attributes[:\#{name}] = new_value\n        end\n      end\n  EOS\nend\n", __FILE__, __LINE__

#ensure_value_getterObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/fog/hyperv/fog_extensions/enum.rb', line 15

def ensure_value_getter
  return if model.private_methods.include?("#{name}_values".to_sym)

  model.class_eval "      private\n      def \#{name}_values\n        \#{values}.freeze\n      end\n  EOS\nend\n", __FILE__, __LINE__