map 6.0.0 → 6.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/map.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  class Map < Hash
3
- Version = '6.0.0' unless defined?(Version)
3
+ Version = '6.0.1' unless defined?(Version)
4
4
  Load = Kernel.method(:load) unless defined?(Load)
5
5
 
6
6
  class << Map
@@ -0,0 +1,140 @@
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/map.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "map"
6
- spec.version = "6.0.0"
6
+ spec.version = "6.0.1"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "map"
9
9
  spec.description = "description: map kicks the ass"
@@ -17,6 +17,8 @@ Gem::Specification::new do |spec|
17
17
  "lib",
18
18
  "lib/map",
19
19
  "lib/map.rb",
20
+ "lib/map/integrations",
21
+ "lib/map/integrations/active_record.rb",
20
22
  "lib/map/options.rb",
21
23
  "lib/map/struct.rb",
22
24
  "map.gemspec",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: map
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.0
4
+ version: 6.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-05-23 00:00:00.000000000 Z
12
+ date: 2012-05-28 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! 'description: map kicks the ass'
15
15
  email: ara.t.howard@gmail.com
@@ -23,6 +23,7 @@ files:
23
23
  - TODO
24
24
  - a.rb
25
25
  - lib/map.rb
26
+ - lib/map/integrations/active_record.rb
26
27
  - lib/map/options.rb
27
28
  - lib/map/struct.rb
28
29
  - map.gemspec