Class: Grably::Core::Product

Inherits:
Object
  • Object
show all
Defined in:
lib/grably/core/product.rb,
lib/grably/core/product.rb

Overview

Product is core, minimal entity in build process. It describes real file with virtual destination. Product instances should be immutable.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src, dst = nil, meta = {}) ⇒ Product

Returns a new instance of Product.



233
234
235
236
237
238
239
# File 'lib/grably/core/product.rb', line 233

def initialize(src, dst = nil, meta = {})
  raise 'src should be a string' unless src.is_a?(String)
  raise 'dst should be a string' unless dst.is_a?(String) || dst.nil?
  @src = File.expand_path(src)
  @dst = dst || File.basename(src)
  @meta = meta.freeze # Ensure meta is immutable
end

Instance Attribute Details

#dstObject (readonly)

Returns the value of attribute dst.



231
232
233
# File 'lib/grably/core/product.rb', line 231

def dst
  @dst
end

#metaObject (readonly)

Returns the value of attribute meta.



231
232
233
# File 'lib/grably/core/product.rb', line 231

def meta
  @meta
end

#srcObject (readonly)

Returns the value of attribute src.



231
232
233
# File 'lib/grably/core/product.rb', line 231

def src
  @src
end

Class Method Details

.expand(expr, task = nil) ⇒ Object



295
296
297
# File 'lib/grably/core/product.rb', line 295

def expand(expr, task = nil)
  Grably::Core::ProductExpand.expand(expr, task)
end

Instance Method Details

#==(other) ⇒ Object



272
273
274
275
276
277
278
# File 'lib/grably/core/product.rb', line 272

def ==(other)
  # Everything which not a Product, can't be equal to Product
  return false unless other.is_a? Product

  # Should we include meta in comparison?
  @src.eql?(other.src) && @dst.eql?(other.dst)
end

#[](*keys) ⇒ Object



241
242
243
244
245
246
247
248
# File 'lib/grably/core/product.rb', line 241

def [](*keys)
  return @meta[keys.first] if keys.size == 1

  # Iterate over keys to preserve order so we can unpack result
  # like:
  # foo, bar = product[:foo, :bar]
  keys.map { |k| @meta[k] }
end

#basename(*args) ⇒ Object



288
289
290
# File 'lib/grably/core/product.rb', line 288

def basename(*args)
  File.basename(@dst, *args)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


284
285
286
# File 'lib/grably/core/product.rb', line 284

def eql?(other)
  self == other
end

#exist?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/grably/core/product.rb', line 255

def exist?
  File.exist?(@src)
end

#hashObject



280
281
282
# File 'lib/grably/core/product.rb', line 280

def hash
  @src.hash
end

#inspectObject



259
260
261
# File 'lib/grably/core/product.rb', line 259

def inspect
  'Product[src=\'%s\', dst=\'%s\', meta=%s]'.format(src, dst, meta)
end

#mapObject



263
264
265
266
# File 'lib/grably/core/product.rb', line 263

def map
  src, dst, meta = yield(@src, @dst, @meta)
  Product.new(src || @src, dst || @dst, meta || @meta)
end

#to_sObject



268
269
270
# File 'lib/grably/core/product.rb', line 268

def to_s
  inspect
end

#update(values) ⇒ Object



250
251
252
253
# File 'lib/grably/core/product.rb', line 250

def update(values)
  # Provide immutable update
  Product.new(@src, @dst, @meta.merge(values))
end