Class: Fog::Attributes::Default

Inherits:
Object
  • Object
show all
Defined in:
lib/fog/core/attributes/default.rb

Overview

Fog Default Attribute

This class handles the attributes without a type force. The attributes returned from the provider will keep its original values.

Direct Known Subclasses

Array, Boolean, Float, Integer, String, Time, Timestamp

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, options) ⇒ Default

Returns a new instance of Default.

Parameters:

  • model (Klass)

    model class

  • name (Symbol)

    name of the attribute

  • options (Hash)

    attribute options

Options Hash (options):

  • :squash (Symbol)
  • :aliases (::Array)
  • :default (Object)
  • :as (Object)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/core/attributes/default.rb', line 17

def initialize(model, name, options)
  @model = model
  @model.attributes << name
  @name = name
  @squash = options.fetch(:squash, false)
  @aliases = options.fetch(:aliases, [])
  @default = options[:default]
  @as = options.fetch(:as, name)
  create_setter
  create_getter
  create_aliases
  set_defaults
  create_mask
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



8
9
10
# File 'lib/fog/core/attributes/default.rb', line 8

def aliases
  @aliases
end

#asObject (readonly)

Returns the value of attribute as.



8
9
10
# File 'lib/fog/core/attributes/default.rb', line 8

def as
  @as
end

#defaultObject (readonly)

Returns the value of attribute default.



8
9
10
# File 'lib/fog/core/attributes/default.rb', line 8

def default
  @default
end

#modelObject (readonly)

Returns the value of attribute model.



8
9
10
# File 'lib/fog/core/attributes/default.rb', line 8

def model
  @model
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/fog/core/attributes/default.rb', line 8

def name
  @name
end

#squashObject (readonly)

Returns the value of attribute squash.



8
9
10
# File 'lib/fog/core/attributes/default.rb', line 8

def squash
  @squash
end

Instance Method Details

#create_aliasesObject



70
71
72
73
74
# File 'lib/fog/core/attributes/default.rb', line 70

def create_aliases
  Array(aliases).each do |alias_name|
    model.aliases[alias_name] = name
  end
end

#create_getterObject



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/core/attributes/default.rb', line 58

def create_getter
  model.class_eval <<-EOS, __FILE__, __LINE__
    def #{name}
      return attributes[:#{name}] unless attributes[:#{name}].nil?
      if !attributes.key?(:#{name}) && !self.class.default_values[:#{name}].nil? && !persisted?
        return self.class.default_values[:#{name}]
      end
      attributes[:#{name}]
    end
  EOS
end

#create_maskObject



80
81
82
# File 'lib/fog/core/attributes/default.rb', line 80

def create_mask
  model.masks[name] = as
end

#create_setterObject



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/core/attributes/default.rb', line 32

def create_setter
  if squash
    model.class_eval <<-EOS, __FILE__, __LINE__
        def #{name}=(new_data)
          if new_data.is_a?(Hash)
            if new_data.has_key?(:'#{squash}')
              attributes[:#{name}] = new_data[:'#{squash}']
            elsif new_data.has_key?("#{squash}")
              attributes[:#{name}] = new_data["#{squash}"]
            else
              attributes[:#{name}] = [ new_data ]
            end
          else
            attributes[:#{name}] = new_data
          end
        end
    EOS
  else
    model.class_eval <<-EOS, __FILE__, __LINE__
        def #{name}=(new_#{name})
          attributes[:#{name}] = new_#{name}
        end
    EOS
  end
end

#set_defaultsObject



76
77
78
# File 'lib/fog/core/attributes/default.rb', line 76

def set_defaults
  model.default_values[name] = default unless default.nil?
end