Class: Dropwallet::Core::Model

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, ActiveModel::Validations
Defined in:
lib/dropwallet/core/model.rb

Direct Known Subclasses

Product, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}, options = {}) ⇒ Model

attr_accessor :attributes



7
8
9
10
11
12
13
# File 'lib/dropwallet/core/model.rb', line 7

def initialize(attributes = {}, options = {})
  @attributes = attributes
  defaults = {:new => true}
  options = defaults.merge(options)
  @new = options[:new]
  return @attributes
end

Class Method Details

.allObject



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dropwallet/core/model.rb', line 77

def self.all()
  items = JSON.parse(RestClient.get("#{serviceUrl}", {:accept => :json}))
  if debug?
    puts item.to_s
  end
  objects = []
  items.each do |item|
    objects << self.new(item)
  end
  return items
end

.baseServiceUrlObject



45
46
47
# File 'lib/dropwallet/core/model.rb', line 45

def self.baseServiceUrl
  "https://#{Dropwallet::username}:#{Dropwallet::password}@api.dropwallet.net"
end

.create(attributes = {}, options = {}) ⇒ Object



15
16
17
18
19
# File 'lib/dropwallet/core/model.rb', line 15

def self.create(attributes = {}, options = {})
  model = self.new(attributes, options)

  return model
end

.debug?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/dropwallet/core/model.rb', line 54

def self.debug?
  true
end

.field(name, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/dropwallet/core/model.rb', line 21

def self.field(name, options = {})
    name = name.to_s
    define_method("#{name}=") do |value|
      write_attribute(name, value)
    end
    define_method(name) do
      read_attribute(name)
    end
end

.find(id) ⇒ Object

Default Restful Calls



69
70
71
72
73
74
75
# File 'lib/dropwallet/core/model.rb', line 69

def self.find(id)
  item = JSON.parse(RestClient.get("#{serviceUrl}/#{id}", {:accept => :json}))
  if debug?
    puts item.to_s
  end
  return self.new(item)
end

.serviceUrlObject



49
50
51
52
# File 'lib/dropwallet/core/model.rb', line 49

def self.serviceUrl
  className = self.name.split('::').last.downcase
  "#{baseServiceUrl}/#{className}s"
end

Instance Method Details

#read_attribute(key) ⇒ Object



35
36
37
# File 'lib/dropwallet/core/model.rb', line 35

def read_attribute(key)
  @attributes[key.to_s]
end

#read_attribute_for_validation(key) ⇒ Object



31
32
33
# File 'lib/dropwallet/core/model.rb', line 31

def read_attribute_for_validation(key)
  return read_attribute(key.to_s)
end

#to_jsonObject



64
65
66
# File 'lib/dropwallet/core/model.rb', line 64

def to_json
  return @attributes.to_json
end

#to_sObject



60
61
62
# File 'lib/dropwallet/core/model.rb', line 60

def to_s
  return @attributes.to_s
end

#write_attribute(key, value) ⇒ Object



41
42
43
# File 'lib/dropwallet/core/model.rb', line 41

def write_attribute(key, value)
  @attributes[key.to_s] = value
end