metadata 0.0.2 → 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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +53 -0
- data/Rakefile +2 -0
- data/lib/metadata.rb +13 -0
- data/lib/metadata/formats.rb +13 -0
- data/lib/metadata/formats/base.rb +50 -0
- data/lib/metadata/formats/json.rb +9 -0
- data/lib/metadata/formats/string.rb +79 -0
- data/lib/metadata/hook.rb +22 -0
- data/lib/metadata/rails/model/active_record.rb +3 -0
- data/lib/metadata/version.rb +3 -0
- data/metadata.gemspec +17 -0
- metadata +18 -4
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Colin Young
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Metadata
|
2
|
+
|
3
|
+

|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class User < ActiveRecord::Base
|
9
|
+
...
|
10
|
+
metadata do |obj|
|
11
|
+
puts obj.name
|
12
|
+
is a: 'user'
|
13
|
+
with friends: obj.friends.count
|
14
|
+
stop
|
15
|
+
end
|
16
|
+
...
|
17
|
+
end
|
18
|
+
```
|
19
|
+
|
20
|
+
Results in
|
21
|
+
|
22
|
+
Joe is a user with 37 friends.
|
23
|
+
|
24
|
+
Why is this useful?
|
25
|
+
|
26
|
+
* Translation
|
27
|
+
* Other formats
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
gem 'metadata'
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install metadata
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
TODO: Write usage instructions here
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
52
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
53
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/metadata.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
module Metadata
|
2
|
+
module Formats
|
3
|
+
class Base
|
4
|
+
include ActionView::Helpers if defined? Rails
|
5
|
+
|
6
|
+
attr_accessor :output
|
7
|
+
attr_accessor :object
|
8
|
+
|
9
|
+
def initialize(object, &block)
|
10
|
+
@output = []
|
11
|
+
@object = object
|
12
|
+
|
13
|
+
instance_exec(@object, &block)
|
14
|
+
end
|
15
|
+
|
16
|
+
def puts(str)
|
17
|
+
str = str.capitalize if @output.empty?
|
18
|
+
@output << str
|
19
|
+
end
|
20
|
+
|
21
|
+
def is(*args)
|
22
|
+
end
|
23
|
+
|
24
|
+
def was(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
def it(*args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def around(*args)
|
31
|
+
end
|
32
|
+
|
33
|
+
def about(*args)
|
34
|
+
end
|
35
|
+
|
36
|
+
def with(*args)
|
37
|
+
end
|
38
|
+
|
39
|
+
def stop
|
40
|
+
end
|
41
|
+
|
42
|
+
protected
|
43
|
+
|
44
|
+
def word(word)
|
45
|
+
word.to_s # @todo Translate
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Metadata
|
2
|
+
module Formats
|
3
|
+
class String < Base
|
4
|
+
|
5
|
+
DICTIONARY = {
|
6
|
+
'is' => {
|
7
|
+
present: 'is',
|
8
|
+
past: 'was'
|
9
|
+
}
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize(object, &block)
|
13
|
+
@next = {}
|
14
|
+
super(object, &block)
|
15
|
+
end
|
16
|
+
|
17
|
+
def is(something, tense=:present)
|
18
|
+
something = pairs(something) if something.is_a?(Hash)
|
19
|
+
|
20
|
+
@output << statement(change_tense(word(:is), tense), something)
|
21
|
+
end
|
22
|
+
|
23
|
+
def was(something)
|
24
|
+
is(something, :past)
|
25
|
+
end
|
26
|
+
|
27
|
+
def it(something)
|
28
|
+
something = pairs(something) if something.is_a?(Hash)
|
29
|
+
|
30
|
+
@output << statement(word(:it), something)
|
31
|
+
end
|
32
|
+
|
33
|
+
def with(hash)
|
34
|
+
@output << ::String.new.tap do |s|
|
35
|
+
hash.map do |k,v|
|
36
|
+
k = k.to_s
|
37
|
+
k = k.singularize if k.respond_to? :singularize and v == 1
|
38
|
+
s << statement(word(:with), v, k)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def stop
|
44
|
+
@output << "."
|
45
|
+
@next[:capitalize] = true
|
46
|
+
end
|
47
|
+
|
48
|
+
def around(*args)
|
49
|
+
@output << statement(word(:around), args.first)
|
50
|
+
end
|
51
|
+
|
52
|
+
def and_
|
53
|
+
@output << word(:and)
|
54
|
+
end
|
55
|
+
|
56
|
+
def to_s
|
57
|
+
statement(*@output)
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def statement(*args)
|
63
|
+
if @next[:capitalize]
|
64
|
+
args[0] = args[0].to_s.capitalize
|
65
|
+
@next.delete :capitalize
|
66
|
+
end
|
67
|
+
args.join(' ').strip.gsub(' .', '.')
|
68
|
+
end
|
69
|
+
|
70
|
+
def pairs(something)
|
71
|
+
statement(something.keys.first, something.values.first)
|
72
|
+
end
|
73
|
+
|
74
|
+
def change_tense(verb, tense)
|
75
|
+
DICTIONARY[verb][tense]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Metadata
|
2
|
+
module Hook
|
3
|
+
|
4
|
+
def metadata(args={})
|
5
|
+
# Self right now is the object that has metadata on it
|
6
|
+
return if self.class._metadata.nil?
|
7
|
+
|
8
|
+
options = self.class._metadata.merge(args)
|
9
|
+
format = Metadata::Format.named(options[:format])
|
10
|
+
format.new(self, &options[:block])
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
attr_accessor :_metadata
|
15
|
+
|
16
|
+
def metadata(*args, &block)
|
17
|
+
options = args.first.is_a?(Hash) ? args.first : {}
|
18
|
+
self._metadata = {format: options[:format] || :string, block: block}
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/metadata.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/metadata/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Colin Young"]
|
6
|
+
gem.email = ["me@colinyoung.com"]
|
7
|
+
gem.description = %q{Generate metadata from any ruby object (works great with rails.)}
|
8
|
+
gem.summary = %q{Metadata is an easy way to represent an object in a human or machine readable format.}
|
9
|
+
gem.homepage = "https://github.com/colinyoung/metadata"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "metadata"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Metadata::VERSION
|
17
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: metadata
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-06 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Generate metadata from any ruby object (works great with rails.)
|
15
15
|
email:
|
@@ -17,7 +17,21 @@ email:
|
|
17
17
|
executables: []
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
|
-
files:
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/metadata.rb
|
27
|
+
- lib/metadata/formats.rb
|
28
|
+
- lib/metadata/formats/base.rb
|
29
|
+
- lib/metadata/formats/json.rb
|
30
|
+
- lib/metadata/formats/string.rb
|
31
|
+
- lib/metadata/hook.rb
|
32
|
+
- lib/metadata/rails/model/active_record.rb
|
33
|
+
- lib/metadata/version.rb
|
34
|
+
- metadata.gemspec
|
21
35
|
homepage: https://github.com/colinyoung/metadata
|
22
36
|
licenses: []
|
23
37
|
post_install_message:
|
@@ -38,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
52
|
version: '0'
|
39
53
|
requirements: []
|
40
54
|
rubyforge_project:
|
41
|
-
rubygems_version: 1.8.
|
55
|
+
rubygems_version: 1.8.24
|
42
56
|
signing_key:
|
43
57
|
specification_version: 3
|
44
58
|
summary: Metadata is an easy way to represent an object in a human or machine readable
|