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.
10 11 12 13 14 15 16 17 18 |
# File 'lib/easypost/object.rb', line 10 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.
7 8 9 |
# File 'lib/easypost/object.rb', line 7 def api_key @api_key end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/easypost/object.rb', line 7 def name @name end |
#parent ⇒ Object
Returns the value of attribute parent.
7 8 9 |
# File 'lib/easypost/object.rb', line 7 def parent @parent end |
#unsaved_values ⇒ Object
Returns the value of attribute unsaved_values.
7 8 9 |
# File 'lib/easypost/object.rb', line 7 def unsaved_values @unsaved_values end |
Class Method Details
.construct_from(values, api_key = nil, parent = nil, name = nil) ⇒ Object
20 21 22 23 24 |
# File 'lib/easypost/object.rb', line 20 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
51 52 53 |
# File 'lib/easypost/object.rb', line 51 def [](k) @values[k.to_s] end |
#[]=(k, v) ⇒ Object
55 56 57 |
# File 'lib/easypost/object.rb', line 55 def []=(k, v) send(:"#{k}=", v) end |
#as_json(options = {}) ⇒ Object
71 72 73 |
# File 'lib/easypost/object.rb', line 71 def as_json( = {}) @values.as_json end |
#each(&blk) ⇒ Object
79 80 81 |
# File 'lib/easypost/object.rb', line 79 def each(&blk) @values.each(&blk) end |
#id ⇒ Object
87 88 89 |
# File 'lib/easypost/object.rb', line 87 def id @values[:id] end |
#id=(id) ⇒ Object
83 84 85 |
# File 'lib/easypost/object.rb', line 83 def id=(id) @values[:id] = id end |
#inspect ⇒ Object
30 31 32 33 |
# File 'lib/easypost/object.rb', line 30 def inspect id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : "" "#<#{self.class}:#{id_string}> JSON: " + to_json end |
#keys ⇒ Object
59 60 61 |
# File 'lib/easypost/object.rb', line 59 def keys @values.keys end |
#refresh_from(values, api_key, partial = false) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/easypost/object.rb', line 35 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.to_s] = Util.convert_to_easypost_object(v, api_key, self, k) @transient_values.delete(k) @unsaved_values.delete(k) end end |
#to_hash ⇒ Object
75 76 77 |
# File 'lib/easypost/object.rb', line 75 def to_hash @values end |
#to_json(options = {}) ⇒ Object
67 68 69 |
# File 'lib/easypost/object.rb', line 67 def to_json( = {}) JSON.dump(@values) end |
#to_s(*args) ⇒ Object
26 27 28 |
# File 'lib/easypost/object.rb', line 26 def to_s(*args) JSON.dump(@values) end |
#values ⇒ Object
63 64 65 |
# File 'lib/easypost/object.rb', line 63 def values @values.values end |