Class: VagrantCloud::Resource::Base
- Inherits:
-
Object
- Object
- VagrantCloud::Resource::Base
- Includes:
- Logify
- Defined in:
- lib/vagrant-cloud/resources/base.rb
Direct Known Subclasses
Class Method Summary collapse
- .attribute(key, default = nil) ⇒ Object
-
.attributes ⇒ Array<Symbol>
The list of attributes defined by this class.
-
.from_hash(hash) ⇒ ~Resource::Base
Construct a new object from the hash.
-
.has_attribute?(key) ⇒ true, false
Determine if this class has a given attribute.
Instance Method Summary collapse
-
#attributes ⇒ hash
The list of attributes for this resource.
-
#initialize(attributes = {}) ⇒ Base
constructor
Create a new instance.
- #inspect ⇒ Object
-
#set(key, value) ⇒ Object
Set a given attribute on this resource.
-
#to_hash ⇒ Hash
The hash representation.
-
#to_json ⇒ String
The JSON representation of this object.
- #to_s ⇒ Object
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 |
.attributes ⇒ Array<Symbol>
The list of attributes defined by this class.
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.
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.
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
#attributes ⇒ hash
The list of attributes for this resource.
115 116 117 |
# File 'lib/vagrant-cloud/resources/base.rb', line 115 def attributes @attributes ||= self.class.attributes.dup end |
#inspect ⇒ Object
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.
130 131 132 |
# File 'lib/vagrant-cloud/resources/base.rb', line 130 def set(key, value) attributes[key.to_sym] = value end |
#to_hash ⇒ Hash
The hash representation
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_json ⇒ String
The JSON representation of this object.
157 158 159 |
# File 'lib/vagrant-cloud/resources/base.rb', line 157 def to_json JSON.fast_generate(to_hash) end |
#to_s ⇒ Object
162 163 164 |
# File 'lib/vagrant-cloud/resources/base.rb', line 162 def to_s "#<#{short_classname}>" end |