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.
20 21 22 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 20 def initialize(spec) @spec = spec end |
Instance Attribute Details
#spec ⇒ Gem::Specification (readonly, protected)
42 43 44 |
# File 'lib/kamaze/project/tools/gemspec/reader/decorator.rb', line 42 def spec @spec end |
Instance Method Details
#decorate_to_hash ⇒ Hash (protected)
Decorate to obtain a 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_ostruct ⇒ OpenStruct (protected)
Decorate to obtain an 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_mapping ⇒ Hash (protected)
Provides methods mapping
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
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 |