Module: RouteNGN::ClassMethods

Defined in:
lib/routengn/mapper.rb,
lib/routengn/uploader.rb

Overview

InstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



60
61
62
# File 'lib/routengn/mapper.rb', line 60

def fields
  @fields
end

#primary_fieldObject (readonly)

Returns the value of attribute primary_field.



61
62
63
# File 'lib/routengn/mapper.rb', line 61

def primary_field
  @primary_field
end

Instance Method Details

#all(opts = {}) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/routengn/mapper.rb', line 126

def all(opts = {})
  result = []

  response = RouteNGN.get base_url, opts

  data = response['data']

  return [new_from_saved(data)] unless data.is_a? Array

  data.each do |d|
    result << new_from_saved(d)
  end

  result
end

#base_urlObject



80
81
82
# File 'lib/routengn/mapper.rb', line 80

def base_url
  "/#{name.downcase.pluralize}"
end

#belongs_to(klass, opts = {}) ⇒ Object

This isn’t really a klass (in the traditional sense) but a symbol rather… so we need to_s



96
97
98
99
100
# File 'lib/routengn/mapper.rb', line 96

def belongs_to(klass, opts = {})  # This isn't really a klass (in the traditional sense) but a symbol rather... so we need to_s
  attr = opts[:column] ? opts[:column] : :"#{klass}_id" 
  field attr
  define_method(klass) { klass.to_s.camelize.constantize.first :id => send(attr) }
end

#delete(id) ⇒ Object



112
113
114
115
# File 'lib/routengn/mapper.rb', line 112

def delete(id)
  response = RouteNGN.delete base_url, :id => id
  response.success?
end

#field(name, opts = {}) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/routengn/mapper.rb', line 84

def field(name, opts = {})
  @fields ||= []
  return if @fields.include? name
  @fields << name
  define_method(name) { instance_variable_get :"@#{name}" }
  define_method(:"#{name}=") { |val| instance_variable_set :"@#{name}", val }
  if opts[:primary] # TODO prevent multiple primaries
    alias_method :primary, name
    @primary_field = name
  end
end

#find(*args) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/routengn/mapper.rb', line 117

def find(*args)
  case args.first
  when :all
    all(*args[1..-1])
  else
    first(*args[1..-1])
  end
end

#first(opts = {}) ⇒ Object



142
143
144
# File 'lib/routengn/mapper.rb', line 142

def first(opts = {})
  all(opts).first # optimize/simplify?
end

#has_many(klass, opts = {}) ⇒ Object

This isn’t really a klass (in the traditional sense) but a symbol rather… so we need to_s



107
108
109
110
# File 'lib/routengn/mapper.rb', line 107

def has_many(klass, opts = {})  # This isn't really a klass (in the traditional sense) but a symbol rather... so we need to_s
  attr = opts[:column] ? opts[:column] : :"#{name}_id"
  define_method(klass.to_s.pluralize.to_sym) { klass.to_s.singularize.camelize.constantize.all attr => primary }
end

#has_one(klass, opts = {}) ⇒ Object

This isn’t really a klass (in the traditional sense) but a symbol rather… so we need to_s



102
103
104
105
# File 'lib/routengn/mapper.rb', line 102

def has_one(klass, opts = {})  # This isn't really a klass (in the traditional sense) but a symbol rather... so we need to_s
  attr = opts[:column] ? opts[:column] : :"#{name}_id"
  define_method(klass) { klass.to_s.camelize.constantize.first attr => primary }
end

#new(opts = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/routengn/mapper.rb', line 63

def new(opts = {})
  instance = super() # don't want implicit args
  @fields.each do |field|
    field = field.to_s
    next unless opts.has_key? field
    instance.send :"#{field}=", opts[field]
  end
  raise PrimaryFieldException.new unless instance.respond_to? :primary
  instance
end

#new_from_saved(opts = {}) ⇒ Object



74
75
76
77
78
# File 'lib/routengn/mapper.rb', line 74

def new_from_saved(opts = {})
  instance = new opts
  instance.saved = true
  instance
end

#upload(file, params = {}) ⇒ Object



11
12
13
14
15
16
# File 'lib/routengn/uploader.rb', line 11

def upload(file, params = {})
  data, headers = Multipart::Post.prepare_query("title" => 'title', "uploaded_data" => File.new(file))
  puts data.inspect
  puts headers.inspect
  RouteNGN.connection.access_token.post("/upload#{base_url}", data, headers)
end