Module: Amee::Model

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/amee/model.rb', line 4

def self.included(base)
  base.extend ClassMethods
  base.__send__(:attr_writer, :session)
  base.__send__(:attr_accessor, :path)
  base.__send__(:attr_accessor, :uid)
  base.__send__(:attr_accessor, :name)
  base.__send__(:attr_accessor, :modified)
  base.__send__(:attr_accessor, :created)
  base.__send__(:attr_accessor, :resource_path)
  base.__send__(:attr_accessor, :lazy_loaded)
  
  base.class_eval do
    class  << base; attr_reader :lazy_populators end
    class  << base; attr_accessor :path_prefix end
    path_prefix = ""
  end
end

Instance Method Details

#full_pathObject



97
98
99
# File 'lib/amee/model.rb', line 97

def full_path
  self.class.path_prefix + resource_path
end

#initialize(hash = {}, session = nil) ⇒ Object



92
93
94
95
# File 'lib/amee/model.rb', line 92

def initialize(hash = {}, session = nil)
  @session = session
  populate_from_hash!(hash)
end

#populate!Object

Raises:

  • (::NotImplementedError)


105
106
107
# File 'lib/amee/model.rb', line 105

def populate!
  raise ::NotImplementedError, "#{self.class} included me and has not overriden me"
end

#populate_from_hash!(hash) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/amee/model.rb', line 109

def populate_from_hash!(hash)
  unless hash.nil? || hash.empty?
    populate_paths(hash["resource_path"], hash["path"])
    hash.each do |key, value|
      set_attr_method = "#{Amee::Utils::String.snake_case(key)}="
      if !value.nil? && respond_to?(set_attr_method)
        self.__send__(set_attr_method, value) 
      else
        # attempt to set attribute basically
      end
    end      
    @populated = true
  end
end

#populate_paths(resource_path, path) ⇒ Object

we want the resource paths populated along with the path  before we populate any other attribute as they may use these attributes



127
128
129
# File 'lib/amee/model.rb', line 127

def populate_paths(resource_path, path)
  self.resource_path, self.path = resource_path, path
end

#populated?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/amee/model.rb', line 101

def populated?
  @populated && lazy_loaded
end

#sessionObject

ClassMethods



88
89
90
# File 'lib/amee/model.rb', line 88

def session
  @session || (raise "Must bind this object to a Amee session before using")
end