Class: OutsideIn::Base Abstract
- Inherits:
-
Object
- Object
- OutsideIn::Base
- Defined in:
- lib/outside_in/base.rb
Overview
Subclass and declare attributes with #api_attr to implement a custom model class.
The base class for API models.
Models interact with the remote service through the low level #call_remote method. Each model class defines its own high-level finder methods that encapsulate the remote service call. For example:
module OutsideIn
class Thing < Base
def self.by_name(name)
new(call_remote("/things/named/#{URI.escape(name)}"))
end
end
end
Model attributes are declared using #api_attr. Only attributes declared this way are recognized by the initializer when setting the model’s initial state.
Class Method Summary collapse
-
.api_attr(*names) ⇒ void
Adds one or more defined attributes for this model class.
-
.api_attrs ⇒ Hash<Symbol, Class>
Returns the map of defined attributes for this model class.
Instance Method Summary collapse
-
#initialize(attrs = {}) ⇒ OutsideIn::Base
constructor
Returns a new instance.
Constructor Details
#initialize(attrs = {}) ⇒ OutsideIn::Base
Returns a new instance.
Each entry of attrs
whose key identifies a defined model attribute is used to set the value of that attribute. If the attribute’s type is a Class
, then an instance of that class is created with the raw value passed to its initializer. Otherwise, the raw value is used directly.
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/outside_in/base.rb', line 64 def initialize(attrs = {}) self.class.api_attrs.each_pair do |name, clazz| str = name.to_s if attrs.include?(str) v = attrs[str] val = if v.is_a?(Array) v.map {|it| clazz.nil?? it : clazz.new(it)} else clazz.nil?? v : clazz.new(v) end instance_variable_set("@#{str}".to_sym, val) end end end |
Class Method Details
.api_attr(*names) ⇒ void
This method returns an undefined value.
Adds one or more defined attributes for this model class.
If the first argument is a Hash
, then its entries are added directly to the defined attributes map. Otherwise, each argument is taken to be the name of a primitive-typed attribute. In either case, Module#attr_accessor is called for each attribute.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/outside_in/base.rb', line 40 def self.api_attr(*names) @api_attrs ||= {} if ! names.empty? && names.first.is_a?(Hash) names.first.each_pair do |name, clazz| @api_attrs[name.to_sym] = clazz attr_accessor(name.to_sym) end else names.each do |name| @api_attrs[name.to_sym] = nil attr_accessor(name.to_sym) end end end |
.api_attrs ⇒ Hash<Symbol, Class>
Returns the map of defined attributes for this model class. The keys are attribute symbols and the values are the attribute classes (or nil
, indicating that the attribute is of a primitive type).
27 28 29 |
# File 'lib/outside_in/base.rb', line 27 def api_attrs @api_attrs end |