Class: Spaceship::Base
- Inherits:
-
Object
- Object
- Spaceship::Base
- Defined in:
- lib/spaceship/base.rb
Overview
Spaceship::Base is the superclass for models in Apple Developer Portal. It’s mainly responsible for mapping responses to objects.
A class-level attribute ‘client` is used to maintain the which spaceship we are using to talk to ADP.
Example of creating a new ADP model:
class Widget < Spaceship::Base
attr_accessor :id, :name, :foo_bar, :wiz_baz
attr_mapping({
'name' => :name,
'fooBar' => :foo_bar,
'wizBaz' => :wiz_baz
})
end
When you want to instantiate a model pass in the parsed response: ‘Widget.new(widget_json)`
Direct Known Subclasses
Class Attribute Summary collapse
-
.client ⇒ Spaceship::Client
The client used to make requests.
Class Method Summary collapse
-
.attr_mapping(attr_map = nil) ⇒ Object
Defines the attribute mapping between the response from Apple and our model objects.
-
.method_missing(method_sym, *args, &block) ⇒ Object
Call a method to return a subclass constant.
-
.remap_keys!(attrs) ⇒ Hash
Remaps the attributes passed into the initializer to the model attributes using the map defined by ‘attr_map`.
-
.set_client(client) ⇒ Spaceship::Base
Sets client and returns self for chaining.
Instance Method Summary collapse
-
#client ⇒ Spaceship::Client
The current spaceship client used by the model to make requests.
-
#initialize(attrs = {}) ⇒ Base
constructor
The initialize method accepts a parsed response from Apple and sets all attributes that are defined by ‘attr_mapping`.
- #inspect ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Base
The initialize method accepts a parsed response from Apple and sets all attributes that are defined by ‘attr_mapping`
Do not override ‘initialize` in your own models.
113 114 115 116 117 118 119 |
# File 'lib/spaceship/base.rb', line 113 def initialize(attrs = {}) self.class.remap_keys!(attrs) attrs.each do |key, val| self.send("#{key}=", val) if respond_to?("#{key}=") end @client = self.class.client end |
Class Attribute Details
.client ⇒ Spaceship::Client
The client used to make requests.
28 29 30 |
# File 'lib/spaceship/base.rb', line 28 def client @client end |
Class Method Details
.attr_mapping(attr_map = nil) ⇒ Object
Defines the attribute mapping between the response from Apple and our model objects. Keys are to match keys in the response and the values are to match attributes on the model.
Example of using ‘attr_mapping`
class Widget < Spaceship::Base
attr_accessor :id, :name, :foo_bar, :wiz_baz
attr_mapping({
'name' => :name,
'fooBar' => :foo_bar,
'wizBaz' => :wiz_baz
})
end
70 71 72 73 74 75 76 |
# File 'lib/spaceship/base.rb', line 70 def attr_mapping(attr_map = nil) if attr_map @attr_mapping = attr_map else @attr_mapping ||= ancestors[1].attr_mapping rescue nil end end |
.method_missing(method_sym, *args, &block) ⇒ Object
Call a method to return a subclass constant.
If ‘method_sym` is an underscored name of a class, return the class with the current client passed into it. If the method does not match, NoMethodError is raised.
Example:
Certificate.production_push
#=> Certificate::ProductionPush
ProvisioningProfile.ad_hoc
#=> ProvisioningProfile::AdHoc
ProvisioningProfile.some_other_method
#=> NoMethodError: undefined method `some_other_method' for ProvisioningProfile
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/spaceship/base.rb', line 95 def method_missing(method_sym, *args, &block) module_name = method_sym.to_s module_name.sub!(/^[a-z\d]/) { $&.upcase } module_name.gsub!(/(?:_|(\/))([a-z\d])/) { $2.upcase } if const_defined?(module_name) klass = const_get(module_name) klass.set_client(@client) else super end end |
.remap_keys!(attrs) ⇒ Hash
Remaps the attributes passed into the initializer to the model attributes using the map defined by ‘attr_map`.
This method consumes the input parameter meaning attributes that were remapped are deleted.
48 49 50 51 52 53 54 |
# File 'lib/spaceship/base.rb', line 48 def remap_keys!(attrs) return if attr_mapping.nil? attr_mapping.each do |from, to| attrs[to] = attrs.delete(from) end end |
.set_client(client) ⇒ Spaceship::Base
Sets client and returns self for chaining.
35 36 37 38 |
# File 'lib/spaceship/base.rb', line 35 def set_client(client) self.client = client self end |
Instance Method Details
#client ⇒ Spaceship::Client
Returns The current spaceship client used by the model to make requests.
123 124 125 |
# File 'lib/spaceship/base.rb', line 123 def client @client end |
#inspect ⇒ Object
127 128 129 130 131 132 133 134 |
# File 'lib/spaceship/base.rb', line 127 def inspect inspectables = instance_variables - [:@client] inspect_vars = inspectables.map do |ivar| val = instance_variable_get(ivar) "#{ivar}=#{val.inspect}" end "\n#<#{self.class.name}\n\t#{inspect_vars.join("\n\t")}>" end |