Module: RouteNGN::ClassMethods
- Defined in:
- lib/routengn/mapper.rb,
lib/routengn/uploader.rb
Overview
InstanceMethods
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#primary_field ⇒ Object
readonly
Returns the value of attribute primary_field.
Instance Method Summary collapse
- #all(opts = {}) ⇒ Object
- #base_url ⇒ Object
-
#belongs_to(klass, opts = {}) ⇒ Object
This isn't really a klass (in the traditional sense) but a symbol rather...
- #delete(id) ⇒ Object
- #delete_all(opts = {}) ⇒ Object
- #field(name, opts = {}) ⇒ Object
- #find(*args) ⇒ Object
- #first(opts = {}) ⇒ Object
-
#has_many(klass, opts = {}) ⇒ Object
This isn't really a klass (in the traditional sense) but a symbol rather...
-
#has_one(klass, opts = {}) ⇒ Object
This isn't really a klass (in the traditional sense) but a symbol rather...
- #new(opts = {}) ⇒ Object
- #new_from_saved(opts = {}) ⇒ Object
- #upload(file, opts = {}) ⇒ Object
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
60 61 62 |
# File 'lib/routengn/mapper.rb', line 60 def fields @fields end |
#primary_field ⇒ Object (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
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/routengn/mapper.rb', line 132 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_url ⇒ Object
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
97 98 99 100 101 |
# File 'lib/routengn/mapper.rb', line 97 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
113 114 115 116 |
# File 'lib/routengn/mapper.rb', line 113 def delete(id) response = RouteNGN.delete base_url, :id => id response.success? end |
#delete_all(opts = {}) ⇒ Object
118 119 120 121 |
# File 'lib/routengn/mapper.rb', line 118 def delete_all(opts = {}) response = RouteNGN.delete base_url, opts response.success? end |
#field(name, opts = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 |
# 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] raise MultiplePrimaryFieldsException.new if @primary_field alias_method :primary, name @primary_field = name end end |
#find(*args) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/routengn/mapper.rb', line 123 def find(*args) case args.first when :all all(*args[1..-1]) else first(*args[1..-1]) end end |
#first(opts = {}) ⇒ Object
148 149 150 |
# File 'lib/routengn/mapper.rb', line 148 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
108 109 110 111 |
# File 'lib/routengn/mapper.rb', line 108 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.downcase}_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
103 104 105 106 |
# File 'lib/routengn/mapper.rb', line 103 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.downcase}_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, opts = {}) ⇒ Object
11 12 13 14 15 |
# File 'lib/routengn/uploader.rb', line 11 def upload(file, opts = {}) args = {:uploaded_data => File.new(file)}.merge! opts data, headers = HTTP::Multipart.prepare_query(args) RouteNGN.upload("/upload#{base_url}", data, headers) end |