Module: Antelope::Generator::Base::Coerce
- Included in:
- Antelope::Generator::Base
- Defined in:
- lib/antelope/generator/base/coerce.rb
Overview
Handles coercion of directives and their values.
Instance Method Summary collapse
-
#coerce_directive_class(values, type) ⇒ Object
If the expected of the directive is a Class, then we try to determine which class is expected, and return the proper values.
-
#coerce_directive_value(defined, values, type) ⇒ Object?
Coerce the given directive value to the given type.
-
#coerce_nested_hash(key, value) ⇒ Hash
Coerces a key of the format
<name>[.<name>]*
into a full hash accessable by ruby. -
#directives ⇒ Hash
Retrieves all directives from the grammar, and giving them the proper values for this instance.
Instance Method Details
#coerce_directive_class(values, type) ⇒ Object
If the expected of the directive is a Class, then we try to determine which class is expected, and return the proper values.
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/antelope/generator/base/coerce.rb', line 99 def coerce_directive_class(values, type) if type == Array values elsif type == String values[0].to_s elsif type == Fixnum || type == Integer || type == Numeric values[0].to_i elsif type == Float values[0].to_f else type.new(*values) end end |
#coerce_directive_value(defined, values, type) ⇒ Object?
Coerce the given directive value to the given type. For the
type nil
, it checks the size of the values; for no values,
it returns true; for one value, it returns that one value;
for any other size value, it returns the values. For the
type Boolean
, if no values were given, or if the first
value isn't "false", it returns true. For the type
:single
(or :one
), it returns the first value. For the
type Array
, it returns the values. For any other type
that is a class, it tries to initialize the class with the
given arguments.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/antelope/generator/base/coerce.rb', line 69 def coerce_directive_value(defined, values, type) return nil unless defined || type.is_a?(Array) case type when nil values.any? ? values[0] : true when :single, :one values[0] when Boolean values[0].to_s != 'false' when Array values.zip(type).map do |value, t| coerce_directive_value(defined, [value], t) end when Class coerce_directive_class(values, type) else raise UnknownTypeError, "unknown type #{type}" end end |
#coerce_nested_hash(key, value) ⇒ Hash
Coerces a key of the format <name>[.<name>]*
into a full
hash accessable by ruby.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/antelope/generator/base/coerce.rb', line 34 def coerce_nested_hash(key, value) parts = key.split('.').map { |p| p.gsub(/-/, '_') } top = {} hash = top parts.each do |part| hash[part] = if parts.last == part value else {} end hash = hash[part] end top[key] = value top end |
#directives ⇒ Hash
Retrieves all directives from the grammar, and giving them the proper values for this instance.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/antelope/generator/base/coerce.rb', line 12 def directives @_directives ||= begin hash = Hashie::Mash.new self.class.directives.each do |key, (_, definition)| directive_value = coerce_directive_value(grammar..key?(key), grammar.[key], definition) value = coerce_nested_hash(key, directive_value) hash.deep_merge!(value) end hash end end |