Module: EventBright::ApiObjectClassMethods

Included in:
ApiObject
Defined in:
lib/eventbright/api_object_class_methods.rb

Instance Method Summary collapse

Instance Method Details

#collection(args = {}) ⇒ Object

Defines a has may relation



102
103
104
105
106
107
108
109
110
111
# File 'lib/eventbright/api_object_class_methods.rb', line 102

def collection(args = {})
  @class_collections ||= {}
  args.each{|symbol, klass|
    module_eval( "def #{symbol}(); collection_get(:#{symbol});  end")
    module_eval( "def dirty_#{symbol}!(); collection_dirty!(:#{symbol});  end")
    module_eval( "def dirty_#{symbol}?(); collection_dirty?(:#{symbol});  end")
    module_eval( "def #{symbol}=(val, no_dirty = false); collection_set(:#{symbol}, val, no_dirty); end")
    @class_collections[symbol] = klass
  }
end

#collectionsObject



112
113
114
# File 'lib/eventbright/api_object_class_methods.rb', line 112

def collections
  @class_collections || {}
end

#has(args = {}) ⇒ Object

Defines a has 1 relation



87
88
89
90
91
92
93
94
95
96
# File 'lib/eventbright/api_object_class_methods.rb', line 87

def has(args = {})
  @class_relations ||= {}
  args.each{|symbol, klass|
    module_eval( "def #{symbol}(); relation_get(:#{symbol});  end")
    module_eval( "def dirty_#{symbol}!(); relation_dirty!(:#{symbol});  end")
    module_eval( "def dirty_#{symbol}?(); relation_dirty?(:#{symbol});  end")
    module_eval( "def #{symbol}=(val, no_dirty = false); relation_set(:#{symbol}, val, no_dirty); end")
    @class_relations[symbol] = klass
  }
end

#ignores(*args) ⇒ Object



13
14
15
16
17
# File 'lib/eventbright/api_object_class_methods.rb', line 13

def ignores(*args)
  @ignores ||= []
  @ignores.concat(args) unless args.empty?
  @ignores
end

#plural_name(name = false) ⇒ Object



8
9
10
11
# File 'lib/eventbright/api_object_class_methods.rb', line 8

def plural_name(name = false)
  @plural_name = name if name
  @plural_name || "#{self.singlet_name}s"
end

#readable(*args) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/eventbright/api_object_class_methods.rb', line 49

def readable(*args)
  args.each{|symbol|
  
    module_eval( "def #{symbol}(); attribute_get(:#{symbol}); end")
    module_eval( "def #{symbol}=(val, no_dirty = false); attribute_set(:#{symbol}, val, true); end")
  }
end

#readable_date(*args) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/eventbright/api_object_class_methods.rb', line 65

def readable_date(*args)
  args.each{|symbol|
  
    module_eval( "def #{symbol}(); EventBright.formatted_time(attribute_get(:#{symbol})); end")
    module_eval( "def #{symbol}=(val, no_dirty = false); attribute_set(:#{symbol}, Time.parse(val), true); end")
  }
end

#reformats(*args) ⇒ Object

Columns to reformat when sending outgoing data (Reformatting is assumed to be done by calling the method with the same name as the attribute, so to reformat foo, use def foo… with an attribute_get inside)



29
30
31
32
33
# File 'lib/eventbright/api_object_class_methods.rb', line 29

def reformats(*args)
  @reformats ||= []
  @reformats.concat(args) unless args.empty?
  @reformats
end

#relationsObject



97
98
99
# File 'lib/eventbright/api_object_class_methods.rb', line 97

def relations
  @class_relations || {}
end

#remap(args = {}) ⇒ Object

Columns that are the same as other columns. This is mainly useful for incoming data with inconsistent naming. Args are passed as a hash, where the key is the new method name, and the value is the target method name you are mapping the new one onto. Note that this means there is only the original one stored on the object. Also note this is different from the renames list, which is exclusively for outgoing hashes sent to the API.



79
80
81
82
83
84
# File 'lib/eventbright/api_object_class_methods.rb', line 79

def remap(args = {})
  args.each{|k,v|
    module_eval( "def #{k}(); #{v};  end")
    module_eval( "def #{k}=(val,no_dirty = false); self.__send__('#{v}=', val, no_dirty); end")
  }
end

#renames(attrs = false) ⇒ Object

Columns to rename when sending outgoing data



36
37
38
39
40
# File 'lib/eventbright/api_object_class_methods.rb', line 36

def renames(attrs = false)
  @renames ||= {}
  @renames.merge!(attrs) if attrs
  @renames
end

#requires(*args) ⇒ Object



19
20
21
22
23
# File 'lib/eventbright/api_object_class_methods.rb', line 19

def requires(*args)
  @requires ||= []
  @requires.concat(args) unless args.empty?
  @requires
end

#singlet_name(name = false) ⇒ Object



3
4
5
6
# File 'lib/eventbright/api_object_class_methods.rb', line 3

def singlet_name(name = false)
  @singlet_name = name if name
  @singlet_name || self.to_s.gsub('EventBright::', '').downcase
end

#updatable(*args) ⇒ Object



42
43
44
45
46
47
# File 'lib/eventbright/api_object_class_methods.rb', line 42

def updatable(*args)
  args.each{|symbol|
    module_eval( "def #{symbol}(); attribute_get(:#{symbol});  end")
    module_eval( "def #{symbol}=(val, no_dirty = false); attribute_set(:#{symbol}, val, no_dirty); end")
  }
end

#updatable_date(*args) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/eventbright/api_object_class_methods.rb', line 57

def updatable_date(*args)
  args.each{|symbol|
  
    module_eval( "def #{symbol}(); EventBright.formatted_time(attribute_get(:#{symbol})); end")
    module_eval( "def #{symbol}=(val, no_dirty = false); attribute_set(:#{symbol}, Time.parse(val), no_dirty); end")
  }
end