tagutils 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest.txt CHANGED
@@ -3,13 +3,21 @@ Manifest.txt
3
3
  README.md
4
4
  Rakefile
5
5
  lib/tagutils.rb
6
- lib/tagutils/active_record.rb
7
- lib/tagutils/models/categorization.rb
8
- lib/tagutils/models/category.rb
9
- lib/tagutils/models/tag.rb
10
- lib/tagutils/models/tagging.rb
11
- lib/tagutils/schema_categories.rb
12
- lib/tagutils/schema_tags.rb
6
+ lib/tagutils/categories.rb
7
+ lib/tagutils/categories/active_record.rb
8
+ lib/tagutils/categories/models/categorization.rb
9
+ lib/tagutils/categories/models/category.rb
10
+ lib/tagutils/categories/models/category_comp.rb
11
+ lib/tagutils/categories/schema.rb
12
+ lib/tagutils/tags.rb
13
+ lib/tagutils/tags/active_record.rb
14
+ lib/tagutils/tags/models/tag.rb
15
+ lib/tagutils/tags/models/tag_comp.rb
16
+ lib/tagutils/tags/models/tagging.rb
17
+ lib/tagutils/tags/readers/tag.rb
18
+ lib/tagutils/tags/schema.rb
13
19
  lib/tagutils/version.rb
14
20
  test/helper.rb
21
+ test/tags.1.yml
15
22
  test/test_models.rb
23
+ test/test_tag_reader.rb
data/README.md CHANGED
@@ -67,6 +67,31 @@ Movie.with_category( 'doc' )
67
67
  # e.g. scope :with_category, ->(category_key){ joins(:categories).where('categories.key' => category_key) }
68
68
  ~~~
69
69
 
70
+ ### Reader
71
+
72
+ The `TagReader` lets you read plain text fixtures (data sets). Example:
73
+
74
+ ~~~
75
+ tags.1.yml:
76
+ -----------
77
+
78
+ # organizations
79
+
80
+ orgs: un, g5, g8, g20, eu, commonwealth, mercosur, nafta
81
+ football: fifa, uefa, afc, ofc, caf, csf, concacaf
82
+
83
+ # national regions
84
+
85
+ brasil: s|South, se|Southeast, co|Centerwest, ne|Northeast, n|North
86
+ ~~~
87
+
88
+ To read the tags use:
89
+
90
+ ~~~
91
+ TagReader.new( <include_path>).read( `tags.1` )
92
+ ~~~
93
+
94
+
70
95
 
71
96
  ## Real World Usage
72
97
 
data/lib/tagutils.rb CHANGED
@@ -2,24 +2,44 @@
2
2
  # 3rd party gems / libs
3
3
 
4
4
  require 'active_record' ## todo: add sqlite3? etc.
5
+
6
+ require 'props'
7
+ require 'props/db' # note: also use ConfDb (ConfDb::Model::Prop etc.)
5
8
  require 'logutils'
9
+ require 'textutils'
10
+
11
+ #########
12
+ # fix/todo:
13
+ # move props/db - Props.create_from_fixture! to textutils/db
14
+ # require 'textutils/db'
15
+
16
+ #######################
17
+ # fix/remove once removed from HashReaderV2
18
+
19
+ module WorldDb
20
+ module Model
21
+ Prop = ConfDb::Model::Prop
22
+ end
23
+ Models = Model
24
+ end
25
+
6
26
 
7
27
 
8
28
  # our own code
9
29
 
10
30
  require 'tagutils/version' # let it always go first
11
31
 
12
- require 'tagutils/schema_tags'
13
- require 'tagutils/models/tag'
14
- require 'tagutils/models/tagging'
15
-
16
- require 'tagutils/schema_categories'
17
- require 'tagutils/models/category'
18
- require 'tagutils/models/categorization'
32
+ require 'tagutils/tags'
33
+ require 'tagutils/categories'
19
34
 
20
35
 
21
- require 'tagutils/active_record' # -- adds has_many_tags, has_many_categories class macros
22
36
 
37
+ ####
38
+ # use shared/common module/namespace ?
39
+ # e.g.
40
+ # - ClassificationDb, ClassiDb ??
41
+ # - TaxonomyDb, TaxonDb, TaxyDb ???
42
+ # - TopicDb, KeywordDb ?? -- why? why not??
23
43
 
24
44
  module TagUtils
25
45
 
@@ -34,67 +54,4 @@ module TagUtils
34
54
  end # module TagUtils
35
55
 
36
56
 
37
-
38
- module TagDb
39
-
40
- #####
41
- # add convenience module alias in plural
42
- # e.g. lets you use include TagDb::Models
43
- Models = Model
44
-
45
- def self.create
46
- CreateDb.new.up
47
- ## WorldDb::Model::Prop.create!( key: 'db.schema.world.version', value: VERSION )
48
- end
49
-
50
- # delete ALL records (use with care!)
51
- def self.delete!
52
- puts '*** deleting tag/tagging table records/data...'
53
- Model::Tagging.delete_all
54
- Model::Tag.delete_all
55
- end
56
-
57
- def self.tables
58
- puts "#{Model::Tag.count} tags"
59
- puts "#{Model::Tagging.count} taggings"
60
- end
61
-
62
- end # module TagDb
63
-
64
-
65
- module CategoryDb
66
- #####
67
- # add convenience module alias in plural
68
- # e.g. lets you use include CategoryDb::Models
69
- Models = Model
70
-
71
- def self.create
72
- CreateDb.new.up
73
- ## WorldDb::Model::Prop.create!( key: 'db.schema.world.version', value: VERSION )
74
- end
75
-
76
- # delete ALL records (use with care!)
77
- def self.delete!
78
- puts '*** deleting category/categorization table records/data...'
79
- Model::Categorization.delete_all
80
- Model::Category.delete_all
81
- end
82
-
83
- def self.tables
84
- puts "#{Model::Category.count} categories"
85
- puts "#{Model::Categorization.count} categorizations"
86
- end
87
- end # module CategoryDb
88
-
89
- CatDb = CategoryDb # for conveniene add alias for CatDb
90
-
91
-
92
- ####
93
- # use shared/common module/namespace ?
94
- # e.g.
95
- # - ClassificationDb, ClassiDb ??
96
- # - TaxonomyDb, TaxonDb, TaxyDb ???
97
- # - TopicDb, KeywordDb ?? -- why? why not??
98
-
99
-
100
57
  puts TagUtils.banner # say hello
@@ -0,0 +1,31 @@
1
+
2
+
3
+ require 'tagutils/categories/schema'
4
+ require 'tagutils/categories/models/category'
5
+ require 'tagutils/categories/models/category_comp'
6
+ require 'tagutils/categories/models/categorization'
7
+
8
+ require 'tagutils/categories/active_record' # -- adds has_many_tags, has_many_categories class macros
9
+
10
+
11
+ module CategoryDb
12
+ VERSION = TagUtils::VERSION
13
+
14
+ def self.create
15
+ CreateDb.new.up
16
+ ConfDb::Model::Prop.create!( key: 'db.schema.category.version', value: VERSION )
17
+ end
18
+
19
+ # delete ALL records (use with care!)
20
+ def self.delete!
21
+ puts '*** deleting category/categorization table records/data...'
22
+ Model::Categorization.delete_all
23
+ Model::Category.delete_all
24
+ end
25
+
26
+ def self.tables
27
+ puts " #{Model::Category.count} categories"
28
+ puts " #{Model::Categorization.count} categorizations"
29
+ end
30
+ end # module CategoryDb
31
+
@@ -0,0 +1,27 @@
1
+
2
+ module CategoryDb
3
+ module ClassMacros
4
+ def has_many_categories( opts={} )
5
+ puts " [CategoryDb.has_many_categories] adding categorizations n category has_many assocs to model >#{name}<"
6
+
7
+ has_many :categorizations, class_name: 'CategoryDb::Model::Categorization', :as => :categorizable
8
+ has_many :categories, class_name: 'CategoryDb::Model::Category', :through => :categorizations
9
+
10
+ ### check: use category_name instead of category_key ???
11
+ scope :with_category, ->(category_key){ joins(:categories).where('categories.key' => category_key) }
12
+ end
13
+
14
+ ## same as scope above
15
+ ## def with_category( category_key )
16
+ ## joins(:categories).where( 'categories.key' => category_key )
17
+ ## end
18
+ end
19
+ end
20
+
21
+
22
+ module ActiveRecord
23
+ class Base
24
+ extend CategoryDb::ClassMacros
25
+ end # class Base
26
+ end # module ActiveRecord
27
+
@@ -23,22 +23,18 @@ class Category < ActiveRecord::Base
23
23
  before_save :on_before_save
24
24
 
25
25
  def on_before_save
26
- # replace space with dash e.g. north america becomes north-america and so on
27
- self.slug = key.gsub( ' ', '-' )
26
+ # replace space with dash e.g. north america becomes north_america and so on
27
+ self.slug = key.gsub( ' ', '_' )
28
28
  end
29
29
 
30
-
31
- ############################################################################
32
- # alias for name (remove! add depreciated api call) ?? why? why not??
33
-
34
- def title() name; end
35
- def title=(value) self.name = value; end
36
-
37
- scope :by_title, -> { order( 'name desc' ) } ### depreciated ??? - use by_name
30
+ end # class Category
38
31
 
39
32
 
40
- end # class Category
33
+ end # module Model
41
34
 
35
+ #####
36
+ # add convenience module alias in plural
37
+ # e.g. lets you use include CategoryDb::Models
38
+ Models = Model
42
39
 
43
- end # module CategoryDb
44
- end # module Model
40
+ end # module CategoryDb
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module CategoryDb
4
+ module Model
5
+
6
+ class Category
7
+
8
+ ##############################################
9
+ # alias for name (depreciated api calls)
10
+
11
+ def title() name; end
12
+ def title=(value) self.name = value; end
13
+
14
+ scope :by_title, -> { order( 'name desc' ) }
15
+
16
+ end # class Category
17
+
18
+
19
+ end # module CategoryDb
20
+ end # module Model
@@ -0,0 +1,34 @@
1
+
2
+
3
+ require 'tagutils/tags/schema'
4
+ require 'tagutils/tags/models/tag'
5
+ require 'tagutils/tags/models/tag_comp'
6
+ require 'tagutils/tags/models/tagging'
7
+
8
+ require 'tagutils/tags/active_record' # -- adds has_many_tags, has_many_categories class macros
9
+ require 'tagutils/tags/readers/tag'
10
+
11
+
12
+
13
+ module TagDb
14
+ VERSION = TagUtils::VERSION
15
+
16
+ def self.create
17
+ CreateDb.new.up
18
+ ConfDb::Model::Prop.create!( key: 'db.schema.tag.version', value: VERSION )
19
+ end
20
+
21
+ # delete ALL records (use with care!)
22
+ def self.delete!
23
+ puts '*** deleting tag/tagging table records/data...'
24
+ Model::Tagging.delete_all
25
+ Model::Tag.delete_all
26
+ end
27
+
28
+ def self.tables
29
+ puts " #{Model::Tag.count} tags"
30
+ puts " #{Model::Tagging.count} taggings"
31
+ end
32
+
33
+ end # module TagDb
34
+
@@ -19,30 +19,11 @@ module TagDb
19
19
  end
20
20
 
21
21
 
22
- module CategoryDb
23
- module ClassMacros
24
- def has_many_categories( opts={} )
25
- puts " [CategoryDb.has_many_categories] adding categorizations n category has_many assocs to model >#{name}<"
26
-
27
- has_many :categorizations, class_name: 'CategoryDb::Model::Categorization', :as => :categorizable
28
- has_many :categories, class_name: 'CategoryDb::Model::Category', :through => :categorizations
29
-
30
- ### check: use category_name instead of category_key ???
31
- scope :with_category, ->(category_key){ joins(:categories).where('categories.key' => category_key) }
32
- end
33
-
34
- ## same as scope above
35
- ## def with_category( category_key )
36
- ## joins(:categories).where( 'categories.key' => category_key )
37
- ## end
38
- end
39
- end
40
22
 
41
23
 
42
24
  module ActiveRecord
43
25
  class Base
44
26
  extend TagDb::ClassMacros
45
- extend CategoryDb::ClassMacros
46
27
  end # class Base
47
28
  end # module ActiveRecord
48
29
 
@@ -20,25 +20,20 @@ class Tag < ActiveRecord::Base
20
20
  before_save :on_before_save
21
21
 
22
22
  def on_before_save
23
- # replace space with dash e.g. north america becomes north-america and so on
24
- self.slug = key.gsub( ' ', '-' )
25
-
23
+ # replace space with dash e.g. north america becomes north_america and so on
24
+ self.slug = key.gsub( ' ', '_' )
25
+
26
26
  ## if name is empty auto fill w/ key
27
27
  self.name = key if name.blank?
28
28
  end
29
29
 
30
-
31
- ############################################################################
32
- # alias for name (remove! add depreciated api call) ?? why? why not??
33
-
34
- def title() name; end
35
- def title=(value) self.name = value; end
36
-
37
- scope :by_title, -> { order( 'name desc' ) } ### depreciated ??? - use by_name
38
-
39
-
40
30
  end # class Tag
41
31
 
32
+ end # module Model
33
+
34
+ #####
35
+ # add convenience module alias in plural
36
+ # e.g. lets you use include TagDb::Models
37
+ Models = Model
42
38
 
43
- end # module TagDb
44
- end # module Model
39
+ end # module TagDb
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module TagDb
4
+ module Model
5
+
6
+ class Tag
7
+
8
+ ##################################################
9
+ # alias for name (depreciated api calls)
10
+
11
+ def title() name; end
12
+ def title=(value) self.name = value; end
13
+
14
+ scope :by_title, -> { order( 'name desc' ) }
15
+
16
+ end # class Tag
17
+
18
+
19
+ end # module TagDb
20
+ end # module Model
File without changes
@@ -0,0 +1,100 @@
1
+ # encoding: UTF-8
2
+
3
+ ### use TagUtils - why? why not??
4
+ module TagDb
5
+
6
+
7
+ class TagReader
8
+
9
+ include LogUtils::Logging
10
+
11
+ ## make models available by default with namespace
12
+ # e.g. lets you use Tag instead of Model::Tag
13
+ include Models
14
+
15
+ ## value helpers e.g. is_year?, is_taglist? etc.
16
+ include TextUtils::ValueHelper
17
+
18
+
19
+ attr_reader :include_path
20
+
21
+ def strict?() @strict == true; end
22
+
23
+ def initialize( include_path, opts = {} )
24
+
25
+ @include_path = include_path
26
+
27
+ ## option: for now issue warning on update, that is, if key/record (country,region,city) already exists
28
+ @strict = opts[:strict].present? ? true : false
29
+ end
30
+
31
+
32
+ def read( name )
33
+ ## check for grade in name e.g. tag.1, tag.2 etc.
34
+
35
+ if name =~ /^tag.*\.(\d)$/
36
+ read_worker( name, :grade => $1.to_i )
37
+ else
38
+ read_worker( name )
39
+ end
40
+ end
41
+
42
+
43
+ def read_worker( name, more_attribs={} )
44
+
45
+ reader = HashReaderV2.new( name, include_path )
46
+
47
+ grade = 1
48
+
49
+ if more_attribs[:grade].present?
50
+ grade = more_attribs[:grade].to_i
51
+ end
52
+
53
+ reader.each do |key, value|
54
+
55
+ ### fix/todo: move to Tag.read method for reuse !!!!
56
+
57
+
58
+ ### split value by comma (e.g. northern america,southern america, etc.)
59
+ logger.debug "adding grade #{grade} tags >>#{key}<< >>#{value}<<..."
60
+ tag_pairs = value.split(',')
61
+ tag_pairs.each do |pair|
62
+ ## split key|name
63
+ values = pair.split('|')
64
+
65
+ key = values[0]
66
+ ### remove (optional comment) from key (e.g. carribean (islands))
67
+ key = key.gsub( /\(.+\)/, '' )
68
+ ## remove leading n trailing space
69
+ key = key.strip
70
+
71
+ name = values[1] || '' # nb: name might be empty/missing
72
+ name = name.strip
73
+
74
+ tag_attribs = {}
75
+
76
+ ## check if it exists
77
+ ## todo/fix: add country_id for lookup?
78
+ tag = Tag.find_by_key( key )
79
+ if tag.present?
80
+ logger.debug "update tag #{tag.id}-#{tag.key}:"
81
+ else
82
+ logger.debug "create tag:"
83
+ tag = Tag.new
84
+ tag_attribs[ :key ] = key
85
+ end
86
+
87
+ tag_attribs[ :name ] = name
88
+ tag_attribs[ :grade ] = grade
89
+
90
+ logger.debug tag_attribs.to_json
91
+
92
+ tag.update_attributes!( tag_attribs )
93
+ end
94
+ end # each key,value
95
+
96
+ end # method read_worker
97
+
98
+
99
+ end # class TagReader
100
+ end # module TagDb
File without changes
@@ -1,4 +1,4 @@
1
1
 
2
2
  module TagUtils
3
- VERSION = '0.2.0'
3
+ VERSION = '0.2.1'
4
4
  end
data/test/helper.rb CHANGED
@@ -20,6 +20,7 @@ require 'logger'
20
20
 
21
21
  require 'active_record'
22
22
 
23
+
23
24
  # our own code
24
25
 
25
26
  require 'tagutils'
@@ -78,10 +79,11 @@ def setup_in_memory_db
78
79
  ## build schema
79
80
 
80
81
  LogDb.create
82
+ ConfDb.create
83
+
81
84
  TagDb.create
82
85
  CategoryDb.create
83
86
  CreateMovieDb.new.up
84
-
85
87
  end
86
88
 
87
89
 
data/test/tags.1.yml ADDED
@@ -0,0 +1,35 @@
1
+ ### setup some grade 1 tags
2
+
3
+ ## some tags
4
+
5
+ continents: africa, america, asia, europe, oceania
6
+
7
+ ####################
8
+ # geo regions
9
+ africa: northern africa, western africa, central africa, eastern africa, southern africa
10
+
11
+ americas: north america, south america, central america, caribbean (islands)
12
+
13
+ ## check if yaml can handle key w/ space??
14
+ south_america: andean states, southern cone
15
+
16
+ europe: northern europe, southern europe, western europe, central europe, eastern europe, southeastern europe
17
+
18
+ ## more regions
19
+ europe2: baltic (states), scandinavia, nordic (countries), balkans
20
+
21
+
22
+ ## get used for more than one continent
23
+ more: middle east, indian ocean, atlantic ocean
24
+
25
+ # orgs
26
+
27
+ orgs: un, g5, g8, g20, eu, commonwealth, mercosur, nafta
28
+ football: fifa, uefa, afc, ofc, caf, csf, concacaf
29
+
30
+ europe3: benelux, euro, schengen
31
+
32
+ misc: microstate
33
+
34
+ # national regions
35
+ brasil: s|South, se|Southeast, co|Centerwest, ne|Northeast, n|North
@@ -0,0 +1,23 @@
1
+ # encoding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class TestTagReader < MiniTest::Unit::TestCase
6
+
7
+ def setup
8
+ TagDb.delete!
9
+ CategoryDb.delete!
10
+
11
+ Movie.delete_all
12
+ end
13
+
14
+ def test_read
15
+ include_path = "#{TagUtils.root}/test"
16
+ puts "[TestTagReader.test_read] include_path: #{include_path}"
17
+
18
+ reader = TagDb::TagReader.new( include_path )
19
+ reader.read( 'tags.1' )
20
+ end
21
+
22
+
23
+ end # class TestTagReader
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tagutils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-09 00:00:00.000000000 Z
12
+ date: 2014-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &85193010 !ruby/object:Gem::Requirement
16
+ requirement: &86092400 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *85193010
24
+ version_requirements: *86092400
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: logutils
27
- requirement: &85192530 !ruby/object:Gem::Requirement
27
+ requirement: &86091990 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0.6'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *85192530
35
+ version_requirements: *86091990
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rdoc
38
- requirement: &85191800 !ruby/object:Gem::Requirement
38
+ requirement: &86091650 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '3.10'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *85191800
46
+ version_requirements: *86091650
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: hoe
49
- requirement: &85191060 !ruby/object:Gem::Requirement
49
+ requirement: &86091280 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '3.3'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *85191060
57
+ version_requirements: *86091280
58
58
  description: tagutils - tag utilities (tag, taggings, tag list, etc.)
59
59
  email: openmundi@googlegroups.com
60
60
  executables: []
@@ -67,16 +67,24 @@ files:
67
67
  - README.md
68
68
  - Rakefile
69
69
  - lib/tagutils.rb
70
- - lib/tagutils/active_record.rb
71
- - lib/tagutils/models/categorization.rb
72
- - lib/tagutils/models/category.rb
73
- - lib/tagutils/models/tag.rb
74
- - lib/tagutils/models/tagging.rb
75
- - lib/tagutils/schema_categories.rb
76
- - lib/tagutils/schema_tags.rb
70
+ - lib/tagutils/categories.rb
71
+ - lib/tagutils/categories/active_record.rb
72
+ - lib/tagutils/categories/models/categorization.rb
73
+ - lib/tagutils/categories/models/category.rb
74
+ - lib/tagutils/categories/models/category_comp.rb
75
+ - lib/tagutils/categories/schema.rb
76
+ - lib/tagutils/tags.rb
77
+ - lib/tagutils/tags/active_record.rb
78
+ - lib/tagutils/tags/models/tag.rb
79
+ - lib/tagutils/tags/models/tag_comp.rb
80
+ - lib/tagutils/tags/models/tagging.rb
81
+ - lib/tagutils/tags/readers/tag.rb
82
+ - lib/tagutils/tags/schema.rb
77
83
  - lib/tagutils/version.rb
78
84
  - test/helper.rb
85
+ - test/tags.1.yml
79
86
  - test/test_models.rb
87
+ - test/test_tag_reader.rb
80
88
  - .gemtest
81
89
  homepage: https://github.com/rubylibs/tagutils
82
90
  licenses:
@@ -107,3 +115,4 @@ specification_version: 3
107
115
  summary: tagutils - tag utilities (tag, taggings, tag list, etc.)
108
116
  test_files:
109
117
  - test/test_models.rb
118
+ - test/test_tag_reader.rb