Class: MageHand::Base
- Inherits:
-
Object
- Object
- MageHand::Base
- Defined in:
- lib/ob_port/base.rb
Constant Summary collapse
- @@simple_attributes =
{}
- @@instance_attributes =
{}
Class Method Summary collapse
- .attr_array(method_name, options = {}) ⇒ Object
- .attr_instance(method_name, options = {}) ⇒ Object
- .attr_simple(*method_names) ⇒ Object
- .attributes ⇒ Object
- .inflate_if_nil(*method_names) ⇒ Object
- .instance_attributes ⇒ Object
- .model_name ⇒ Object
- .simple_attributes ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
- #inflate ⇒ Object
-
#initialize(attributes = nil) ⇒ Base
constructor
A new instance of Base.
- #instance_attributes ⇒ Object
- #simple_attributes ⇒ Object
- #to_json(*a) ⇒ Object
- #update_attributes!(attributes) ⇒ Object
Constructor Details
#initialize(attributes = nil) ⇒ Base
Returns a new instance of Base.
16 17 18 |
# File 'lib/ob_port/base.rb', line 16 def initialize(attributes=nil) update_attributes!(attributes) end |
Class Method Details
.attr_array(method_name, options = {}) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ob_port/base.rb', line 96 def self.attr_array(method_name, ={}) self.class_eval do name = method_name.to_s class_name = [:class_name] || name.singularize.classify code = <<-CODE def #{name} @#{name} end def #{name}=(new_#{name}) @#{name} = [] new_#{name}.each do |#{name.singularize}| @#{name} << #{class_name}.new(#{name.singularize}) end end CODE puts code if ENV['DEBUG'] module_eval code end end |
.attr_instance(method_name, options = {}) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ob_port/base.rb', line 75 def self.attr_instance(method_name, ={}) self.class_eval do name = method_name.to_s class_name = [:class_name] || name.classify code = <<-CODE def #{name} @#{name} end def #{name}=(new_#{name}) @#{name} = #{class_name}.new(new_#{name.singularize}) end CODE puts code if ENV['DEBUG'] module_eval code @@instance_attributes[self.name] = [] unless @@instance_attributes[self.name] @@instance_attributes[self.name] << method_name end end |
.attr_simple(*method_names) ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/ob_port/base.rb', line 6 def self.attr_simple(*method_names) @@simple_attributes[self.name] = [] unless @@simple_attributes[self.name] method_names.each do |method_name| attr_accessor method_name @@simple_attributes[self.name] << method_name end end |
.attributes ⇒ Object
56 57 58 |
# File 'lib/ob_port/base.rb', line 56 def self.attributes simple_attributes + instance_attributes end |
.inflate_if_nil(*method_names) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/ob_port/base.rb', line 117 def self.inflate_if_nil(*method_names) self.class_eval do method_names.each do |method_name| alias_method "#{method_name.to_s}_original".to_sym, method_name define_method method_name do inflate if self.send("#{method_name.to_s}_original".to_sym).nil? self.send("#{method_name.to_s}_original".to_sym) end end end end |
.instance_attributes ⇒ Object
48 49 50 |
# File 'lib/ob_port/base.rb', line 48 def self.instance_attributes @@instance_attributes[self.name] || [] end |
.model_name ⇒ Object
36 37 38 |
# File 'lib/ob_port/base.rb', line 36 def self.model_name @_model_name ||= ActiveModel::Name.new(self) end |
.simple_attributes ⇒ Object
40 41 42 |
# File 'lib/ob_port/base.rb', line 40 def self.simple_attributes @@simple_attributes[self.name] || [] end |
Instance Method Details
#attributes ⇒ Object
60 61 62 |
# File 'lib/ob_port/base.rb', line 60 def attributes self.class.attributes end |
#inflate ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/ob_port/base.rb', line 28 def inflate #if we don't have an id, the opject has not been created on the server yet return unless self.id hash = JSON.parse( MageHand::get_client.access_token.get(individual_url).body) update_attributes!(hash) end |
#instance_attributes ⇒ Object
52 53 54 |
# File 'lib/ob_port/base.rb', line 52 def instance_attributes self.class.instance_attributes end |
#simple_attributes ⇒ Object
44 45 46 |
# File 'lib/ob_port/base.rb', line 44 def simple_attributes self.class.simple_attributes end |
#to_json(*a) ⇒ Object
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/ob_port/base.rb', line 64 def to_json(*a) json_hash = {} self.simple_attributes.each do |attribute| json_hash[attribute.to_s] = self.send(attribute) if self.send(attribute) end self.instance_attributes.each do |attribute| json_hash[attribute.to_s] = self.send(attribute).to_json if self.send(attribute) end json_hash.to_json(*a) end |
#update_attributes!(attributes) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/ob_port/base.rb', line 20 def update_attributes!(attributes) return unless attributes attributes.each do |key, value| setter = "#{key}=" self.send setter, value if self.respond_to?(setter) end end |