Class: Kamaze::Project::Tools::Gemspec::Reader::Decorator

Inherits:
Object
  • Object
show all
Defined in:
lib/kamaze/project/tools/gemspec/reader/decorator.rb

Overview

Note:

Decorator is not recursive

Decorator for Gem::Specification

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spec) ⇒ Decorator

Returns a new instance of Decorator.

Parameters:

  • spec (Gem::Specification)


20
21
22
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 20

def initialize(spec)
  @spec = spec
end

Instance Attribute Details

#specGem::Specification (readonly, protected)

Returns:

  • (Gem::Specification)


42
43
44
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 42

def spec
  @spec
end

Instance Method Details

#decorate_to_hashHash (protected)

Decorate to obtain a Hash

Returns:

  • (Hash)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 60

def decorate_to_hash
  result = {}

  spec.instance_variables.each do |k|
    k = k.to_s.gsub(/^@/, '').to_sym

    result[k] = spec.__send__(k) if spec.respond_to?(k)
  end

  result
end

#decorate_to_ostructOpenStruct (protected)

Decorate to obtain an OpenStruct

Returns:

  • (OpenStruct)


75
76
77
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 75

def decorate_to_ostruct
  OpenStruct.new(decorate_to_h)
end

#methods_mappingHash (protected)

Provides methods mapping

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 47

def methods_mapping
  {
    Hash => :decorate_to_hash,
    hash: :decorate_to_hash,
    OpenStruct => :decorate_to_ostruct,
    ostruct: :decorate_to_ostruct,
    open_struct: :decorate_to_ostruct,
  }
end

#to(type) ⇒ Object

Decorate specification to a given type

Parameters:

  • type (Class|symbol)

Returns:

Raises:

  • (ArgumentError)

See Also:

  • methods


31
32
33
34
35
36
37
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 31

def to(type)
  method = methods_mapping.fetch(type)
rescue KeyError
  raise ArgumentError, "invalid type: #{type}"
else
  self.__send__(method)
end