Class: Dropwallet::Core::Model
- Inherits:
-
Object
- Object
- Dropwallet::Core::Model
- Includes:
- ActiveModel::Serialization, ActiveModel::Validations
- Defined in:
- lib/dropwallet/core/model.rb
Class Method Summary collapse
- .all ⇒ Object
- .baseServiceUrl ⇒ Object
- .create(attributes = {}, options = {}) ⇒ Object
- .debug? ⇒ Boolean
- .field(name, options = {}) ⇒ Object
-
.find(id) ⇒ Object
Default Restful Calls.
- .serviceUrl ⇒ Object
Instance Method Summary collapse
-
#initialize(attributes = {}, options = {}) ⇒ Model
constructor
attr_accessor :attributes.
- #read_attribute(key) ⇒ Object
- #read_attribute_for_validation(key) ⇒ Object
- #to_json ⇒ Object
- #to_s ⇒ Object
- #write_attribute(key, value) ⇒ Object
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 = {}, = {}) @attributes = attributes defaults = {:new => true} = defaults.merge() @new = [:new] return @attributes end |
Class Method Details
.all ⇒ Object
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 |
.baseServiceUrl ⇒ Object
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 = {}, = {}) model = self.new(attributes, ) return model end |
.debug? ⇒ 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, = {}) 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 |
.serviceUrl ⇒ Object
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_json ⇒ Object
64 65 66 |
# File 'lib/dropwallet/core/model.rb', line 64 def to_json return @attributes.to_json end |
#to_s ⇒ Object
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 |