rhodes 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,6 @@
1
+ == 0.2.5 2009-01-28
2
+ * erb labels are now humanized
3
+
1
4
  == 0.2.4 2009-01-27
2
5
  * added show.erb to model generator [#164]
3
6
  * base source_adapter should include user_id on sync
data/Manifest.txt CHANGED
@@ -17,6 +17,7 @@ generators/templates/model/show.erb
17
17
  generators/templates/source/source_adapter.rb
18
18
  lib/ServeME.rb
19
19
  lib/TestServe.rb
20
+ lib/bsearch.rb
20
21
  lib/builtinME.rb
21
22
  lib/date.rb
22
23
  lib/date/format.rb
data/Rakefile CHANGED
@@ -18,7 +18,8 @@ $hoe = Hoe.new('rhodes', Rhodes::VERSION) do |p|
18
18
  ['templater'],
19
19
  ['diff-lcs'],
20
20
  ['extlib'],
21
- ['newgem']
21
+ ['newgem'],
22
+ ['activesupport']
22
23
  ]
23
24
 
24
25
  p.clean_globs |= %w[**/.DS_Store tmp *.log]
data/generators/rhogen.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'rubygems'
2
2
  require 'templater'
3
+ require 'activesupport'
3
4
 
4
5
  module Rhogen
5
-
6
6
  extend Templater::Manifold
7
7
 
8
8
  desc <<-DESC
@@ -50,6 +50,7 @@ module Rhogen
50
50
  end
51
51
 
52
52
  class ModelGenerator < BaseGenerator
53
+ include ActiveSupport::Inflector
53
54
 
54
55
  def self.source_root
55
56
  File.join(File.dirname(__FILE__), 'templates', 'model')
@@ -7,7 +7,7 @@
7
7
  <input type="hidden" name="id" value="<%%=@<%= name %>.object%>"/>
8
8
  <% attributes.each do |attribute| %>
9
9
  <div class="row">
10
- <label><%=attribute%>: </label>
10
+ <label><%=humanize(attribute)%>: </label>
11
11
  <input type="text" name="<%= name %>[<%= attribute %>]" value="<%%=@<%=name%>.<%=attribute%>%>"/>
12
12
  </div>
13
13
  <% end %>
@@ -7,7 +7,7 @@
7
7
  <input type="hidden" name="id" value="<%%=@<%= name %>.object%>"/>
8
8
  <% attributes.each do |attribute| %>
9
9
  <div class="row">
10
- <label><%=attribute%>: </label>
10
+ <label><%=humanize(attribute)%>: </label>
11
11
  <input type="text" name="<%= name %>[<%= attribute %>]"/>
12
12
  </div>
13
13
  <% end %>
data/lib/bsearch.rb ADDED
@@ -0,0 +1,120 @@
1
+ #
2
+ # Ruby/Bsearch - a binary search library for Ruby.
3
+ #
4
+ # Copyright (C) 2001 Satoru Takabayashi <satoru@namazu.org>
5
+ # All rights reserved.
6
+ # This is free software with ABSOLUTELY NO WARRANTY.
7
+ #
8
+ # You can redistribute it and/or modify it under the terms of
9
+ # the Ruby's licence.
10
+ #
11
+ # Example:
12
+ #
13
+ # % irb -r ./bsearch.rb
14
+ # >> %w(a b c c c d e f).bsearch_first {|x| x <=> "c"}
15
+ # => 2
16
+ # >> %w(a b c c c d e f).bsearch_last {|x| x <=> "c"}
17
+ # => 4
18
+ # >> %w(a b c e f).bsearch_first {|x| x <=> "c"}
19
+ # => 2
20
+ # >> %w(a b e f).bsearch_first {|x| x <=> "c"}
21
+ # => nil
22
+ # >> %w(a b e f).bsearch_last {|x| x <=> "c"}
23
+ # => nil
24
+ # >> %w(a b e f).bsearch_lower_boundary {|x| x <=> "c"}
25
+ # => 2
26
+ # >> %w(a b e f).bsearch_upper_boundary {|x| x <=> "c"}
27
+ # => 2
28
+ # >> %w(a b c c c d e f).bsearch_range {|x| x <=> "c"}
29
+ # => 2...5
30
+ # >> %w(a b c d e f).bsearch_range {|x| x <=> "c"}
31
+ # => 2...3
32
+ # >> %w(a b d e f).bsearch_range {|x| x <=> "c"}
33
+ # => 2...2
34
+
35
+ module Bsearch
36
+ VERSION = '1.5'
37
+ end
38
+
39
+ class Array
40
+ #
41
+ # The binary search algorithm is extracted from Jon Bentley's
42
+ # Programming Pearls 2nd ed. p.93
43
+ #
44
+
45
+ #
46
+ # Return the lower boundary. (inside)
47
+ #
48
+ def bsearch_lower_boundary (range = 0 ... self.length, &block)
49
+ lower = range.first() -1
50
+ upper = if range.exclude_end? then range.last else range.last + 1 end
51
+ while lower + 1 != upper
52
+ mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational)
53
+ if yield(self[mid]) < 0
54
+ lower = mid
55
+ else
56
+ upper = mid
57
+ end
58
+ end
59
+ return upper
60
+ end
61
+
62
+ #
63
+ # This method searches the FIRST occurrence which satisfies a
64
+ # condition given by a block in binary fashion and return the
65
+ # index of the first occurrence. Return nil if not found.
66
+ #
67
+ def bsearch_first (range = 0 ... self.length, &block)
68
+ boundary = bsearch_lower_boundary(range, &block)
69
+ if boundary >= self.length || yield(self[boundary]) != 0
70
+ return nil
71
+ else
72
+ return boundary
73
+ end
74
+ end
75
+
76
+ alias bsearch bsearch_first
77
+
78
+ #
79
+ # Return the upper boundary. (outside)
80
+ #
81
+ def bsearch_upper_boundary (range = 0 ... self.length, &block)
82
+ lower = range.first() -1
83
+ upper = if range.exclude_end? then range.last else range.last + 1 end
84
+ while lower + 1 != upper
85
+ mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational)
86
+ if yield(self[mid]) <= 0
87
+ lower = mid
88
+ else
89
+ upper = mid
90
+ end
91
+ end
92
+ return lower + 1 # outside of the matching range.
93
+ end
94
+
95
+ #
96
+ # This method searches the LAST occurrence which satisfies a
97
+ # condition given by a block in binary fashion and return the
98
+ # index of the last occurrence. Return nil if not found.
99
+ #
100
+ def bsearch_last (range = 0 ... self.length, &block)
101
+ # `- 1' for canceling `lower + 1' in bsearch_upper_boundary.
102
+ boundary = bsearch_upper_boundary(range, &block) - 1
103
+
104
+ if (boundary <= -1 || yield(self[boundary]) != 0)
105
+ return nil
106
+ else
107
+ return boundary
108
+ end
109
+ end
110
+
111
+ #
112
+ # Return the search result as a Range object.
113
+ #
114
+ def bsearch_range (range = 0 ... self.length, &block)
115
+ lower = bsearch_lower_boundary(range, &block)
116
+ upper = bsearch_upper_boundary(range, &block)
117
+ return lower ... upper
118
+ end
119
+ end
120
+
@@ -1,3 +1,5 @@
1
+ require 'bsearch'
2
+
1
3
  module Rho
2
4
  class RhoContact
3
5
  class << self
@@ -62,6 +64,47 @@ module Rho
62
64
  end
63
65
  end
64
66
 
65
- end
66
- end
67
- end
67
+ # Examples of how to use select method:
68
+ #
69
+ # selected = Rho::RhoContact.select('first_name' => 'David') { |x| x[1]['last_name']=='Taylor' }
70
+ # ==> returns record(s) of the David Taylor
71
+ #
72
+ # selected = Rho::RhoContact.select('first_name' => 'Kate')
73
+ # ==> Returns all records of Kate
74
+ #
75
+ # selected = Rho::RhoContact.select('last_name' => 'User') do |x|
76
+ # x[1]['first_name']=='Test' and x[1]['company_name']=="rhomobile"
77
+ # end
78
+ # ==> returns all records of the Test User from the company rhomobile
79
+ #
80
+ def select(index, &block)
81
+ key, value = index.keys[0], index.values[0]
82
+ if @contacts.nil? or @key != key
83
+ @key, @contacts = key, find(:all).to_a.sort! {|x,y| x[1][key] <=> y[1][key] }
84
+ end
85
+ found = @contacts[@contacts.bsearch_range {|x| x[1][key] <=> value}]
86
+ unless found.nil? or block.nil?
87
+ return found.select(&block)
88
+ end
89
+ return found
90
+ end
91
+
92
+ def select_by_name(first_last_name, &block)
93
+ if @contacts.nil?
94
+ @contacts = find(:all).to_a.sort! do |x,y|
95
+ x[1]['first_name'] + " " + x[1]['last_name'] <=> y[1]['first_name'] + " " + y[1]['last_name']
96
+ end
97
+ end
98
+ range = @contacts.bsearch_range do |x|
99
+ x[1]['first_name'] + " " + x[1]['last_name'] <=> first_last_name
100
+ end
101
+ found = @contacts[range]
102
+ unless found.nil? or block.nil?
103
+ return found.select(&block)
104
+ end
105
+ return found
106
+ end
107
+
108
+ end #<< self
109
+ end # class RhoContact
110
+ end # module Rho
data/lib/rhodes.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Rhodes
2
2
  unless defined? Rhodes::VERSION
3
- VERSION = '0.2.4'
3
+ VERSION = '0.2.5'
4
4
  end
5
5
  unless defined? Rhodes::DBVERSION
6
6
  DBVERSION = '0.2.2'
@@ -28,12 +28,16 @@ module Rhom
28
28
  def method_missing(name, *args)
29
29
  unless name == Fixnum
30
30
  varname = name.to_s.gsub(/=/,"")
31
- if instance_variable_defined? "@#{varname}"
32
- #TODO: Figure out why this returns an array
33
- instance_variable_get( "@#{varname}" )[0]
34
- else
35
- instance_variable_set( "@#{varname}", args )
31
+ setting = (name.to_s =~ /=/)
32
+ inst_var = nil
33
+
34
+ if setting
35
+ inst_var = instance_variable_set( "@#{varname}", args[0] )
36
+ else
37
+ inst_var = instance_variable_get( "@#{varname}" )
36
38
  end
39
+
40
+ inst_var
37
41
  end
38
42
  end
39
43
 
@@ -60,12 +60,12 @@ module Rhom
60
60
  # create a temp id for the create type
61
61
  # TODO: This is duplicative of get_new_obj
62
62
  temp_objid = djb_hash(obj.values.to_s, 10).to_s
63
- self.send 'object'.to_sym, "#{temp_objid}"
64
- self.send 'source_id'.to_sym, obj['source_id'].to_s
65
- self.send 'update_type'.to_sym, 'create'
63
+ self.send :object=, "#{temp_objid}"
64
+ self.send :source_id=, obj['source_id'].to_s
65
+ self.send :update_type=, 'create'
66
66
  obj.each do |key,value|
67
67
  val = self.inst_strip_braces(value)
68
- self.send key, val
68
+ self.send "#{key}=".to_sym, val
69
69
  end
70
70
  end
71
71
 
@@ -106,10 +106,10 @@ module Rhom
106
106
  attrib = obj['attrib']
107
107
  value = obj['value']
108
108
  hash_list[object] = get_new_obj(obj) if not hash_list[object]
109
- if not method_name_reserved?(attrib) and hash_list[object].send attrib.to_sym
109
+ if not method_name_reserved?(attrib) and hash_list[object].send(attrib.to_sym)
110
110
  hash_list[object].remove_var(attrib)
111
111
  end
112
- hash_list[object].send attrib.to_sym, value if not method_name_reserved?(attrib)
112
+ hash_list[object].send("#{attrib}=".to_sym(), value) if not method_name_reserved?(attrib)
113
113
  nil # remove the element from the array
114
114
  end
115
115
  end
@@ -131,7 +131,7 @@ module Rhom
131
131
  # returns new model instance with a temp object id
132
132
  def get_new_obj(obj, type='query')
133
133
  tmp_obj = self.new
134
- tmp_obj.send 'object'.to_sym, "{#{obj['object'].to_s}}"
134
+ tmp_obj.send :object=, "{#{obj['object'].to_s}}"
135
135
  tmp_obj
136
136
  end
137
137
  end #class methods
@@ -7,9 +7,9 @@ module Rhom
7
7
  attr_accessor :source_url
8
8
  attr_reader :source_id, :name, :last_updated, :last_inserted_size,
9
9
  :last_deleted_size, :last_sync_duration,
10
- :last_sync_success
10
+ :last_sync_success, :distinct_objects
11
11
 
12
- def initialize(args)
12
+ def initialize(args,count=0)
13
13
  # setup the name
14
14
  # TODO: should really store this in the database
15
15
  Rho::RhoConfig::sources.each do |key,value|
@@ -24,6 +24,11 @@ module Rhom
24
24
  @last_deleted_size = args['last_deleted_size'].to_i
25
25
  @last_sync_duration = args['last_sync_duration'].to_i
26
26
  @last_sync_success = args['last_sync_success'].to_i == 1 ? true : false
27
+ @distinct_objects = ::Rhom::RhomDbAdapter::select_from_table(
28
+ ::Rhom::TABLE_NAME,
29
+ 'object',
30
+ {"source_id"=>@source_id},
31
+ {"distinct"=>true}).length
27
32
  end
28
33
 
29
34
  class << self
@@ -36,7 +36,7 @@ describe "RhomObjectFactory" do
36
36
  "2".should == Case.get_source_id
37
37
  "3".should == Employee.get_source_id
38
38
  end
39
-
39
+
40
40
  it "should dynamically assign values" do
41
41
  account = Account.new
42
42
  account.name = 'hello name'
@@ -109,7 +109,7 @@ describe "RhomObjectFactory" do
109
109
  @new_acct.name.should == "Mobio US"
110
110
  @new_acct.industry.should == "Technology"
111
111
  end
112
-
112
+
113
113
  it "should fully update a record" do
114
114
  new_attributes = {"name"=>"Mobio US", "industry"=>"Electronics"}
115
115
  @account = Account.find(:all).first
@@ -120,4 +120,21 @@ describe "RhomObjectFactory" do
120
120
  @new_acct.name.should == "Mobio US"
121
121
  @new_acct.industry.should == "Electronics"
122
122
  end
123
+
124
+ it "should retrieve and modify one record" do
125
+ @acct = Account.find('44e804f2-4933-4e20-271c-48fcecd9450d')
126
+
127
+ @acct.name.should == "Mobio US"
128
+ @acct.industry.should == "Technology"
129
+
130
+ @acct.name = "Rhomobile US"
131
+
132
+ @acct.name.should == "Rhomobile US"
133
+ end
134
+
135
+ it "should return an empty value for a non-existent attribute" do
136
+ @acct = Account.find('44e804f2-4933-4e20-271c-48fcecd9450d')
137
+
138
+ @acct.foobar.should be_nil
139
+ end
123
140
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rhodes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rhomobile Dev
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-01-27 00:00:00 -08:00
12
+ date: 2009-01-28 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -82,6 +82,16 @@ dependencies:
82
82
  - !ruby/object:Gem::Version
83
83
  version: "0"
84
84
  version:
85
+ - !ruby/object:Gem::Dependency
86
+ name: activesupport
87
+ type: :runtime
88
+ version_requirement:
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: "0"
94
+ version:
85
95
  - !ruby/object:Gem::Dependency
86
96
  name: hoe
87
97
  type: :development
@@ -124,6 +134,7 @@ files:
124
134
  - generators/templates/source/source_adapter.rb
125
135
  - lib/ServeME.rb
126
136
  - lib/TestServe.rb
137
+ - lib/bsearch.rb
127
138
  - lib/builtinME.rb
128
139
  - lib/date.rb
129
140
  - lib/date/format.rb