Class: EYCli::Model::Base

Inherits:
Hashie::Mash
  • Object
show all
Extended by:
SmartyParser
Defined in:
lib/ey_cli/models/base.rb

Direct Known Subclasses

Account, App, Deployment, Environment

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SmartyParser

parse, smarty

Class Method Details

.allObject



6
7
8
9
# File 'lib/ey_cli/models/base.rb', line 6

def self.all
  resp = EYCli.api.get(base_path)
  parse resp.body[base_path]
end

.base_path(path = "#{class_name}s") ⇒ Object

HAX: pluralize!



21
22
23
# File 'lib/ey_cli/models/base.rb', line 21

def self.base_path(path = "#{class_name}s") # HAX: pluralize!
  @base_path ||= path
end

.child_path(args) ⇒ Object



33
34
35
# File 'lib/ey_cli/models/base.rb', line 33

def self.child_path(args)
  resolve_child_path(args)
end

.class_nameObject



25
26
27
# File 'lib/ey_cli/models/base.rb', line 25

def self.class_name
  self.name.split('::').last.downcase
end

.collection_path(args) ⇒ Object



37
38
39
# File 'lib/ey_cli/models/base.rb', line 37

def self.collection_path(args)
  base_path % args
end

.create(hash) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/ey_cli/models/base.rb', line 41

def self.create(hash)
  path = create_collection_path(hash)

  response = EYCli.api.post(path, nil, hash)
  new response.body[class_name]
rescue Faraday::Error::ClientError => e
  new MultiJson.decode(e.response[:body])
end

.create_collection_path(hash) ⇒ Object



56
57
58
# File 'lib/ey_cli/models/base.rb', line 56

def self.create_collection_path(hash)
  raise "Not implemented. Override this method for each model"
end

.find(*args) ⇒ Object



11
12
13
14
# File 'lib/ey_cli/models/base.rb', line 11

def self.find(*args)
  resp = EYCli.api.get(resolve_child_path(args))
  parse resp.body[class_name]
end

.find_by_name(name, collection = all) ⇒ Object



16
17
18
19
# File 'lib/ey_cli/models/base.rb', line 16

def self.find_by_name(name, collection = all)
  # FIXME: EY api doesn't have filters. Let's do it by hand.
  collection.select {|a| a.name == name }.first
end

.resolve_child_path(args) ⇒ Object



29
30
31
# File 'lib/ey_cli/models/base.rb', line 29

def self.resolve_child_path(args)
  "#{collection_path(args[0..-1])}/#{args.last}"
end

Instance Method Details

#convert_value(val, duping = false) ⇒ Object

Overrides Hashie::Mash#convert_value to not convert already parsed Mashes again



51
52
53
54
# File 'lib/ey_cli/models/base.rb', line 51

def convert_value(val, duping=false) #:nodoc:
  return val.dup if val.is_a?(Hashie::Mash)
  super(val, duping)
end