Class: Flow::Models::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/flow/models/model.rb

Direct Known Subclasses

Card, Pool, Transaction

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, model_hash) ⇒ Model

Returns a new instance of Model.



9
10
11
12
# File 'lib/flow/models/model.rb', line 9

def initialize(connection, model_hash)
  @connection = connection
  @model_hash = model_hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/flow/models/model.rb', line 24

def method_missing(meth, *args, &block)
  if model_hash.key?(meth.to_s)
    return Model.load_model(meth, connection, model_hash[meth.to_s])
  end

  super
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



7
8
9
# File 'lib/flow/models/model.rb', line 7

def connection
  @connection
end

#model_hashObject (readonly)

Returns the value of attribute model_hash.



6
7
8
# File 'lib/flow/models/model.rb', line 6

def model_hash
  @model_hash
end

Class Method Details

.__load_pathObject



36
37
38
39
40
41
# File 'lib/flow/models/model.rb', line 36

def __load_path
  # Really wish I had Rail's pluralize here, but this
  # fits all of our cases for now and should fit most
  # if not all of our cases going forward.
  "#{__model_name}s"
end

.__model_nameObject



43
44
45
46
47
# File 'lib/flow/models/model.rb', line 43

def __model_name
  model_name = self.name.split('::').last.downcase
  raise "should not call this for abstract model" if model_name == 'model'
  model_name
end

.build(connection, model_hash) ⇒ Object



49
50
51
52
53
# File 'lib/flow/models/model.rb', line 49

def build(connection, model_hash)
  # Default behavior is just to create an instance
  # of the class.
  self.new(connection, model_hash)
end

.load(connection, token) ⇒ Object



55
56
57
58
59
60
# File 'lib/flow/models/model.rb', line 55

def load(connection, token)
  r = connection.get("#{__load_path}/#{token}")
  raise Flow::Errors::LoadError.new(r.message) unless r.success?

  return r.send(__model_name)
end

.load_model(name, connection, model_hash) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/flow/models/model.rb', line 62

def load_model(name, connection, model_hash)
  name = name.to_sym

  case name
  when :pool
    return Flow::Models::Pool.build(connection, model_hash)
  when :card
    return Flow::Models::Card.build(connection, model_hash)
  when :transaction
    return Flow::Models::Transaction.build(connection, model_hash)
  else
    return model_hash
  end
end

Instance Method Details

#reloadObject



14
15
16
# File 'lib/flow/models/model.rb', line 14

def reload
  self.class.load(connection, self.token)
end

#reload!Object



18
19
20
21
22
# File 'lib/flow/models/model.rb', line 18

def reload!
  updated_version = self.reload
  @model_hash = updated_version.model_hash
  self
end