maiha-dm-ys 0.1

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/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [maiha@wota.jp]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,23 @@
1
+ dm-ys
2
+ =====
3
+ a DataMapper extension that uses html table as its schema and data powerfully like YunkerStar
4
+
5
+
6
+ Example
7
+ =======
8
+
9
+ class Monster
10
+ include DataMapper::YunkerStar
11
+
12
+ uri "http://ds.gkwiki2.com/47.html"
13
+ thead "table.style_table thead tr"
14
+ tbody "table.style_table tbody tr"
15
+ end
16
+
17
+ irb(main):001:0> Monster.count
18
+ => 120
19
+ irb(main):002:0> Monster.first
20
+ => #<Monster id=nil LV="2" 種族="妖精" 名称="ピクシー" HP="30" MP="27" ...
21
+
22
+
23
+ Copyright (c) 2008 maiha@wota.jp, released under the MIT license
@@ -0,0 +1,77 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the dm-ys plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.pattern = 'test/**/*_test.rb'
12
+ t.verbose = true
13
+ end
14
+
15
+ desc 'Generate documentation for the dm-ys plugin.'
16
+ Rake::RDocTask.new(:rdoc) do |rdoc|
17
+ rdoc.rdoc_dir = 'rdoc'
18
+ rdoc.title = 'DslAccessor'
19
+ rdoc.options << '--line-numbers' << '--inline-source'
20
+ rdoc.rdoc_files.include('README')
21
+ rdoc.rdoc_files.include('lib/**/*.rb')
22
+ end
23
+
24
+
25
+ ######################################################################
26
+ ### for gem
27
+
28
+ require 'rubygems'
29
+ require 'rake/gempackagetask'
30
+
31
+ GEM_NAME = "dm-ys"
32
+ AUTHOR = "maiha"
33
+ EMAIL = "maiha@wota.jp"
34
+ HOMEPAGE = "http://github.com/maiha/dm-ys"
35
+ SUMMARY = "a DataMapper extension that uses html table as its schema and data powerfully like YunkerStar"
36
+ GEM_VERSION = "0.1"
37
+
38
+ spec = Gem::Specification.new do |s|
39
+ # s.rubyforge_project = 'merb'
40
+ s.name = GEM_NAME
41
+ s.version = GEM_VERSION
42
+ s.platform = Gem::Platform::RUBY
43
+ s.has_rdoc = true
44
+ s.extra_rdoc_files = ["README", "LICENSE"]
45
+ s.summary = SUMMARY
46
+ s.description = s.summary
47
+ s.author = AUTHOR
48
+ s.email = EMAIL
49
+ s.homepage = HOMEPAGE
50
+ s.require_path = 'lib'
51
+ s.files = %w(LICENSE README Rakefile) + Dir.glob("{core_ext,lib,spec,tasks,test}/**/*")
52
+ s.add_dependency('dm-core')
53
+ s.add_dependency('hpricot')
54
+ s.add_dependency('maiha-dsl_accessor',">= 0.3.2")
55
+ end
56
+
57
+ Rake::GemPackageTask.new(spec) do |pkg|
58
+ pkg.gem_spec = spec
59
+ end
60
+
61
+ desc "Install the gem"
62
+ task :install do
63
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
64
+ end
65
+
66
+ desc "Uninstall the gem"
67
+ task :uninstall do
68
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
69
+ end
70
+
71
+ desc "Create a gemspec file"
72
+ task :gemspec do
73
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
74
+ file.puts spec.to_ruby
75
+ end
76
+ end
77
+
@@ -0,0 +1,11 @@
1
+ require 'set'
2
+ require 'rubygems'
3
+ require 'dm-core'
4
+ require 'dsl_accessor'
5
+
6
+ __DIR__ = File.dirname(__FILE__)
7
+ require __DIR__ + '/dm-ys/base'
8
+ require __DIR__ + '/dm-ys/cached_accessor'
9
+ require __DIR__ + '/dm-ys/memory_repository'
10
+ require __DIR__ + '/dm-ys/proxy'
11
+ require __DIR__ + '/dm-ys/scraper'
@@ -0,0 +1,29 @@
1
+ module DataMapper
2
+ module YunkerStar
3
+ # @api public
4
+ def self.append_inclusions(*inclusions)
5
+ extra_inclusions.concat inclusions
6
+ true
7
+ end
8
+
9
+ def self.extra_inclusions
10
+ @extra_inclusions ||= []
11
+ end
12
+
13
+ # @api private
14
+ def self.included(model)
15
+ name = 'YunkelStar'
16
+ model.send :include, DataMapper::Resource
17
+ model.send :include, Proxy
18
+ # model.send :include, MemoryRepository
19
+ model.const_set(name, self) unless model.const_defined?(name)
20
+ extra_inclusions.each { |inclusion| model.send(:include, inclusion) }
21
+ descendants << model
22
+ end
23
+
24
+ # @api semipublic
25
+ def self.descendants
26
+ @descendants ||= Set.new
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ module DataMapper
2
+ module YunkerStar
3
+ ######################################################################
4
+ ### CachedAccessor
5
+
6
+ module CachedAccessor
7
+ def self.included(klass)
8
+ def klass.cached_accessor(&block)
9
+ Thread.current[:cached_accessor] = true
10
+ Define.new(self, &block)
11
+ ensure
12
+ Thread.current[:cached_accessor] = false
13
+ end
14
+ super
15
+ end
16
+
17
+ class Define
18
+ def initialize(klass, &block)
19
+ @klass = klass
20
+ instance_eval(&block)
21
+ end
22
+
23
+ def method_missing(symbol, &block)
24
+ @klass.send(:define_method, symbol, &block)
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,21 @@
1
+ module DataMapper
2
+ module YunkerStar
3
+ ######################################################################
4
+ ### MemoryRepository
5
+
6
+ module MemoryRepository
7
+ def self.included(model)
8
+ DataMapper.setup(:memory, "sqlite3::memory:")
9
+ model.send :include, self
10
+ model.send :extend, self
11
+ super
12
+ end
13
+
14
+ # force repository
15
+ def repository(name = nil)
16
+ DataMapper.repository(name || :memory)
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,75 @@
1
+ module DataMapper
2
+ module YunkerStar
3
+
4
+ # ==== Example
5
+ #
6
+ # Class Foo
7
+ # include DataMapper::YunkerStar
8
+ #
9
+ # uri "http://ds.gkwiki2.com/47.html"
10
+ # thead "table.style_table thead tr"
11
+ # tbody "table.style_table tbody tr"
12
+
13
+ module Proxy
14
+ def self.included(model)
15
+ model.class_eval do
16
+ extend ClassMethods
17
+ dsl_accessor :uri
18
+ dsl_accessor :tbody
19
+ dsl_accessor :thead
20
+ property :id, DataMapper::Types::Serial
21
+ end
22
+ end
23
+
24
+ module ClassMethods
25
+ def proxy
26
+ @proxy ||= lazy_load
27
+ end
28
+
29
+ def lazy_load
30
+ loader = Scraper.new(self)
31
+ loader.labels.each do |name|
32
+ type = String # TODO
33
+ property name.intern, type
34
+ end
35
+ return loader
36
+ end
37
+
38
+ def labels
39
+ proxy.labels
40
+ end
41
+
42
+ def entries
43
+ proxy.entries
44
+ end
45
+
46
+ def count
47
+ entries.size
48
+ end
49
+
50
+ def all
51
+ @all ||= proxy.entries.map{|array|
52
+ new(Hash[*proxy.labels.zip(array).flatten])
53
+ }
54
+ end
55
+
56
+ def first
57
+ all.first
58
+ end
59
+
60
+ def last
61
+ all.last
62
+ end
63
+
64
+ def get(*ids)
65
+ if ids.size == 1 and !ids.first.is_a?(Array)
66
+ all[ids.to_i]
67
+ else
68
+ ids.map{|id| get(id)}
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ end
75
+ end
@@ -0,0 +1,27 @@
1
+ module DataMapper
2
+ module YunkerStar
3
+
4
+ ######################################################################
5
+ ### Scrape html
6
+
7
+ require 'nkf'
8
+ require 'open-uri'
9
+ require 'hpricot'
10
+
11
+ class Scraper
12
+ include CachedAccessor
13
+
14
+ def initialize(model)
15
+ @model = model
16
+ @html = NKF.nkf('-w', open(@model.uri).read)
17
+ end
18
+
19
+ cached_accessor do
20
+ doc {Hpricot(@html)}
21
+ labels {doc.search(@model.thead).first.children.map(&:inner_html)}
22
+ entries {doc.search(@model.tbody).map{|tr| tr.children.map(&:inner_html)}}
23
+ end
24
+ end
25
+
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: maiha-dm-ys
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.1"
5
+ platform: ruby
6
+ authors:
7
+ - maiha
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-13 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dm-core
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ - !ruby/object:Gem::Dependency
25
+ name: hpricot
26
+ version_requirement:
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: "0"
32
+ version:
33
+ - !ruby/object:Gem::Dependency
34
+ name: maiha-dsl_accessor
35
+ version_requirement:
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.2
41
+ version:
42
+ description: a DataMapper extension that uses html table as its schema and data powerfully like YunkerStar
43
+ email: maiha@wota.jp
44
+ executables: []
45
+
46
+ extensions: []
47
+
48
+ extra_rdoc_files:
49
+ - README
50
+ - LICENSE
51
+ files:
52
+ - LICENSE
53
+ - README
54
+ - Rakefile
55
+ - lib/dm-ys.rb
56
+ - lib/dm-ys
57
+ - lib/dm-ys/base.rb
58
+ - lib/dm-ys/memory_repository.rb
59
+ - lib/dm-ys/scraper.rb
60
+ - lib/dm-ys/cached_accessor.rb
61
+ - lib/dm-ys/proxy.rb
62
+ has_rdoc: true
63
+ homepage: http://github.com/maiha/dm-ys
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ version:
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ version:
81
+ requirements: []
82
+
83
+ rubyforge_project:
84
+ rubygems_version: 1.2.0
85
+ signing_key:
86
+ specification_version: 2
87
+ summary: a DataMapper extension that uses html table as its schema and data powerfully like YunkerStar
88
+ test_files: []
89
+