Class: Lurch::Inflector
- Inherits:
-
Object
- Object
- Lurch::Inflector
- Defined in:
- lib/lurch/inflector.rb
Class Method Summary collapse
Instance Method Summary collapse
- #define_encode_key(inflection_mode) ⇒ Object
- #define_encode_type(types_mode) ⇒ Object
- #encode_keys(hash) ⇒ Object
- #encode_types(hash) ⇒ Object
-
#initialize(inflection_mode, types_mode) ⇒ Inflector
constructor
A new instance of Inflector.
Constructor Details
#initialize(inflection_mode, types_mode) ⇒ Inflector
Returns a new instance of Inflector.
3 4 5 6 |
# File 'lib/lurch/inflector.rb', line 3 def initialize(inflection_mode, types_mode) define_encode_key(inflection_mode) define_encode_type(types_mode) end |
Class Method Details
.classify(s) ⇒ Object
8 9 10 |
# File 'lib/lurch/inflector.rb', line 8 def self.classify(s) Inflecto.classify(s) end |
.decode_key(key) ⇒ Object
12 13 14 |
# File 'lib/lurch/inflector.rb', line 12 def self.decode_key(key) Inflecto.underscore(key.to_s).to_sym end |
.decode_type(type) ⇒ Object
16 17 18 |
# File 'lib/lurch/inflector.rb', line 16 def self.decode_type(type) Inflecto.pluralize(decode_key(type)).to_sym end |
Instance Method Details
#define_encode_key(inflection_mode) ⇒ Object
20 21 22 23 24 25 26 27 28 29 |
# File 'lib/lurch/inflector.rb', line 20 def define_encode_key(inflection_mode) case inflection_mode when :dasherize define_singleton_method(:encode_key) { |key| Inflecto.dasherize(key.to_s) } when :underscore define_singleton_method(:encode_key) { |key| Inflecto.underscore(key.to_s) } else raise ArgumentError, "Invalid inflection mode: #{inflection_mode}" end end |
#define_encode_type(types_mode) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lurch/inflector.rb', line 31 def define_encode_type(types_mode) case types_mode when :pluralize define_singleton_method(:encode_type) do |type| key = encode_key(type) Inflecto.pluralize(key) end when :singularize define_singleton_method(:encode_type) do |type| key = encode_key(type) Inflecto.singularize(key) end else raise ArgumentError, "Invalid types mode: #{types_mode}" end end |
#encode_keys(hash) ⇒ Object
48 49 50 51 52 |
# File 'lib/lurch/inflector.rb', line 48 def encode_keys(hash) hash.each_with_object({}) do |(key, value), acc| acc[encode_key(key)] = block_given? ? yield(value) : value end end |
#encode_types(hash) ⇒ Object
54 55 56 57 58 |
# File 'lib/lurch/inflector.rb', line 54 def encode_types(hash) hash.each_with_object({}) do |(key, value), acc| acc[encode_type(key)] = block_given? ? yield(value) : value end end |