Class: MageHand::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ob_port/base.rb

Direct Known Subclasses

Campaign, User, WikiPage

Constant Summary collapse

@@simple_attributes =
{}
@@instance_attributes =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, attributes = nil) ⇒ Base

Returns a new instance of Base.



17
18
19
20
# File 'lib/ob_port/base.rb', line 17

def initialize(client, attributes=nil)
  self.client = client
  update_attributes!(attributes)
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/ob_port/base.rb', line 3

def client
  @client
end

Class Method Details

.attr_array(method_name, options = {}) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ob_port/base.rb', line 98

def self.attr_array(method_name, options={})
  self.class_eval do
    name = method_name.to_s
    class_name = options[: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(client, #{name.singularize})
        end
      end
    CODE
    puts code if ENV['DEBUG']
    module_eval code
  end
end

.attr_instance(method_name, options = {}) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ob_port/base.rb', line 77

def self.attr_instance(method_name, options={})
   self.class_eval do
    name = method_name.to_s
    class_name = options[:class_name] || name.classify
    code = <<-CODE
      def #{name}
        @#{name}
      end

      def #{name}=(new_#{name})
        @#{name} = #{class_name}.new(client, 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



7
8
9
10
11
12
13
# File 'lib/ob_port/base.rb', line 7

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

.attributesObject



58
59
60
# File 'lib/ob_port/base.rb', line 58

def self.attributes
  simple_attributes + instance_attributes
end

.inflate_if_nil(*method_names) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ob_port/base.rb', line 119

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_attributesObject



50
51
52
# File 'lib/ob_port/base.rb', line 50

def self.instance_attributes
  @@instance_attributes[self.name] || []
end

.model_nameObject



38
39
40
# File 'lib/ob_port/base.rb', line 38

def self.model_name
  @_model_name ||= ActiveModel::Name.new(self)
end

.simple_attributesObject



42
43
44
# File 'lib/ob_port/base.rb', line 42

def self.simple_attributes
  @@simple_attributes[self.name] || []
end

Instance Method Details

#attributesObject



62
63
64
# File 'lib/ob_port/base.rb', line 62

def attributes
  self.class.attributes
end

#inflateObject



30
31
32
33
34
35
36
# File 'lib/ob_port/base.rb', line 30

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(client.access_token.get(individual_url).body)
  update_attributes!(hash)
end

#instance_attributesObject



54
55
56
# File 'lib/ob_port/base.rb', line 54

def instance_attributes
  self.class.instance_attributes
end

#simple_attributesObject



46
47
48
# File 'lib/ob_port/base.rb', line 46

def simple_attributes
  self.class.simple_attributes
end

#to_json(*a) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/ob_port/base.rb', line 66

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



22
23
24
25
26
27
28
# File 'lib/ob_port/base.rb', line 22

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