Class: Reap::InfoObject

Inherits:
Object
  • Object
show all
Defined in:
lib/reap/iobject.rb

Overview

InfoObject

This a base class used by Metadata. It provides a more versitle means of defining a data-oriented class.

Direct Known Subclasses

Project::Metadata

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ InfoObject

Initialize



104
105
106
# File 'lib/reap/iobject.rb', line 104

def initialize(data={})
  update(data)
end

Class Method Details

.alias_accessor(aliaz, name) ⇒ Object

Define an attribute alias.



58
59
60
61
# File 'lib/reap/iobject.rb', line 58

def alias_accessor(aliaz, name)
  alias_method aliaz, name
  alias_method "#{aliaz}=", "#{name}="
end

.alias_method(aliaz, name) ⇒ Object



65
66
67
68
69
70
# File 'lib/reap/iobject.rb', line 65

def alias_method(aliaz, name)
  super
  if method_defined?("#{name}=")
    super("#{aliaz}=", "#{name}=")
  end
end

.attr_accessor(name, *aliases, &blk) ⇒ Object

Define an attribute.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reap/iobject.rb', line 44

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_attributesObject



38
39
40
# File 'lib/reap/iobject.rb', line 38

def instance_attributes
  @attributes ||= []
end

.validate(message, &block) ⇒ Object



77
78
79
# File 'lib/reap/iobject.rb', line 77

def validate(message, &block)
  validation << [message, block]
end

.validationObject



73
74
75
# File 'lib/reap/iobject.rb', line 73

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? ++



130
131
132
133
134
135
136
# File 'lib/reap/iobject.rb', line 130

def [](name)
  begin
    h = send(name)
  rescue NoMethodError
    h = nil
  end
end

#attributesObject

List attributes. (Needed?)



96
97
98
# File 'lib/reap/iobject.rb', line 96

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?



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/reap/iobject.rb', line 145

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_dataObject



90
91
92
# File 'lib/reap/iobject.rb', line 90

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.



165
166
167
168
169
170
171
# File 'lib/reap/iobject.rb', line 165

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

#taguriObject

For yaml conversion, no tag.



215
# File 'lib/reap/iobject.rb', line 215

def taguri; nil; end

#to_hashObject Also known as: to_h

Convert to hash.



219
220
221
222
223
224
225
# File 'lib/reap/iobject.rb', line 219

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.



230
231
232
# File 'lib/reap/iobject.rb', line 230

def to_xml( opts={} )
  raise "not yet implemented"
end

#to_yaml(opts = {}) ⇒ Object

Use YAML format.



208
209
210
211
# File 'lib/reap/iobject.rb', line 208

def to_yaml( opts={} )
  require 'yaml'
  super
end

#to_yaml_propertiesObject

Order of attributes for yaml conversion.



202
203
204
# File 'lib/reap/iobject.rb', line 202

def to_yaml_properties
  attributes.collect{ |a| "@#{a}" }
end

#update(data) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/reap/iobject.rb', line 108

def update(data)
  instance_data.update(data.rekey(:to_s))

  data.each do |k,v|
    send("#{k}=", v) if respond_to?("#{k}=")
  end

  # TODO Could add yield(self) via:
  #yld.to_h.each do |k,v|
  #  send( "#{k}=", v ) rescue nil
  #end
end

#valid?Boolean

Returns:

  • (Boolean)


178
179
180
181
182
183
184
185
# File 'lib/reap/iobject.rb', line 178

def valid?
  begin
    validate
    return true
  rescue TypeError
    return false
  end
end

#validateObject Also known as: assert_valid



188
189
190
191
192
# File 'lib/reap/iobject.rb', line 188

def validate
  self.class.validation.each do |message, block|
    raise(TypeError, message) unless instance_eval(&block)
  end
end