Class: EasyPost::EasyPostObject
- Inherits:
-
Object
- Object
- EasyPost::EasyPostObject
- Includes:
- Enumerable
- Defined in:
- lib/easypost/object.rb
Direct Known Subclasses
Constant Summary collapse
- @@immutable_values =
Set.new([:api_key, :id])
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#name ⇒ Object
Returns the value of attribute name.
-
#parent ⇒ Object
Returns the value of attribute parent.
-
#unsaved_values ⇒ Object
Returns the value of attribute unsaved_values.
Class Method Summary collapse
Instance Method Summary collapse
- #[](k) ⇒ Object
- #[]=(k, v) ⇒ Object
- #as_json(options = {}) ⇒ Object
- #each(&blk) ⇒ Object
- #id ⇒ Object
- #id=(id) ⇒ Object
-
#initialize(id = nil, api_key = nil, parent = nil, name = nil) ⇒ EasyPostObject
constructor
A new instance of EasyPostObject.
- #inspect ⇒ Object
- #keys ⇒ Object
- #refresh_from(values, api_key, partial = false) ⇒ Object
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ Object
- #to_s(*args) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(id = nil, api_key = nil, parent = nil, name = nil) ⇒ EasyPostObject
Returns a new instance of EasyPostObject.
8 9 10 11 12 13 14 15 16 |
# File 'lib/easypost/object.rb', line 8 def initialize(id=nil, api_key=nil, parent=nil, name=nil) @api_key = api_key @values = {} @unsaved_values = Set.new @transient_values = Set.new @parent = parent @name = name self.id = id if id end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/easypost/object.rb', line 5 def api_key @api_key end |
#name ⇒ Object
Returns the value of attribute name.
5 6 7 |
# File 'lib/easypost/object.rb', line 5 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
5 6 7 |
# File 'lib/easypost/object.rb', line 5 def parent @parent end |
#unsaved_values ⇒ Object
Returns the value of attribute unsaved_values.
5 6 7 |
# File 'lib/easypost/object.rb', line 5 def unsaved_values @unsaved_values end |
Class Method Details
.construct_from(values, api_key = nil, parent = nil, name = nil) ⇒ Object
18 19 20 21 22 |
# File 'lib/easypost/object.rb', line 18 def self.construct_from(values, api_key=nil, parent=nil, name=nil) obj = self.new(values[:id], api_key, parent, name) obj.refresh_from(values, api_key) obj end |
Instance Method Details
#[](k) ⇒ Object
49 50 51 52 |
# File 'lib/easypost/object.rb', line 49 def [](k) k = k.to_sym if k.is_a?(String) @values[k] end |
#[]=(k, v) ⇒ Object
54 55 56 |
# File 'lib/easypost/object.rb', line 54 def []=(k, v) send(:"#{k}=", v) end |
#as_json(options = {}) ⇒ Object
70 71 72 |
# File 'lib/easypost/object.rb', line 70 def as_json( = {}) @values.as_json end |
#each(&blk) ⇒ Object
78 79 80 |
# File 'lib/easypost/object.rb', line 78 def each(&blk) @values.each(&blk) end |
#id ⇒ Object
86 87 88 |
# File 'lib/easypost/object.rb', line 86 def id @values[:id] end |
#id=(id) ⇒ Object
82 83 84 |
# File 'lib/easypost/object.rb', line 82 def id=(id) @values[:id] = id end |
#inspect ⇒ Object
28 29 30 31 |
# File 'lib/easypost/object.rb', line 28 def inspect() id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : "" "#<#{self.class}:#{id_string}> JSON: " + MultiJson.dump(@values, :pretty => true) end |
#keys ⇒ Object
58 59 60 |
# File 'lib/easypost/object.rb', line 58 def keys @values.keys end |
#refresh_from(values, api_key, partial = false) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/easypost/object.rb', line 33 def refresh_from(values, api_key, partial=false) @api_key = api_key added = Set.new(values.keys - @values.keys) instance_eval do add_accessors(added) end values.each do |k, v| @values[k] = Util.convert_to_easypost_object(v, api_key, self, k) @transient_values.delete(k) @unsaved_values.delete(k) end end |
#to_hash ⇒ Object
74 75 76 |
# File 'lib/easypost/object.rb', line 74 def to_hash @values end |
#to_json(options = {}) ⇒ Object
66 67 68 |
# File 'lib/easypost/object.rb', line 66 def to_json( = {}) MultiJson.dump(@values) end |
#to_s(*args) ⇒ Object
24 25 26 |
# File 'lib/easypost/object.rb', line 24 def to_s(*args) MultiJson.dump(@values, :pretty => true) end |
#values ⇒ Object
62 63 64 |
# File 'lib/easypost/object.rb', line 62 def values @values.values end |