Class: Kamaze::Project::Tools::Gemspec::Reader::Decorator
- Defined in:
- lib/kamaze/project/tools/gemspec/reader/decorator.rb
Overview
Note:
Decorator is not recursive
Decorator for Gem::Specification
Instance Attribute Summary collapse
- #spec ⇒ Gem::Specification readonly protected
Instance Method Summary collapse
-
#decorate_to_hash ⇒ Hash
protected
Decorate to obtain a
Hash
. -
#decorate_to_ostruct ⇒ OpenStruct
protected
Decorate to obtain an
OpenStruct
. -
#initialize(spec) ⇒ Decorator
constructor
A new instance of Decorator.
-
#methods_mapping ⇒ Hash
protected
Provides methods mapping.
-
#to(type) ⇒ Object
Decorate specification to a given type.
Constructor Details
#initialize(spec) ⇒ Decorator
Returns a new instance of Decorator.
17 18 19 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 17 def initialize(spec) @spec = spec end |
Instance Attribute Details
#spec ⇒ Gem::Specification (readonly, protected)
39 40 41 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 39 def spec @spec end |
Instance Method Details
#decorate_to_hash ⇒ Hash (protected)
Decorate to obtain a Hash
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 57 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_ostruct ⇒ OpenStruct (protected)
Decorate to obtain an OpenStruct
72 73 74 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 72 def decorate_to_ostruct OpenStruct.new(decorate_to_h) end |
#methods_mapping ⇒ Hash (protected)
Provides methods mapping
44 45 46 47 48 49 50 51 52 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 44 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
28 29 30 31 32 33 34 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 28 def to(type) method = methods_mapping.fetch(type) rescue KeyError raise ArgumentError, "invalid type: #{type}" else self.__send__(method) end |