maiha-dm-ys 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -33,7 +33,7 @@ AUTHOR = "maiha"
33
33
  EMAIL = "maiha@wota.jp"
34
34
  HOMEPAGE = "http://github.com/maiha/dm-ys"
35
35
  SUMMARY = "a DataMapper extension that uses html table as its schema and data powerfully like YunkerStar"
36
- GEM_VERSION = "0.2.1"
36
+ GEM_VERSION = "0.2.2"
37
37
 
38
38
  spec = Gem::Specification.new do |s|
39
39
  # s.rubyforge_project = 'merb'
data/lib/dm-ys.rb CHANGED
@@ -8,4 +8,5 @@ require __DIR__ + '/dm-ys/base'
8
8
  require __DIR__ + '/dm-ys/cached_accessor'
9
9
  require __DIR__ + '/dm-ys/memory_repository'
10
10
  require __DIR__ + '/dm-ys/proxy'
11
+ require __DIR__ + '/dm-ys/indexed_property'
11
12
  require __DIR__ + '/dm-ys/scraper'
data/lib/dm-ys/base.rb CHANGED
@@ -15,6 +15,7 @@ module DataMapper
15
15
  name = 'YunkelStar'
16
16
  model.send :include, DataMapper::Resource
17
17
  model.send :include, Proxy
18
+ model.send :include, IndexedProperty
18
19
  # model.send :include, MemoryRepository
19
20
  model.const_set(name, self) unless model.const_defined?(name)
20
21
  extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
@@ -0,0 +1,31 @@
1
+ module DataMapper
2
+ module YunkerStar
3
+
4
+ # ==== Example
5
+ #
6
+ # Class Foo
7
+ # include DataMapper::YunkerStar
8
+ # property :id, Serial
9
+ #
10
+ # foo = Foo.new
11
+ # foo[0] == foo[:id] == foo["id"] == foo.attributes[:id]
12
+
13
+ class InvalidIndex < RuntimeError; end
14
+
15
+ module IndexedProperty
16
+ def [](key)
17
+ case key
18
+ when Integer
19
+ self[properties.map(&:name)[key]]
20
+ when String
21
+ attributes[key.intern]
22
+ when Symbol
23
+ attributes[key]
24
+ else
25
+ raise InvalidIndex, "expected Integer/String/Symbol, but got #{key.class}"
26
+ end
27
+ end
28
+ end
29
+
30
+ end
31
+ end
data/lib/dm-ys/proxy.rb CHANGED
@@ -53,8 +53,10 @@ module DataMapper
53
53
  end
54
54
 
55
55
  def all
56
+ count = 0
56
57
  @all ||= proxy.entries.map{|array|
57
- new(Hash[*proxy.names.zip(array).flatten])
58
+ count += 1
59
+ new(Hash[*proxy.names.zip(array).flatten].merge(:id=>count))
58
60
  }
59
61
  end
60
62
 
@@ -0,0 +1,37 @@
1
+ require File.join( File.dirname(__FILE__), "spec_helper" )
2
+
3
+ describe DataMapper::YunkerStar do
4
+ class ::Cute1
5
+ include DataMapper::YunkerStar
6
+ uri spec_data_path("cute1.html")
7
+ end
8
+
9
+ before(:each) do
10
+ @cute = Cute1.first
11
+ end
12
+
13
+ it "should provide #[]" do
14
+ @cute.should respond_to(:[])
15
+ end
16
+
17
+ describe "#[]" do
18
+ it "should accept symbol as a key" do
19
+ @cute[:id].should == 1
20
+ end
21
+
22
+ it "should accept string as a key" do
23
+ @cute["id"].should == 1
24
+ end
25
+
26
+ it "should accept integer as a key" do
27
+ @cute[0].should == 1
28
+ end
29
+
30
+ it "should raise InvalidIndex for unknown key" do
31
+ lambda {
32
+ @cute[Object]
33
+ }.should raise_error(DataMapper::YunkerStar::InvalidIndex)
34
+ end
35
+ end
36
+
37
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maiha-dm-ys
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - maiha
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-01 00:00:00 -08:00
12
+ date: 2009-03-02 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -58,6 +58,7 @@ files:
58
58
  - lib/dm-ys.rb
59
59
  - lib/dm-ys
60
60
  - lib/dm-ys/base.rb
61
+ - lib/dm-ys/indexed_property.rb
61
62
  - lib/dm-ys/memory_repository.rb
62
63
  - lib/dm-ys/scraper.rb
63
64
  - lib/dm-ys/cached_accessor.rb
@@ -67,7 +68,7 @@ files:
67
68
  - spec/data/cute1.html
68
69
  - spec/data/ki.html
69
70
  - spec/data/blank.html
70
- - spec/attribute_spec.rb
71
+ - spec/indexed_property_spec.rb
71
72
  - spec/spec_helper.rb
72
73
  - spec/pagination_spec.rb
73
74
  has_rdoc: true