map 6.6.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,140 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- class Map
3
- module Integrations
4
- module ActiveRecord
5
- def self.included( klass )
6
- klass.extend ClassMethods
7
- end
8
-
9
- module ClassMethods
10
- def to_map( record , *args )
11
- # prep
12
- model = record.class
13
- map = Map.new
14
- map[ :model ] = model.name.underscore
15
- map[ :id ] = record.id
16
-
17
- # yank out options if they are patently obvious...
18
- if args.size == 2 and args.first.is_a?( Array ) and args.last.is_a?( Hash )
19
- options = Map.for args.last
20
- args = args.first
21
- else
22
- options = nil
23
- end
24
-
25
- # get base to_dao from class
26
- base = column_names
27
-
28
- # available options keys
29
- opts = %w( include includes with exclude excludes without )
30
-
31
- # proc to remove options
32
- extract_options =
33
- proc do |array|
34
- to_return = Map.new
35
- last = array.last
36
- if last.is_a?( Hash )
37
- last = Map.for last
38
- if opts.any? { | opt | last.has_key? opt }
39
- array.pop
40
- to_return = last
41
- end
42
- end
43
- to_return
44
- end
45
-
46
- # handle case where options are bundled in args...
47
- options ||= extract_options[args]
48
-
49
- # use base options iff none provided
50
- base_options = extract_options[base]
51
- if options.blank? and !base_options.blank?
52
- options = base_options
53
- end
54
-
55
- # refine the args with includes iff found in options
56
- include_opts = [ :include , :includes , :with ]
57
- if options.any? { | option | include_opts.include? option.to_sym }
58
- args.replace( base ) if args.empty?
59
- args.push( options[ :include ] ) if options[ :include ]
60
- args.push( options[ :includes ] ) if options[ :includes ]
61
- args.push( options[ :with ] ) if options[ :with ]
62
- end
63
-
64
- # take passed in args or model defaults
65
- list = args.empty? ? base : args
66
- list = column_names if list.empty?
67
-
68
- # proc to ensure we're all mapped out
69
- map_nested =
70
- proc do | value , *args |
71
- if value.is_a?( Array )
72
- value.map { | v | map_nested[ v , *args ] }
73
- else
74
- if value.respond_to? :to_map
75
- value.to_map *args
76
- else
77
- value
78
- end
79
- end
80
- end
81
-
82
- # okay - go!
83
- list.flatten.each do | attr |
84
- if attr.is_a?( Array )
85
- related , *argv = attr
86
- v = record.send related
87
- value = map_nested[ value , *argv ]
88
- map[ related ] = value
89
- next
90
- end
91
-
92
- if attr.is_a?( Hash )
93
- attr.each do | related , argv |
94
- v = record.send related
95
- argv = !argv.is_a?( Array ) ? [ argv ] : argv
96
- value = map_nested[ v , *argv ]
97
- map[ related ] = value
98
- end
99
- next
100
- end
101
-
102
- value = record.send attr
103
-
104
- if value.respond_to?( :to_map )
105
- map[ attr ] = value.to_map
106
- next
107
- end
108
-
109
- if value.is_a?( Array )
110
- map[ attr ] = value.map &map_nested
111
- next
112
- end
113
-
114
- map[ attr ] = value
115
- end
116
-
117
- # refine the map with excludes iff passed as options
118
- exclude_opts = [ :exclude , :excludes , :without ]
119
- if options.any? { | option | exclude_opts.include? option.to_sym }
120
- [ options[ :exclude ] , options[ :excludes ] , options[ :without ] ].each do | paths |
121
- paths = Array paths
122
- next if paths.blank?
123
- paths.each { | path | map.rm path }
124
- end
125
- end
126
-
127
- map
128
- end
129
- end
130
-
131
- def to_map( *args )
132
- self.class.to_map self , *args
133
- end
134
- end
135
- end
136
- end
137
-
138
- if defined?( ActiveRecord::Base )
139
- ActiveRecord::Base.send :include , Map::Integrations::ActiveRecord
140
- end
data/lib/map/params.rb DELETED
@@ -1,79 +0,0 @@
1
- class Map
2
- def param_for(*args, &block)
3
- options = Map.options_for!(args)
4
-
5
- prefix = options[:prefix] || 'map'
6
-
7
- src_key = args.flatten.map{|arg| Map.alphanumeric_key_for(arg)}
8
-
9
- dst_key = src_key.map{|k| k.is_a?(Numeric) ? 0 : k}
10
-
11
- src = self
12
- dst = Map.new
13
-
14
- value =
15
- if options.has_key?(:value)
16
- options[:value]
17
- else
18
- src.get(src_key).to_s
19
- end
20
-
21
- dst.set(dst_key, value)
22
-
23
- Param.param_for(dst, prefix)
24
- end
25
-
26
- def name_for(*args, &block)
27
- options = Map.options_for!(args)
28
- options[:value] = nil
29
- args.push(options)
30
- param_for(*args, &block)
31
- end
32
-
33
- def to_params
34
- to_params = Array.new
35
-
36
- depth_first_each do |key, val|
37
- to_params.push(param_for(key))
38
- end
39
-
40
- to_params.join('&')
41
- end
42
-
43
- module Param
44
- def param_for(value, prefix = nil)
45
- case value
46
- when Array
47
- value.map { |v|
48
- param_for(v, "#{ prefix }[]")
49
- }.join("&")
50
-
51
- when Hash
52
- value.map { |k, v|
53
- param_for(v, prefix ? "#{ prefix }[#{ escape(k) }]" : escape(k))
54
- }.join("&")
55
-
56
- when String
57
- raise ArgumentError, "value must be a Hash" if prefix.nil?
58
- "#{ prefix }=#{ escape(value) }"
59
-
60
- else
61
- prefix
62
- end
63
- end
64
-
65
- if(''.respond_to?(:bytesize))
66
- def bytesize(string) string.bytesize end
67
- else
68
- def bytesize(string) string.size end
69
- end
70
-
71
- require 'uri' unless defined?(URI)
72
-
73
- def escape(s)
74
- URI.encode_www_form_component(s)
75
- end
76
-
77
- extend(self)
78
- end
79
- end
data/lib/map/struct.rb DELETED
@@ -1,52 +0,0 @@
1
- # -*- encoding : utf-8 -*-
2
- class Map
3
- class Struct
4
- instance_methods.each { |m| undef_method m unless m =~ /^__|object_id/ }
5
-
6
- attr :map
7
-
8
- def initialize(map)
9
- @map = map
10
- end
11
-
12
- def method_missing(method, *args, &block)
13
- method = method.to_s
14
- case method
15
- when /=$/
16
- key = method.chomp('=')
17
- value = args.shift
18
- @map[key] = value
19
- when /\?$/
20
- key = method.chomp('?')
21
- value = @map.has?( key )
22
- else
23
- key = method
24
- raise(IndexError, key) unless @map.has_key?(key)
25
- value = @map[key]
26
- end
27
- value.is_a?(Map) ? value.struct : value
28
- end
29
-
30
- Keys = lambda{|*keys| keys.flatten!; keys.compact!; keys.map!{|key| key.to_s}} unless defined?(Keys)
31
-
32
- delegates = %w(
33
- inspect
34
- )
35
-
36
- delegates.each do |delegate|
37
- module_eval <<-__, __FILE__, __LINE__
38
- def #{ delegate }(*args, &block)
39
- @map.#{ delegate }(*args, &block)
40
- end
41
- __
42
- end
43
- end
44
-
45
- def struct
46
- @struct ||= Struct.new(self)
47
- end
48
-
49
- def Map.struct(*args, &block)
50
- new(*args, &block).struct
51
- end
52
- end