Module: Chimps::Utils::UsesModel
- Included in:
- Commands::Create, Commands::Destroy, Commands::List, Commands::Search, Commands::Show, Commands::Update
- Defined in:
- lib/chimps/utils/uses_model.rb
Instance Method Summary collapse
- #define_model_option ⇒ Object
- #model ⇒ Object
- #model=(model) ⇒ Object
- #model_identifier ⇒ Object
- #model_path ⇒ Object
- #models_path ⇒ Object
- #models_string ⇒ Object
- #plural_model ⇒ Object
Instance Method Details
#define_model_option ⇒ Object
42 43 44 45 46 |
# File 'lib/chimps/utils/uses_model.rb', line 42 def define_model_option on_tail("-m", "--model MODEL", "Use a different resource, one of: #{models_string}") do |m| self.model= m end end |
#model ⇒ Object
5 6 7 |
# File 'lib/chimps/utils/uses_model.rb', line 5 def model @model ||= self.class::MODELS.first end |
#model=(model) ⇒ Object
30 31 32 33 |
# File 'lib/chimps/utils/uses_model.rb', line 30 def model= model raise CLIError.new("Invalid model: #{model}. Must be one of #{models_string}") unless self.class::MODELS.include?(model) @model = model end |
#model_identifier ⇒ Object
17 18 19 20 |
# File 'lib/chimps/utils/uses_model.rb', line 17 def model_identifier raise CLIError.new("Must provide an ID or URL-escaped handle as the first argument") if argv.first.blank? argv.first end |
#model_path ⇒ Object
26 27 28 |
# File 'lib/chimps/utils/uses_model.rb', line 26 def model_path "#{plural_model}/#{model_identifier}.json" end |
#models_path ⇒ Object
22 23 24 |
# File 'lib/chimps/utils/uses_model.rb', line 22 def models_path "#{plural_model}.json" end |
#models_string ⇒ Object
35 36 37 38 39 40 |
# File 'lib/chimps/utils/uses_model.rb', line 35 def models_string returning(self.class::MODELS.dup) do |parts| parts[0] = "#{parts.first} (default)" parts[-1] = "or #{parts.last}" end.join(', ') end |
#plural_model ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/chimps/utils/uses_model.rb', line 9 def plural_model if model[-1].chr == 'y' model[1..-1] + 'ies' else model + 's' end end |