activeobject 0.0.3 → 0.0.4

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.
data/CHANGE CHANGED
@@ -8,3 +8,7 @@ v0.0.2 2009-9-24
8
8
 
9
9
  v0.0.3 2009-9-25
10
10
  - 修复对象to_json时没有包含对象id
11
+
12
+ v0.0.4
13
+ - 修复当修改Model时,实例化object报错的Bug.
14
+ - 对象新增update_attributes属性.
@@ -30,7 +30,7 @@ Gem::Specification::Class.send :include,DirExt
30
30
 
31
31
  Gem::Specification.new do |s|
32
32
  s.name = %{activeobject}
33
- s.version = '0.0.3'
33
+ s.version = '0.0.4'
34
34
  s.description = 'Active Object是用来访问LightCloud/TokyoCabinet/TokyoTyrant的工具,实现了持久化数据与对象的映射。 它类似于ActiveRecord,提供一组访问LightCloud/TokyoCabinet/TokyoTyrant的方法以及验证规则、回调函数和观察器。'
35
35
  s.homepage = "http://www.tokyocabinet.com"
36
36
  s.rubyforge_project = %q{activeobject}
@@ -288,7 +288,7 @@ module ActiveObject
288
288
 
289
289
  end
290
290
 
291
- def initialize(attributes = nil)
291
+ def initialize(attributes = {})
292
292
  self.id = UUID.new.generate
293
293
  @new_record = true
294
294
  assign_attributes(attributes)
@@ -327,6 +327,14 @@ module ActiveObject
327
327
  @destroyed = true
328
328
  end
329
329
 
330
+ # 更新对象属性
331
+ # <tt>options</tt>是需要更新的一组Hash
332
+ def update_attributes(options={})
333
+ options.each do |key,value|
334
+ self.send("#{key}=",value) if self.respond_to?("#{key}=")
335
+ end
336
+ end
337
+
330
338
  def destroy
331
339
  delete
332
340
  end
@@ -377,10 +385,11 @@ module ActiveObject
377
385
  !value.blank?
378
386
  end
379
387
 
380
- def assign_attributes(attributes=nil)
381
- attributes.each do |key,value|
382
- self.send "#{key}=",value
383
- end unless attributes.nil?
388
+ def assign_attributes(attrs={})
389
+ return if attrs.nil? || attrs.size==0
390
+ attrs.each do |key,value|
391
+ self.send("#{key}=",value) if self.respond_to?("#{key}=")
392
+ end unless attrs.empty?
384
393
  end
385
394
 
386
395
  def create_or_update
@@ -0,0 +1,33 @@
1
+ begin
2
+ require 'base64'
3
+ rescue LoadError
4
+ end
5
+
6
+ module ActiveSupport
7
+ if defined? ::Base64
8
+ Base64 = ::Base64
9
+ else
10
+ # Base64 provides utility methods for encoding and de-coding binary data
11
+ # using a base 64 representation. A base 64 representation of binary data
12
+ # consists entirely of printable US-ASCII characters. The Base64 module
13
+ # is included in Ruby 1.8, but has been removed in Ruby 1.9.
14
+ module Base64
15
+ # Encodes a string to its base 64 representation. Each 60 characters of
16
+ # output is separated by a newline character.
17
+ #
18
+ # ActiveSupport::Base64.encode64("Original unencoded string")
19
+ # # => "T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==\n"
20
+ def self.encode64(data)
21
+ [data].pack("m")
22
+ end
23
+
24
+ # Decodes a base 64 encoded string to its original representation.
25
+ #
26
+ # ActiveSupport::Base64.decode64("T3JpZ2luYWwgdW5lbmNvZGVkIHN0cmluZw==")
27
+ # # => "Original unencoded string"
28
+ def self.decode64(data)
29
+ data.unpack("m").first
30
+ end
31
+ end
32
+ end
33
+ end
@@ -4,73 +4,6 @@ module ActiveSupport #:nodoc:
4
4
  module CoreExtensions #:nodoc:
5
5
  module Array #:nodoc:
6
6
  module Conversions
7
- # Converts the array to a comma-separated sentence where the last element is joined by the connector word. Options:
8
- # * <tt>:connector</tt> - The word used to join the last element in arrays with two or more elements (default: "and")
9
- # * <tt>:skip_last_comma</tt> - Set to true to return "a, b and c" instead of "a, b, and c".
10
- def to_sentence(options = {})
11
- options.assert_valid_keys(:connector, :skip_last_comma, :locale)
12
-
13
- default = I18n.translate(:'support.array.sentence_connector', :locale => options[:locale])
14
- default_skip_last_comma = I18n.translate(:'support.array.skip_last_comma', :locale => options[:locale])
15
- options.reverse_merge! :connector => default, :skip_last_comma => default_skip_last_comma
16
- options[:connector] = "#{options[:connector]} " unless options[:connector].nil? || options[:connector].strip == ''
17
-
18
- case length
19
- when 0
20
- ""
21
- when 1
22
- self[0].to_s
23
- when 2
24
- "#{self[0]} #{options[:connector]}#{self[1]}"
25
- else
26
- "#{self[0...-1].join(', ')}#{options[:skip_last_comma] ? '' : ','} #{options[:connector]}#{self[-1]}"
27
- end
28
- end
29
-
30
-
31
- # Calls <tt>to_param</tt> on all its elements and joins the result with
32
- # slashes. This is used by <tt>url_for</tt> in Action Pack.
33
- def to_param
34
- collect { |e| e.to_param }.join '/'
35
- end
36
-
37
- # Converts an array into a string suitable for use as a URL query string,
38
- # using the given +key+ as the param name.
39
- #
40
- # ['Rails', 'coding'].to_query('hobbies') # => "hobbies%5B%5D=Rails&hobbies%5B%5D=coding"
41
- def to_query(key)
42
- prefix = "#{key}[]"
43
- collect { |value| value.to_query(prefix) }.join '&'
44
- end
45
-
46
- def self.included(base) #:nodoc:
47
- base.class_eval do
48
- alias_method :to_default_s, :to_s
49
- alias_method :to_s, :to_formatted_s
50
- end
51
- end
52
-
53
- # Converts a collection of elements into a formatted string by calling
54
- # <tt>to_s</tt> on all elements and joining them:
55
- #
56
- # Blog.find(:all).to_formatted_s # => "First PostSecond PostThird Post"
57
- #
58
- # Adding in the <tt>:db</tt> argument as the format yields a prettier
59
- # output:
60
- #
61
- # Blog.find(:all).to_formatted_s(:db) # => "First Post,Second Post,Third Post"
62
- def to_formatted_s(format = :default)
63
- case format
64
- when :db
65
- if respond_to?(:empty?) && self.empty?
66
- "null"
67
- else
68
- collect { |element| element.id }.join(",")
69
- end
70
- else
71
- to_default_s
72
- end
73
- end
74
7
 
75
8
  # Returns a string that represents this array in XML by sending +to_xml+
76
9
  # to each element. Active Record collections delegate their representation
@@ -150,7 +83,7 @@ module ActiveSupport #:nodoc:
150
83
  def to_xml(options = {})
151
84
  raise "Not all elements respond to to_xml" unless all? { |e| e.respond_to? :to_xml }
152
85
 
153
- options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "records"
86
+ options[:root] ||= all? { |e| e.is_a?(first.class) && first.class.to_s != "Hash" } ? first.class.to_s.underscore.pluralize : "objects"
154
87
  options[:children] ||= options[:root].singularize
155
88
  options[:indent] ||= 2
156
89
  options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
@@ -97,20 +97,6 @@ module ActiveSupport #:nodoc:
97
97
  klass.extend(ClassMethods)
98
98
  end
99
99
 
100
- # Converts a hash into a string suitable for use as a URL query string. An optional <tt>namespace</tt> can be
101
- # passed to enclose the param names (see example below).
102
- #
103
- # ==== Example:
104
- # { :name => 'David', :nationality => 'Danish' }.to_query # => "name=David&nationality=Danish"
105
- #
106
- # { :name => 'David', :nationality => 'Danish' }.to_query('user') # => "user%5Bname%5D=David&user%5Bnationality%5D=Danish"
107
- def to_query(namespace = nil)
108
- collect do |key, value|
109
- value.to_query(namespace ? "#{namespace}[#{key}]" : key)
110
- end.sort * '&'
111
- end
112
-
113
- alias_method :to_param, :to_query
114
100
 
115
101
  def to_xml(options = {})
116
102
  options[:indent] ||= 2
@@ -159,7 +145,7 @@ module ActiveSupport #:nodoc:
159
145
  end
160
146
  end
161
147
  end
162
-
148
+
163
149
  yield options[:builder] if block_given?
164
150
  end
165
151
 
@@ -210,7 +196,7 @@ module ActiveSupport #:nodoc:
210
196
  # blank or nil parsed values are represented by nil
211
197
  elsif value.blank? || value['nil'] == 'true'
212
198
  nil
213
- # If the type is the only element which makes it then
199
+ # If the type is the only element which makes it then
214
200
  # this still makes the value nil, except if type is
215
201
  # a XML node(where type['value'] is a Hash)
216
202
  elsif value['type'] && value.size == 1 && !value['type'].is_a?(::Hash)
@@ -220,7 +206,7 @@ module ActiveSupport #:nodoc:
220
206
  h[k] = typecast_xml_value(v)
221
207
  h
222
208
  end
223
-
209
+
224
210
  # Turn { :files => { :file => #<StringIO> } into { :files => #<StringIO> } so it is compatible with
225
211
  # how multipart uploaded files from HTML appear
226
212
  xml_value["file"].is_a?(StringIO) ? xml_value["file"] : xml_value
@@ -63,7 +63,7 @@ module ActiveSupport #:nodoc:
63
63
  alias_method :titlecase, :titleize
64
64
 
65
65
  # The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
66
- #
66
+ #
67
67
  # +underscore+ will also change '::' to '/' to convert namespaces to paths.
68
68
  #
69
69
  # "ActiveRecord".underscore # => "active_record"
@@ -88,7 +88,7 @@ module ActiveSupport #:nodoc:
88
88
  end
89
89
 
90
90
  # Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
91
- #
91
+ #
92
92
  # ==== Examples
93
93
  #
94
94
  # class Person
@@ -96,10 +96,10 @@ module ActiveSupport #:nodoc:
96
96
  # "#{id}-#{name.parameterize}"
97
97
  # end
98
98
  # end
99
- #
99
+ #
100
100
  # @person = Person.find(1)
101
101
  # # => #<Person id: 1, name: "Donald E. Knuth">
102
- #
102
+ #
103
103
  # <%= link_to(@person.name, person_path %>
104
104
  # # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
105
105
  def parameterize
@@ -129,27 +129,16 @@ module ActiveSupport #:nodoc:
129
129
  def classify
130
130
  Inflector.classify(self)
131
131
  end
132
-
132
+
133
133
  # Capitalizes the first word, turns underscores into spaces, and strips '_id'.
134
134
  # Like +titleize+, this is meant for creating pretty output.
135
135
  #
136
- # "employee_salary" # => "Employee salary"
136
+ # "employee_salary" # => "Employee salary"
137
137
  # "author_id" # => "Author"
138
138
  def humanize
139
139
  Inflector.humanize(self)
140
140
  end
141
141
 
142
- # Creates a foreign key name from a class name.
143
- # +separate_class_name_and_id_with_underscore+ sets whether
144
- # the method should put '_' between the name and 'id'.
145
- #
146
- # Examples
147
- # "Message".foreign_key # => "message_id"
148
- # "Message".foreign_key(false) # => "messageid"
149
- # "Admin::Post".foreign_key # => "post_id"
150
- def foreign_key(separate_class_name_and_id_with_underscore = true)
151
- Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
152
- end
153
142
 
154
143
  # +constantize+ tries to find a declared constant with the name specified
155
144
  # in the string. It raises a NameError when the name is not in CamelCase
@@ -71,4 +71,10 @@ class BaseTest < Test::Unit::TestCase
71
71
  @u.destroy
72
72
  end
73
73
  end
74
+
75
+ def test_update_attributes
76
+ @u = User.create(@attributes)
77
+ @u.update_attributes(:name=>'kame')
78
+ assert_equal @u.name,"kame"
79
+ end
74
80
  end
@@ -5,9 +5,6 @@ require 'rubygems'
5
5
 
6
6
  require 'builder'
7
7
 
8
- user = Array(User.new(@attributes))
9
- puts user.to_json
10
-
11
8
  class SerializationTest < Test::Unit::TestCase
12
9
  def setup
13
10
  @attributes = {:name=>'Aaron',:email=>'aaron@nonobo.com',:password=>'123456'}
@@ -163,10 +163,11 @@ class ValidationsTest < Test::Unit::TestCase
163
163
  hits += 1
164
164
  end
165
165
  end
166
- t = Topic.new("title" => "valid", "content" => "whatever")
166
+ t = Topic.new(:title=> "valid", :content=>"whatever")
167
167
  assert !t.save
168
168
  assert_equal 4, hits
169
169
  assert_equal %w(gotcha gotcha), t.errors.on(:title)
170
+
170
171
  assert_equal %w(gotcha gotcha), t.errors.on(:content)
171
172
  ensure
172
173
  perform = false
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeobject
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - yalong zhang
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-25 00:00:00 +08:00
12
+ date: 2009-09-27 00:00:00 +08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -123,6 +123,7 @@ files:
123
123
  - ./lib/active_support/core_ext/class/inheritable_attributes.rb
124
124
  - ./lib/active_support/core_ext/class/removal.rb
125
125
  - ./lib/active_support/callbacks.rb
126
+ - ./lib/active_support/base64.rb
126
127
  - ./MIT-LICENSE
127
128
  - ./spec/helper.rb
128
129
  - ./spec/model/account.rb