Class: Spaceship::Base
- Inherits:
-
Object
- Object
- Spaceship::Base
- Defined in:
- lib/spaceship/base.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.client ⇒ Object
Returns the value of attribute client.
Class Method Summary collapse
- .attr_mapping(attr_map = nil) ⇒ Object
-
.method_missing(method_sym, *args, &block) ⇒ Object
Call a method to return a subclass constant.
-
.remap_keys!(attrs) ⇒ Object
bang method since it changes the parameter in-place.
-
.set_client(client) ⇒ Object
set client and return self for chaining.
Instance Method Summary collapse
- #client ⇒ Object
-
#initialize(attrs = {}) ⇒ Base
constructor
A new instance of Base.
- #inspect ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ Base
Returns a new instance of Base.
64 65 66 67 68 69 70 |
# File 'lib/spaceship/base.rb', line 64 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 ⇒ Object
Returns the value of attribute client.
4 5 6 |
# File 'lib/spaceship/base.rb', line 4 def client @client end |
Class Method Details
.attr_mapping(attr_map = nil) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/spaceship/base.rb', line 26 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
51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/spaceship/base.rb', line 51 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) ⇒ Object
bang method since it changes the parameter in-place
18 19 20 21 22 23 24 |
# File 'lib/spaceship/base.rb', line 18 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) ⇒ Object
set client and return self for chaining
11 12 13 14 |
# File 'lib/spaceship/base.rb', line 11 def set_client(client) self.client = client self end |
Instance Method Details
#client ⇒ Object
72 73 74 |
# File 'lib/spaceship/base.rb', line 72 def client @client end |
#inspect ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/spaceship/base.rb', line 76 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 |