metadata 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +41 -0
- data/Rakefile +2 -0
- data/lib/metadata/formats/base.rb +48 -0
- data/lib/metadata/formats/json.rb +9 -0
- data/lib/metadata/formats/string.rb +79 -0
- data/lib/metadata/formats.rb +13 -0
- data/lib/metadata/hook.rb +20 -0
- data/lib/metadata/rails/model/active_record.rb +3 -0
- data/lib/metadata/version.rb +3 -0
- data/lib/metadata.rb +13 -0
- data/metadata.gemspec +17 -0
- metadata +61 -0
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,41 @@
|
|
1
|
+
# Metadata
|
2
|
+
|
3
|
+
## Usage
|
4
|
+
|
5
|
+
class User < ActiveRecord::Base
|
6
|
+
metadata do |obj|
|
7
|
+
puts obj.name
|
8
|
+
is a: 'user'
|
9
|
+
with friends: obj.friends.count
|
10
|
+
stop
|
11
|
+
end
|
12
|
+
|
13
|
+
Results in
|
14
|
+
|
15
|
+
Joe is a user with 37 friends.
|
16
|
+
|
17
|
+
## Installation
|
18
|
+
|
19
|
+
Add this line to your application's Gemfile:
|
20
|
+
|
21
|
+
gem 'metadata'
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install metadata
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
TODO: Write usage instructions here
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
module Metadata
|
2
|
+
module Formats
|
3
|
+
class Base
|
4
|
+
attr_accessor :output
|
5
|
+
attr_accessor :object
|
6
|
+
|
7
|
+
def initialize(object, &block)
|
8
|
+
@output = []
|
9
|
+
@object = object
|
10
|
+
|
11
|
+
instance_exec(@object, &block)
|
12
|
+
end
|
13
|
+
|
14
|
+
def puts(str)
|
15
|
+
str = str.capitalize if @output.empty?
|
16
|
+
@output << str
|
17
|
+
end
|
18
|
+
|
19
|
+
def is(*args)
|
20
|
+
end
|
21
|
+
|
22
|
+
def was(*args)
|
23
|
+
end
|
24
|
+
|
25
|
+
def it(*args)
|
26
|
+
end
|
27
|
+
|
28
|
+
def around(*args)
|
29
|
+
end
|
30
|
+
|
31
|
+
def about(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
def with(*args)
|
35
|
+
end
|
36
|
+
|
37
|
+
def stop
|
38
|
+
end
|
39
|
+
|
40
|
+
protected
|
41
|
+
|
42
|
+
def word(word)
|
43
|
+
word.to_s # @todo Translate
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
48
|
+
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,20 @@
|
|
1
|
+
module Metadata
|
2
|
+
module Hook
|
3
|
+
|
4
|
+
def metadata(args={})
|
5
|
+
# Self right now is the object that has metadata on it
|
6
|
+
options = self.class._metadata.merge(args)
|
7
|
+
format = Metadata::Format.named(options[:format])
|
8
|
+
format.new(self, &options[:block])
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
attr_accessor :_metadata
|
13
|
+
|
14
|
+
def metadata(*args, &block)
|
15
|
+
options = args.first.is_a?(Hash) ? args.first : {}
|
16
|
+
self._metadata = {format: options[:format] || :string, block: block}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/metadata.rb
ADDED
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
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metadata
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Colin Young
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-30 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: Generate metadata from any ruby object (works great with rails.)
|
15
|
+
email:
|
16
|
+
- me@colinyoung.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_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
|
35
|
+
homepage: https://github.com/colinyoung/metadata
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.10
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: Metadata is an easy way to represent an object in a human or machine readable
|
59
|
+
format.
|
60
|
+
test_files: []
|
61
|
+
has_rdoc:
|