ro 1.2.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NjU3NjMzOGVkNmQyZDFmNzk0MjBlOTc1MjRiYTFjYzhhOTFjM2E3NA==
4
+ NWFhMGNiNGMwNGRjYWYxZDg1NjgyMzgxMDQ4ZDIwZmY4Y2VjMjk5Nw==
5
5
  data.tar.gz: !binary |-
6
- ZWNmMmIyZjcwNTJkYmU0ZDJkZTYwZGYwYjE4MWEyZDdiMmM5Y2ZkMQ==
6
+ ODUyOWE1NGMwZTNiZmJkZWU3ODI5MzU2NDUwYWZkZDMzYTZjZDAwMg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NjUxM2JkYjg5OTMxODM3ZGVmYjBhYjgwOTc3ZTFlYjY4N2FiNTQyMDc3ZDUz
10
- M2FmNWQxMTMzYjk4NjlhYzIxYTcwYTQ3MTM2ODA0NDEwNzY2YjI2YWJiM2Ey
11
- MzQzMTI5ZTE2MjZhMTE0Mjg0ZTNiZWY3OTdiNWYzYzk4ODA4MDA=
9
+ ZmQ2YjQwMmIyODQwOTA1OWM3YTQwYzhhNTVjOWM0OTBkOGE1YTZhNmViZWU3
10
+ ZGU0ZDZkOGYwMTRmYTJiMTQ3ZGJlYmI1Y2RkOTQyYTQxNzA4ZDBiZTY2YTQy
11
+ MDhiZjIzNDNkNjM3ZWNlYWUyYzA4ZmIwNTU4YTljZjVmZTlkODc=
12
12
  data.tar.gz: !binary |-
13
- NWRkODVlNDYzY2I4NzA4N2Q1ZDM5MTNhYzc1OTU1YWE5MjQ5NWMxZGUwOTZk
14
- YzA0MmJhNjdmNjg1ZjBhNTIxN2QwNzA1M2Q4ZWZkMmQ3ZDJlYjFmYmYwZjA4
15
- M2VmZDlkZjY2ZWMyMTBhYjdlYzI0NTU2NWE5MDg5MTE4OTIwYmY=
13
+ MzY5MTQ4ZjNhOWExMTMyNGI3MTE5NzIzYTAxY2EzMTIxYjc1NjdkYzgwYjFm
14
+ NGU2MjA5Mzc0OTFmMDIyNTYyMmRkYWFhOGFmMDJiZmUwYmQ0MmM3ZTNlNDAz
15
+ MDU4MTExMzg1Yjg3NTUyZjhkNzUzOTZhNjg0MjI1ZmVhY2VkOWM=
data/lib/ro.rb CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  #
12
12
  module Ro
13
- Version = '1.2.0' unless defined?(Version)
13
+ Version = '1.3.1' unless defined?(Version)
14
14
 
15
15
  def version
16
16
  Ro::Version
@@ -311,6 +311,7 @@
311
311
  root.rb
312
312
  lock.rb
313
313
  git.rb
314
+ pagination.rb
314
315
 
315
316
  cache.rb
316
317
 
data/lib/ro/model.rb CHANGED
@@ -58,23 +58,55 @@ module Ro
58
58
  all.last
59
59
  end
60
60
 
61
- def Model.find(*args, &block)
62
- models_for(nodes.find(*args, &block))
61
+ def Model.find(id)
62
+ re = %r/#{ id.to_s.gsub(/[-_]/, '[-_]') }/i
63
+ all.detect{|model| model.id.to_s == id.to_s}
63
64
  end
64
65
 
65
- def Model.paginate(*args, &block)
66
- models_for(nodes.paginate(*args, &block))
66
+ def Model.[](id)
67
+ find(id)
68
+ end
69
+
70
+ def Model.method_missing(method, *args, &block)
71
+ id = method
72
+ model = find(id)
73
+ return model if model
74
+ super
67
75
  end
68
76
 
69
77
  def Model.models_for(result)
70
78
  case result
71
79
  when Array
72
- Array(result).flatten.compact.map{|element| new(element)}
80
+ List.for(Array(result).flatten.compact.map{|element| new(element)})
73
81
  else
74
82
  new(result)
75
83
  end
76
84
  end
77
85
 
86
+ def Model.paginate(*args, &block)
87
+ all.paginate(*args, &block)
88
+ end
89
+
90
+ class List < ::Array
91
+ include Pagination
92
+
93
+ def List.for(*args, &block)
94
+ new(*args, &block)
95
+ end
96
+
97
+ def select(*args, &block)
98
+ List.for super
99
+ end
100
+
101
+ def detect(*args, &block)
102
+ super
103
+ end
104
+ end
105
+
106
+ def List(*args, &block)
107
+ List.new(*args, &block)
108
+ end
109
+
78
110
  #
79
111
  attr_accessor(:node)
80
112
 
@@ -144,7 +176,7 @@ if __FILE__ == $0
144
176
  p ara.id
145
177
  p ara.first_name
146
178
 
147
- require 'pry'
148
- binding.pry
179
+ require 'pry'
180
+ binding.pry
149
181
 
150
182
  end
data/lib/ro/node.rb CHANGED
@@ -2,14 +2,14 @@ module Ro
2
2
  class Node
3
3
  fattr :root
4
4
  fattr :path
5
- fattr :name
6
5
  fattr :type
7
6
  fattr :loaded
8
7
  fattr :fields
9
8
 
10
9
  def initialize(path)
11
10
  @path = Ro.realpath(path.to_s)
12
- @name = File.basename(@path)
11
+ @id = File.basename(@path)
12
+ @slug = Slug.for(@id)
13
13
  @type = File.basename(File.dirname(@path))
14
14
  @root = Ro::Root.new(File.dirname(File.dirname(@path)))
15
15
  @loaded = false
@@ -19,15 +19,47 @@ module Ro
19
19
  end
20
20
 
21
21
  def id
22
- @name
22
+ @id
23
+ end
24
+
25
+ def _id
26
+ @id
27
+ end
28
+
29
+ def type
30
+ attributes[:type] || @type
31
+ end
32
+
33
+ def _type
34
+ @type
35
+ end
36
+
37
+ def path
38
+ attributes[:path] || @path
39
+ end
40
+
41
+ def _path
42
+ @path
23
43
  end
24
44
 
25
45
  def slug
26
- attributes[:slug] || id
46
+ attributes[:slug] || @slug
47
+ end
48
+
49
+ def _slug
50
+ @slug
27
51
  end
28
52
 
29
53
  def identifier
30
- "#{ type }/#{ name }"
54
+ "#{ _type }/#{ _id }"
55
+ end
56
+
57
+ def hash
58
+ identifier.hash
59
+ end
60
+
61
+ def ==(other)
62
+ attributes == other.attributes
31
63
  end
32
64
 
33
65
  def inspect
@@ -67,13 +99,43 @@ module Ro
67
99
  end
68
100
 
69
101
  def assets
70
- asset_paths.map do |asset|
71
- asset.sub(asset_dir + "/", "")
102
+ asset_paths.map do |path|
103
+ name = path.sub(asset_dir + "/", "")
104
+ url = url_for(name)
105
+ Asset.new(name, :path => path, :url => url)
72
106
  end
73
107
  end
74
108
 
75
109
  def asset_urls
76
- asset_names.map{|asset_name| url_for(asset_name)}
110
+ assets.map(&:url)
111
+ end
112
+
113
+ class Asset < ::String
114
+ fattr(:path)
115
+ fattr(:url)
116
+
117
+ def initialize(name, options = {})
118
+ super(name)
119
+ ensure
120
+ options = Map.for(options)
121
+
122
+ Asset.fattrs.each do |attr|
123
+ if options.has_key?(attr)
124
+ value = options[attr]
125
+ send(attr, value)
126
+ end
127
+ end
128
+ end
129
+
130
+ def name
131
+ self
132
+ end
133
+
134
+ IMAGE_RE = %r/[.](jpg|jpeg|png|gif|tif|tiff)$/i
135
+
136
+ def image?
137
+ !!(self =~ IMAGE_RE)
138
+ end
77
139
  end
78
140
 
79
141
  def url_for(*args, &block)
@@ -171,6 +233,10 @@ module Ro
171
233
  def attributes
172
234
  _load{ @attributes }
173
235
  end
236
+
237
+ def attributes=(attributes)
238
+ @attributes = attributes
239
+ end
174
240
 
175
241
  def instance_eval(*args, &block)
176
242
  _load{ super }
@@ -224,6 +290,10 @@ module Ro
224
290
  attributes
225
291
  end
226
292
 
293
+ def load!(&block)
294
+ _load(&block)
295
+ end
296
+
227
297
  def _load(&block)
228
298
  unless @loaded
229
299
  if @loading
data/lib/ro/node/list.rb CHANGED
@@ -57,12 +57,12 @@ module Ro
57
57
  when String, Symbol
58
58
  if @type.nil?
59
59
  type = key.to_s
60
- list = select{|node| type == node.type}
60
+ list = select{|node| type == node._type}
61
61
  list.type = type
62
62
  list
63
63
  else
64
- name = Slug.for(key.to_s)
65
- detect{|node| name == node.name}
64
+ id = Slug.for(key.to_s)
65
+ detect{|node| id == node.id}
66
66
  end
67
67
  else
68
68
  super(*args, &block)
@@ -82,9 +82,9 @@ module Ro
82
82
  select{|node| node.instance_eval(&block)}
83
83
 
84
84
  when !args.empty?
85
- names = args.flatten.compact.uniq.map{|arg| arg.to_s}
86
- index = names.inject(Hash.new){|h,name| h.update(name => name)}
87
- select{|node| index[node.name]}
85
+ ids = args.flatten.compact.uniq.map{|arg| Slug.for(arg.to_s)}
86
+ index = ids.inject(Hash.new){|h,id| h.update(id => id)}
87
+ select{|node| index[node.id]}
88
88
 
89
89
  else
90
90
  raise ArgumentError.new
@@ -99,8 +99,8 @@ module Ro
99
99
  detect{|node| node.instance_eval(&block)}
100
100
 
101
101
  when args.size == 1
102
- name = args.first.to_s
103
- detect{|node| node.name == name}
102
+ id = args.first.to_s
103
+ detect{|node| node.id == id}
104
104
 
105
105
  when args.size > 1
106
106
  where(*args, &block)
@@ -114,71 +114,7 @@ module Ro
114
114
  [root, type].compact.join('/')
115
115
  end
116
116
 
117
- def paginate(*args)
118
- options = Map.options_for!(args)
119
-
120
- ensure_pagination_state!
121
-
122
- page = Integer(args.shift || options[:page] || @page)
123
- per = Integer(args.shift || options[:per] || options[:size] || @per)
124
-
125
- @page = [page.abs, 1].max
126
- @per = [per.abs, 1].max
127
-
128
- offset = (@page - 1) * @per
129
- length = @per
130
-
131
- replace(self.slice(offset, length))
132
-
133
- self
134
- end
135
-
136
- def page(*args)
137
- ensure_pagination_state!
138
-
139
- if args.empty?
140
- return @page
141
- else
142
- options = Map.options_for!(args)
143
- page = args.shift || options[:page]
144
- options[:page] = page
145
- paginate(options)
146
- end
147
- end
148
-
149
- alias_method(:current_page, :page)
150
-
151
- def per(*args)
152
- ensure_pagination_state!
153
-
154
- if args.empty?
155
- return @per
156
- else
157
- options = Map.options_for!(args)
158
- per = args.shift || options[:per]
159
- options[:per] = per
160
- paginate(options)
161
- end
162
- end
163
-
164
- def num_pages
165
- (size.to_f / per).ceil
166
- end
167
-
168
- def total_pages
169
- num_pages
170
- end
171
-
172
- def ensure_pagination_state!
173
- unless defined?(@page)
174
- @page = 1
175
- end
176
- unless defined?(@per)
177
- @per = size
178
- end
179
- [@page, @per]
180
- end
181
-
117
+ include Pagination
182
118
 
183
119
  def method_missing(method, *args, &block)
184
120
  Ro.log "Ro::List(#{ identifier })#method_missing(#{ method.inspect }, #{ args.inspect })"
@@ -0,0 +1,54 @@
1
+ module Ro
2
+ module Pagination
3
+ def paginate(*args)
4
+ options = Map.options_for!(args)
5
+
6
+ page = Integer(args.shift || options[:page] || 1)
7
+ per = Integer(args.shift || options[:per] || options[:size] || 10)
8
+
9
+ page = [page.abs, 1].max
10
+ per = [per.abs, 1].max
11
+
12
+ offset = (page - 1) * per
13
+ length = per
14
+
15
+ slice = dup.slice(offset, length)
16
+ slice.page = page
17
+ slice.per = per
18
+ slice.num_pages = (size.to_f / per).ceil
19
+ slice
20
+ end
21
+
22
+ def page=(page)
23
+ @page = page
24
+ end
25
+
26
+ def page(*args)
27
+ @page
28
+ end
29
+
30
+ def current_page
31
+ @page
32
+ end
33
+
34
+ def per=(per)
35
+ @per = per
36
+ end
37
+
38
+ def per
39
+ @per
40
+ end
41
+
42
+ def num_pages=(num_pages)
43
+ @num_pages = num_pages
44
+ end
45
+
46
+ def num_pages
47
+ @num_pages
48
+ end
49
+
50
+ def total_pages
51
+ num_pages
52
+ end
53
+ end
54
+ end
data/ro.gemspec CHANGED
@@ -3,7 +3,7 @@
3
3
 
4
4
  Gem::Specification::new do |spec|
5
5
  spec.name = "ro"
6
- spec.version = "1.2.0"
6
+ spec.version = "1.3.1"
7
7
  spec.platform = Gem::Platform::RUBY
8
8
  spec.summary = "ro"
9
9
  spec.description = "description: ro kicks the ass"
@@ -24,6 +24,7 @@ Gem::Specification::new do |spec|
24
24
  "lib/ro/model.rb",
25
25
  "lib/ro/node.rb",
26
26
  "lib/ro/node/list.rb",
27
+ "lib/ro/pagination.rb",
27
28
  "lib/ro/root.rb",
28
29
  "lib/ro/slug.rb",
29
30
  "lib/ro/template.rb",
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ro
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ara T. Howard
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-31 00:00:00.000000000 Z
11
+ date: 2014-02-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: map
@@ -157,6 +157,7 @@ files:
157
157
  - lib/ro/model.rb
158
158
  - lib/ro/node.rb
159
159
  - lib/ro/node/list.rb
160
+ - lib/ro/pagination.rb
160
161
  - lib/ro/root.rb
161
162
  - lib/ro/slug.rb
162
163
  - lib/ro/template.rb