Class: VagrantCloud::Resource::Base

Inherits:
Object
  • Object
show all
Includes:
Logify
Defined in:
lib/vagrant-cloud/resources/base.rb

Direct Known Subclasses

Box

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Create a new instance



104
105
106
107
108
# File 'lib/vagrant-cloud/resources/base.rb', line 104

def initialize(attributes = {})
  attributes.each do |key, value|
    set(key, value)
  end
end

Class Method Details

.attribute(key, default = nil) ⇒ Object



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
# File 'lib/vagrant-cloud/resources/base.rb', line 27

def attribute(key, default = nil)
  key = key.to_sym unless key.is_a?(Symbol)

  # Set this attribute in the top-level hash
  attributes[key] = nil

  define_method(key) do
    value = attributes[key]
    return value unless value.nil?

    if default.nil?
      value
    elsif default.is_a?(Proc)
      default.call
    else
      default
    end
  end

  define_method("#{key}?") do
    !!attributes[key]
  end

  define_method("#{key}=") do |value|
    set(key, value)
  end
end

.attributesArray<Symbol>

The list of attributes defined by this class.

Returns:

  • (Array<Symbol>)


60
61
62
# File 'lib/vagrant-cloud/resources/base.rb', line 60

def attributes
  @attributes ||= {}
end

.from_hash(hash) ⇒ ~Resource::Base

Construct a new object from the hash.

Parameters:

  • hash (Hash)

    the hash to create the object with

  • options (Hash)

    the list options

Returns:



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/vagrant-cloud/resources/base.rb', line 86

def from_hash(hash)
  new.tap do |instance|
    hash.each do |key, value|
      method = :"#{key}="
      if instance.respond_to?(method)
        log.debug { "Setting #{method}: #{value}" }
        instance.send(method, value)
      else
        log.debug { "Does not respond to #{method}, skipping" }
      end
    end
  end
end

.has_attribute?(key) ⇒ true, false

Determine if this class has a given attribute.

Parameters:

  • key (#to_sym)

    the key to check as an attribute

Returns:

  • (true, false)


72
73
74
# File 'lib/vagrant-cloud/resources/base.rb', line 72

def has_attribute?(key)
  attributes.has_key?(key.to_sym)
end

Instance Method Details

#attributeshash

The list of attributes for this resource.

Returns:

  • (hash)


115
116
117
# File 'lib/vagrant-cloud/resources/base.rb', line 115

def attributes
  @attributes ||= self.class.attributes.dup
end

#inspectObject



167
168
169
170
171
172
173
174
175
# File 'lib/vagrant-cloud/resources/base.rb', line 167

def inspect
  list = attributes.collect do |key, value|
    unless Resource::Base.has_attribute?(key)
      "#{key}: #{value.inspect}"
    end
  end.compact

  "#<#{short_classname} #{list.join(', ')}>"
end

#set(key, value) ⇒ Object

Set a given attribute on this resource.

Parameters:

  • key (#to_sym)

    the attribute to set

  • value (Object)

    the value to set

Returns:

  • (Object)

    the set value



130
131
132
# File 'lib/vagrant-cloud/resources/base.rb', line 130

def set(key, value)
  attributes[key.to_sym] = value
end

#to_hashHash

The hash representation

Examples:

An example hash response

{ 'key' => 'local-repo1', 'includesPattern' => '**/*' }

Returns:

  • (Hash)


142
143
144
145
146
147
148
149
150
# File 'lib/vagrant-cloud/resources/base.rb', line 142

def to_hash
  attributes.inject({}) do |hash, (key, value)|
    unless Resource::Base.has_attribute?(key)
      hash[key] = value
    end

    hash
  end
end

#to_jsonString

The JSON representation of this object.

Returns:

  • (String)


157
158
159
# File 'lib/vagrant-cloud/resources/base.rb', line 157

def to_json
  JSON.fast_generate(to_hash)
end

#to_sObject



162
163
164
# File 'lib/vagrant-cloud/resources/base.rb', line 162

def to_s
  "#<#{short_classname}>"
end