Class: Etapper::EtapAbstract

Inherits:
Object
  • Object
show all
Defined in:
lib/etapper/classes/etap_abstract.rb

Direct Known Subclasses

Account, JournalEntry

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = nil) ⇒ EtapAbstract

Returns a new instance of EtapAbstract.



7
8
9
10
# File 'lib/etapper/classes/etap_abstract.rb', line 7

def initialize(base = nil)
  @new = !base
  @base = base || eval("Etapper::API::#{self.class.cname}.new")
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(attribute, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/etapper/classes/etap_abstract.rb', line 28

def method_missing(attribute, *args)
  # Try to get it from the defined values hash first, if one exists
  return definedValues[attribute] if respond_to?(:definedValues) and definedValues.has_key?(attribute)
  
  # Look in the base class next
  return base.send(attribute, *args) if base.respond_to?(attribute)

  # Finally, bubble up
  super(attribute, *args)
end

Class Method Details

.cnameObject



3
4
5
# File 'lib/etapper/classes/etap_abstract.rb', line 3

def self.cname
  self.name.gsub /^(.*::)/,''
end

.etap_read_only(*attributes) ⇒ Object

Creates a method that throws an Etapper::ReadOnlyError on attempts to assign that attribute



17
18
19
20
21
22
23
24
25
26
# File 'lib/etapper/classes/etap_abstract.rb', line 17

def self.etap_read_only(*attributes)
  attributes.each do |attribute|
    att = attribute.to_s
    class_eval <<-ENDDEF
    def #{att}=(val)
      raise Etapper::ReadOnlyError, "#{self.cname} #{att} is read-only!"
    end
    ENDDEF
  end
end

Instance Method Details

#==(val) ⇒ Object

Two eTapper classes are the same if they have the same base object



53
54
55
# File 'lib/etapper/classes/etap_abstract.rb', line 53

def ==(val)
  val.base == base
end

#baseObject



12
13
14
# File 'lib/etapper/classes/etap_abstract.rb', line 12

def base
  @base
end

#new?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/etapper/classes/etap_abstract.rb', line 39

def new?
  @new
end

#saveObject



43
44
45
46
47
48
49
50
# File 'lib/etapper/classes/etap_abstract.rb', line 43

def save
  if new?
    method = ("add" + self.class.cname).to_sym        
  else
    method = ("update" + self.class.cname).to_sym        
  end
  Client.instance.send(method, base, true)
end