faceted 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +14 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +20 -0
- data/README.md +6 -0
- data/README.rdoc +21 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/faceted.rb +3 -0
- data/lib/faceted/presenter.rb +116 -0
- metadata +141 -0
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "shoulda", ">= 0"
|
10
|
+
gem "rdoc", "~> 3.12"
|
11
|
+
gem "bundler"
|
12
|
+
gem "jeweler", "~> 1.8.4"
|
13
|
+
gem "simplecov"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activesupport (3.2.8)
|
5
|
+
i18n (~> 0.6)
|
6
|
+
multi_json (~> 1.0)
|
7
|
+
git (1.2.5)
|
8
|
+
i18n (0.6.1)
|
9
|
+
jeweler (1.8.4)
|
10
|
+
bundler (~> 1.0)
|
11
|
+
git (>= 1.2.5)
|
12
|
+
rake
|
13
|
+
rdoc
|
14
|
+
json (1.7.5)
|
15
|
+
multi_json (1.3.6)
|
16
|
+
rake (0.9.2.2)
|
17
|
+
rdoc (3.12)
|
18
|
+
json (~> 1.4)
|
19
|
+
shoulda (3.1.1)
|
20
|
+
shoulda-context (~> 1.0)
|
21
|
+
shoulda-matchers (~> 1.2)
|
22
|
+
shoulda-context (1.0.0)
|
23
|
+
shoulda-matchers (1.3.0)
|
24
|
+
activesupport (>= 3.0.0)
|
25
|
+
simplecov (0.6.4)
|
26
|
+
multi_json (~> 1.0)
|
27
|
+
simplecov-html (~> 0.5.3)
|
28
|
+
simplecov-html (0.5.3)
|
29
|
+
|
30
|
+
PLATFORMS
|
31
|
+
ruby
|
32
|
+
|
33
|
+
DEPENDENCIES
|
34
|
+
bundler
|
35
|
+
jeweler (~> 1.8.4)
|
36
|
+
rdoc (~> 3.12)
|
37
|
+
shoulda
|
38
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Corey Ehmke
|
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.md
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
= faceted
|
2
|
+
|
3
|
+
A set of tools, patterns, and modules for use in API implementations.
|
4
|
+
|
5
|
+
This software is alpha and not production-ready.
|
6
|
+
|
7
|
+
== Contributing to faceted
|
8
|
+
|
9
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
|
10
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
|
11
|
+
* Fork the project.
|
12
|
+
* Start a feature/bugfix branch.
|
13
|
+
* Commit and push until you are happy with your contribution.
|
14
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
15
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
16
|
+
|
17
|
+
== Copyright
|
18
|
+
|
19
|
+
Copyright (c) 2012 Corey Ehmke. See LICENSE.txt for
|
20
|
+
further details.
|
21
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "faceted"
|
18
|
+
gem.homepage = "http://github.com/trunkclub/faceted"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Provides useful tools for API implementations.}
|
21
|
+
gem.description = %Q{Provides useful tools for API implementations.}
|
22
|
+
gem.email = "corey@trunkclub.com"
|
23
|
+
gem.authors = ["Corey Ehmke", "Max Thom Stahl"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rake/testtask'
|
29
|
+
Rake::TestTask.new(:test) do |test|
|
30
|
+
test.libs << 'lib' << 'test'
|
31
|
+
test.pattern = 'test/**/test_*.rb'
|
32
|
+
test.verbose = true
|
33
|
+
end
|
34
|
+
|
35
|
+
task :default => :test
|
36
|
+
|
37
|
+
require 'rdoc/task'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "faceted #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/faceted.rb
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
module Faceted
|
2
|
+
|
3
|
+
module Presenter
|
4
|
+
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# Class methods ==========================================================
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ActiveModel::Naming
|
11
|
+
base.extend ClassMethods
|
12
|
+
base.send(:attr_accessor, :id)
|
13
|
+
base.send(:attr_accessor, :errors)
|
14
|
+
base.send(:attr_accessor, :success)
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
def build_association_from(field)
|
20
|
+
bare_name = field.gsub("_id", "")
|
21
|
+
klass = eval "#{scope}::#{bare_name.classify}"
|
22
|
+
define_method :"#{bare_name}" do
|
23
|
+
klass.new(:id => self.send(field))
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def field(name, args={})
|
28
|
+
|
29
|
+
fields << name
|
30
|
+
|
31
|
+
define_method :"#{name}" do
|
32
|
+
instance_variable_get("@#{name}") || args[:default]
|
33
|
+
end
|
34
|
+
|
35
|
+
define_method :"#{name}=" do |val|
|
36
|
+
instance_variable_set("@#{name}", val)
|
37
|
+
end
|
38
|
+
|
39
|
+
build_association_from(name.to_s) if name.to_s.include?("_id") && ! args[:skip_association]
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
def create(params={})
|
44
|
+
object = self.new(params)
|
45
|
+
object.save
|
46
|
+
object
|
47
|
+
end
|
48
|
+
|
49
|
+
def fields
|
50
|
+
@fields ||= []
|
51
|
+
end
|
52
|
+
|
53
|
+
def fields=(*args)
|
54
|
+
fields << args
|
55
|
+
end
|
56
|
+
|
57
|
+
def materialize(objects=[])
|
58
|
+
objects.compact.inject([]) do |a, object|
|
59
|
+
presenter = self.new
|
60
|
+
presenter.object = object
|
61
|
+
presenter.initialize_with_object
|
62
|
+
a << presenter
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def presented_class
|
67
|
+
@presents
|
68
|
+
end
|
69
|
+
|
70
|
+
def presents(name, args={})
|
71
|
+
@presents = args[:class_name] || name.to_s.classify
|
72
|
+
end
|
73
|
+
|
74
|
+
def scope
|
75
|
+
parent.to_s == "Object" ? "::" : "#{parent.to_s}::"
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
# Instance methods =======================================================
|
81
|
+
|
82
|
+
def initialize(args={})
|
83
|
+
self.id = args[:id]
|
84
|
+
self.initialize_with_object
|
85
|
+
args.symbolize_keys.delete_if{|k,v| v.nil?}.each{|k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") && ! v.blank? }
|
86
|
+
self.errors = []
|
87
|
+
self.success = true
|
88
|
+
end
|
89
|
+
|
90
|
+
def initialize_with_object
|
91
|
+
return unless object
|
92
|
+
object.attributes.each{ |k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") }
|
93
|
+
end
|
94
|
+
|
95
|
+
def object
|
96
|
+
@object ||= self.id ? eval(self.class.presented_class).find(self.id) : nil
|
97
|
+
end
|
98
|
+
|
99
|
+
def object=(obj)
|
100
|
+
@object = obj
|
101
|
+
end
|
102
|
+
|
103
|
+
def save
|
104
|
+
self.class.fields.each do |k|
|
105
|
+
self.object.send("#{k}=", self.send(k)) if self.object.respond_to?("#{k}=")
|
106
|
+
end
|
107
|
+
self.object.save!
|
108
|
+
end
|
109
|
+
|
110
|
+
def to_hash
|
111
|
+
self.fields.inject({}) {|h,k| h[k] = self.send(k)}
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: faceted
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Corey Ehmke
|
9
|
+
- Max Thom Stahl
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-09-12 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: shoulda
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: rdoc
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '3.12'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '3.12'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
- !ruby/object:Gem::Dependency
|
64
|
+
name: jeweler
|
65
|
+
requirement: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ~>
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 1.8.4
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ~>
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: 1.8.4
|
79
|
+
- !ruby/object:Gem::Dependency
|
80
|
+
name: simplecov
|
81
|
+
requirement: !ruby/object:Gem::Requirement
|
82
|
+
none: false
|
83
|
+
requirements:
|
84
|
+
- - ! '>='
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
type: :development
|
88
|
+
prerelease: false
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
description: Provides useful tools for API implementations.
|
96
|
+
email: corey@trunkclub.com
|
97
|
+
executables: []
|
98
|
+
extensions: []
|
99
|
+
extra_rdoc_files:
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
- README.rdoc
|
103
|
+
files:
|
104
|
+
- Gemfile
|
105
|
+
- Gemfile.lock
|
106
|
+
- LICENSE.txt
|
107
|
+
- README.md
|
108
|
+
- README.rdoc
|
109
|
+
- Rakefile
|
110
|
+
- VERSION
|
111
|
+
- lib/faceted.rb
|
112
|
+
- lib/faceted/presenter.rb
|
113
|
+
homepage: http://github.com/trunkclub/faceted
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
post_install_message:
|
117
|
+
rdoc_options: []
|
118
|
+
require_paths:
|
119
|
+
- lib
|
120
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ! '>='
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
segments:
|
127
|
+
- 0
|
128
|
+
hash: -4300348204820488567
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
none: false
|
131
|
+
requirements:
|
132
|
+
- - ! '>='
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 1.8.24
|
138
|
+
signing_key:
|
139
|
+
specification_version: 3
|
140
|
+
summary: Provides useful tools for API implementations.
|
141
|
+
test_files: []
|