Class: Ixtlan::Remote::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/ixtlan/remote/server.rb

Defined Under Namespace

Classes: Meta

Constant Summary collapse

NEW_METHOD =
Proc.new do | model, attributes |
  cond = {}
  model.key.each { |k| cond[ k.name ] = attributes[ k.name.to_s ] }
  m = model.first_or_new( cond )
  m.attributes = attributes
  m
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(factory) ⇒ Server

Returns a new instance of Server.



45
46
47
# File 'lib/ixtlan/remote/server.rb', line 45

def initialize( factory )
  @factory = factory
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



43
44
45
# File 'lib/ixtlan/remote/server.rb', line 43

def url
  @url
end

Instance Method Details

#add_model(clazz, path = nil) ⇒ Object



97
98
99
100
101
# File 'lib/ixtlan/remote/server.rb', line 97

def add_model( clazz, path = nil )
  @factory[ clazz ] = self
  m = map[ clazz ] = Meta.new( new_method( clazz ),
                               (path || clazz.to_s.underscore.pluralize ) )
end

#keys(clazz) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/ixtlan/remote/server.rb', line 103

def keys( clazz )
  # TODO
  if clazz.respond_to?( :key )
    clazz.key.first
  else
    clazz.id
  end
end

#mapObject



53
54
55
# File 'lib/ixtlan/remote/server.rb', line 53

def map
  @map ||= {}
end

#modelsObject



57
58
59
# File 'lib/ixtlan/remote/server.rb', line 57

def models
  map.keys
end

#new_method(clazz) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/ixtlan/remote/server.rb', line 87

def new_method(clazz)
  if defined? DataMapper
    new_method_dm(clazz)
  else
    warn "TODO need better implementation for ActiveRecord in #{__FILE__} #{__LINE__}"
  
    clazz.method( :new )
  end
end

#new_method_dm(clazz) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/ixtlan/remote/server.rb', line 79

def new_method_dm( clazz )
  if clazz.respond_to?( :key ) && clazz.key.kind_of?( DataMapper::PropertySet )
    Proc.new { |a| NEW_METHOD.call( clazz, a ) }
  else
    clazz.method( :new )
  end
end

#new_rest_resource(clazz) ⇒ Object



112
113
114
115
116
# File 'lib/ixtlan/remote/server.rb', line 112

def new_rest_resource( clazz )
  client = RestClient::Resource.new( @url, options )
  meta = map[ clazz ]
  Resource.new( client[ meta.path ], clazz, meta.new_method )
end

#optionsObject



49
50
51
# File 'lib/ixtlan/remote/server.rb', line 49

def options
  @options ||= {}
end