Class: Reap::InfoObject
- Inherits:
-
Object
- Object
- Reap::InfoObject
- Defined in:
- lib/reap/iobject.rb
Direct Known Subclasses
Class Method Summary collapse
-
.alias_accessor(aliaz, name) ⇒ Object
Define an attribute alias.
- .alias_method(aliaz, name) ⇒ Object
-
.attr_accessor(name, *aliases, &blk) ⇒ Object
Define an attribute.
- .instance_attributes ⇒ Object
- .validate(message, &block) ⇒ Object
- .validation ⇒ Object
Instance Method Summary collapse
-
#[](name) ⇒ Object
Fetch attribute value, but return nil if it doesn’t exist.
-
#attributes ⇒ Object
List attributes.
-
#gather(*names) ⇒ Object
Gathers a group of info hash entries into a merged hash.
-
#initialize(data = {}) ⇒ InfoObject
constructor
Initialize.
- #instance_data ⇒ Object
-
#select(*args) ⇒ Object
Collects a group of info entries into a hash.
-
#taguri ⇒ Object
For yaml conversion, no tag.
-
#to_hash ⇒ Object
(also: #to_h)
Convert to hash.
-
#to_xml(opts = {}) ⇒ Object
Use generic XML format.
-
#to_yaml(opts = {}) ⇒ Object
Use YAML format.
-
#to_yaml_properties ⇒ Object
Order of attributes for yaml conversion.
- #update(data) ⇒ Object
- #valid? ⇒ Boolean
- #validate ⇒ Object (also: #assert_valid)
Constructor Details
#initialize(data = {}) ⇒ InfoObject
Initialize
102 103 104 |
# File 'lib/reap/iobject.rb', line 102 def initialize(data={}) update(data) end |
Class Method Details
.alias_accessor(aliaz, name) ⇒ Object
Define an attribute alias.
56 57 58 59 |
# File 'lib/reap/iobject.rb', line 56 def alias_accessor(aliaz, name) alias_method aliaz, name alias_method "#{aliaz}=", "#{name}=" end |
.alias_method(aliaz, name) ⇒ Object
63 64 65 66 67 68 |
# File 'lib/reap/iobject.rb', line 63 def alias_method(aliaz, name) super if method_defined?("#{name}=") super("#{aliaz}=", "#{name}=") end end |
.attr_accessor(name, *aliases, &blk) ⇒ Object
Define an attribute.
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/reap/iobject.rb', line 42 def attr_accessor(name, *aliases, &blk) instance_attributes << name.to_s instance_attributes.uniq! if blk define_method(name, &blk) attr_writer(name) else super(name) end aliases.each{ |aliaz| alias_accessor(aliaz, name) } end |
.instance_attributes ⇒ Object
36 37 38 |
# File 'lib/reap/iobject.rb', line 36 def instance_attributes @attributes ||= [] end |
.validate(message, &block) ⇒ Object
75 76 77 |
# File 'lib/reap/iobject.rb', line 75 def validate(, &block) validation << [, block] end |
.validation ⇒ Object
71 72 73 |
# File 'lib/reap/iobject.rb', line 71 def validation @validation ||= [] end |
Instance Method Details
#[](name) ⇒ Object
Fetch attribute value, but return nil if it doesn’t exist. – TODO Use in method missing instead? ++
128 129 130 131 132 133 134 |
# File 'lib/reap/iobject.rb', line 128 def [](name) begin h = send(name) rescue NoMethodError h = nil end end |
#attributes ⇒ Object
List attributes. (Needed?)
94 95 96 |
# File 'lib/reap/iobject.rb', line 94 def attributes self.class.instance_attributes end |
#gather(*names) ⇒ Object
Gathers a group of info hash entries into a merged hash. The names
are taken in most to least significant order.
gather(:package)
TODO Change name of this method to something better?
143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/reap/iobject.rb', line 143 def gather( *names ) result = names.inject({}) do |hash,name| attributes.each do |n| if n.to_s =~ /^#{name}_(.*?)$/ hash[$1] = self[n.to_s] if self[n.to_s] end end hash end result end |
#instance_data ⇒ Object
88 89 90 |
# File 'lib/reap/iobject.rb', line 88 def instance_data @instance_data ||= {} end |
#select(*args) ⇒ Object
Collects a group of info entries into a hash. Arguments are a list of info entry names and/or a hash or new name to info entry name.
select(:name, :version, :date => :released)
This is used to collect info to pass to tools.
163 164 165 166 167 168 169 |
# File 'lib/reap/iobject.rb', line 163 def select( *args ) maps = (Hash === args.last ? args.pop : {}) h = {} args.each{ |k| h[k.to_s] = self[k] } maps.each{ |k, i| h[k.to_s] = self[i] } h end |
#taguri ⇒ Object
For yaml conversion, no tag.
213 |
# File 'lib/reap/iobject.rb', line 213 def taguri; nil; end |
#to_hash ⇒ Object Also known as: to_h
Convert to hash.
217 218 219 220 221 222 223 |
# File 'lib/reap/iobject.rb', line 217 def to_hash attributes.inject({}) do |h, a| v = self[a.to_s] #send(a) h[a] = v unless v.nil? h end end |
#to_xml(opts = {}) ⇒ Object
Use generic XML format.
228 229 230 |
# File 'lib/reap/iobject.rb', line 228 def to_xml( opts={} ) raise "not yet implemented" end |
#to_yaml(opts = {}) ⇒ Object
Use YAML format.
206 207 208 209 |
# File 'lib/reap/iobject.rb', line 206 def to_yaml( opts={} ) require 'yaml' super end |
#to_yaml_properties ⇒ Object
Order of attributes for yaml conversion.
200 201 202 |
# File 'lib/reap/iobject.rb', line 200 def to_yaml_properties attributes.collect{ |a| "@#{a}" } end |
#update(data) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/reap/iobject.rb', line 106 def update(data) instance_data.update(data.rekey(:to_s)) data.each do |k,v| send("#{k}=", v) rescue nil end # TODO Could add yield(self) via: #yld.to_h.each do |k,v| # send( "#{k}=", v ) rescue nil #end end |
#valid? ⇒ Boolean
176 177 178 179 180 181 182 183 |
# File 'lib/reap/iobject.rb', line 176 def valid? begin validate return true rescue TypeError return false end end |
#validate ⇒ Object Also known as: assert_valid
186 187 188 189 190 |
# File 'lib/reap/iobject.rb', line 186 def validate self.class.validation.each do |, block| raise(TypeError, ) unless instance_eval(&block) end end |