Class: Postmaster::PostmasterObject

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/postmaster/postmaster_object.rb

Direct Known Subclasses

APIResource

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id = nil) ⇒ PostmasterObject

Returns a new instance of PostmasterObject.



10
11
12
13
14
15
16
17
# File 'lib/postmaster/postmaster_object.rb', line 10

def initialize(id=nil)
  @values = {}
  # This really belongs in APIResource, but not putting it there allows us
  # to have a unified inspect method
  @unsaved_values = Set.new
  @transient_values = Set.new
  self.id = id if id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (protected)



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/postmaster/postmaster_object.rb', line 164

def method_missing(name, *args)
  if name.to_s.end_with?('=')
    attr = name.to_s[0...-1].to_sym
    @values[attr] = args[0]
    add_accessors([attr])
    return
  else
    return @values[name] if @values.has_key?(name)
  end

  super
end

Class Method Details

.construct_from(values) ⇒ Object



19
20
21
22
23
# File 'lib/postmaster/postmaster_object.rb', line 19

def self.construct_from(values)
  obj = self.new(values[:id])
  obj.refresh_from(values)
  obj
end

Instance Method Details

#[](k) ⇒ Object



87
88
89
90
# File 'lib/postmaster/postmaster_object.rb', line 87

def [](k)
  k = k.to_sym if k.kind_of?(String)
  @values[k]
end

#[]=(k, v) ⇒ Object



92
93
94
95
# File 'lib/postmaster/postmaster_object.rb', line 92

def []=(k, v)
  k = k.to_sym if k.kind_of?(String)
  send(:"#{k}=", v)
end

#delete(k) ⇒ Object



102
103
104
105
106
# File 'lib/postmaster/postmaster_object.rb', line 102

def delete(k)
  k = k.to_sym if k.kind_of?(String)
  @values.delete(k)
  remove_accessors([k])
end

#each(&blk) ⇒ Object



132
133
134
# File 'lib/postmaster/postmaster_object.rb', line 132

def each(&blk)
  @values.each(&blk)
end

#has_key?(k) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
100
# File 'lib/postmaster/postmaster_object.rb', line 97

def has_key?(k)
  k = k.to_sym if k.kind_of?(String)
  @values.has_key?(k)
end

#inspectObject



29
30
31
32
# File 'lib/postmaster/postmaster_object.rb', line 29

def inspect()
  id_string = (self.respond_to?(:id) && !self.id.nil?) ? " id=#{self.id}" : ""
  "#<#{self.class}:0x#{self.object_id.to_s(16)}#{id_string}> JSON: " + Postmaster::JSON.dump(@values, :pretty => true)
end

#keysObject



108
109
110
# File 'lib/postmaster/postmaster_object.rb', line 108

def keys
  @values.keys
end

#refresh_from(values) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/postmaster/postmaster_object.rb', line 34

def refresh_from(values)
  # which keys should be converted to Postmaster_Objects
  obj_keys = {
    'Postmaster::Shipment.to' => Postmaster::Address,
    'Postmaster::Shipment.from' => Postmaster::Address,
    'Postmaster::Shipment.package' => Postmaster::Package,
    'Postmaster::AddressProposal.address' => Postmaster::Address,
    'Postmaster::Tracking.last_update' => 'DateTime',
    'Postmaster::TrackingHistory.timestamp' => 'DateTime',
    'Postmaster::TransitTime.delivery_timestamp' => 'DateTime'
  }
  
  # which keys should be converted to list of Postmaster_Objects
  obj_list_keys = {
    'Postmaster::AddressValidation.addresses' => Postmaster::Address,
    'Postmaster::Shipment.packages' => Postmaster::Package,
    'Postmaster::Tracking.history' => Postmaster::TrackingHistory
  }
    
  removed = Set.new(@values.keys - values.keys)
  added = Set.new(values.keys - @values.keys)
  # Wipe old state before setting new.  This is useful for e.g. updating a
  # customer, where there is no persistent card parameter.  Mark those values
  # which don't persist as transient

  instance_eval do
    remove_accessors(removed)
    add_accessors(added)
  end
  removed.each do |k|
    @values.delete(k)
  end
  values.each do |k, v|
    full_key = self.class.name + "." + k.to_s
    if obj_keys.has_key?(full_key)
      klass = obj_keys[full_key]
      if klass == 'DateTime'
        @values[k] = DateTime.strptime(v.to_s, '%s')
      else
        @values[k] = klass.construct_from(v)
      end
    elsif obj_list_keys.has_key?(full_key)
      klass = obj_list_keys[full_key]
      @values[k] = []
      v.each do |i|
        @values[k].push(klass.construct_from(i))
      end
    else
      @values[k] = v
    end
  end
end

#to_hashObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/postmaster/postmaster_object.rb', line 120

def to_hash
  result = @values.clone
  result.each do |k, v|
    if v.kind_of? Postmaster::PostmasterObject
      result[k] = v.to_hash
    elsif v.kind_of? Array and v[0].kind_of? Postmaster::PostmasterObject
      result[k] = v.map { |i| i.to_hash }
    end
  end
  result
end

#to_json(*a) ⇒ Object



116
117
118
# File 'lib/postmaster/postmaster_object.rb', line 116

def to_json(*a)
  Postmaster::JSON.dump(@values)
end

#to_s(*args) ⇒ Object



25
26
27
# File 'lib/postmaster/postmaster_object.rb', line 25

def to_s(*args)
  Postmaster::JSON.dump(@values, :pretty => true)
end

#valuesObject



112
113
114
# File 'lib/postmaster/postmaster_object.rb', line 112

def values
  @values.values
end