faceted 0.6.4 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/faceted.gemspec +5 -4
- data/lib/faceted.rb +2 -0
- data/lib/faceted/collector.rb +2 -6
- data/lib/faceted/controller.rb +2 -2
- data/lib/faceted/has_object.rb +68 -0
- data/lib/faceted/interface.rb +39 -0
- data/lib/faceted/model.rb +60 -0
- data/lib/faceted/presenter.rb +4 -95
- metadata +5 -4
- data/README.rdoc +0 -21
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.7.0
|
data/faceted.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "faceted"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.7.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Corey Ehmke", "Max Thom Stahl"]
|
@@ -14,8 +14,7 @@ Gem::Specification.new do |s|
|
|
14
14
|
s.email = "corey@trunkclub.com"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.md"
|
18
|
-
"README.rdoc"
|
17
|
+
"README.md"
|
19
18
|
]
|
20
19
|
s.files = [
|
21
20
|
".rspec",
|
@@ -23,13 +22,15 @@ Gem::Specification.new do |s|
|
|
23
22
|
"Gemfile.lock",
|
24
23
|
"LICENSE.txt",
|
25
24
|
"README.md",
|
26
|
-
"README.rdoc",
|
27
25
|
"Rakefile",
|
28
26
|
"VERSION",
|
29
27
|
"faceted.gemspec",
|
30
28
|
"lib/faceted.rb",
|
31
29
|
"lib/faceted/collector.rb",
|
32
30
|
"lib/faceted/controller.rb",
|
31
|
+
"lib/faceted/has_object.rb",
|
32
|
+
"lib/faceted/interface.rb",
|
33
|
+
"lib/faceted/model.rb",
|
33
34
|
"lib/faceted/presenter.rb",
|
34
35
|
"spec/collector_spec.rb",
|
35
36
|
"spec/presenter_spec.rb",
|
data/lib/faceted.rb
CHANGED
data/lib/faceted/collector.rb
CHANGED
@@ -2,14 +2,14 @@ module Faceted
|
|
2
2
|
|
3
3
|
module Collector
|
4
4
|
|
5
|
-
|
6
|
-
require 'active_support/core_ext/hash'
|
5
|
+
include Faceted::Model
|
7
6
|
|
8
7
|
def self.included(base)
|
9
8
|
base.extend ActiveModel::Naming
|
10
9
|
base.send(:attr_accessor, :errors)
|
11
10
|
base.send(:attr_accessor, :success)
|
12
11
|
base.extend ClassMethods
|
12
|
+
base.extend Faceted::Model::ModelClassMethods
|
13
13
|
end
|
14
14
|
|
15
15
|
# Class methods ===========================================================
|
@@ -31,10 +31,6 @@ module Faceted
|
|
31
31
|
@collects
|
32
32
|
end
|
33
33
|
|
34
|
-
def scope
|
35
|
-
parent.to_s == "Object" ? "::" : "#{parent.to_s}::"
|
36
|
-
end
|
37
|
-
|
38
34
|
end
|
39
35
|
|
40
36
|
# Instance methods =========================================================
|
data/lib/faceted/controller.rb
CHANGED
@@ -9,7 +9,7 @@ module Faceted
|
|
9
9
|
success: obj.success,
|
10
10
|
response: obj.to_hash,
|
11
11
|
errors: obj.errors
|
12
|
-
}
|
12
|
+
}
|
13
13
|
end
|
14
14
|
|
15
15
|
# For rendering a response with a multiple objects, e.g.
|
@@ -19,7 +19,7 @@ module Faceted
|
|
19
19
|
success: true,
|
20
20
|
response: {"#{key}".to_sym => array},
|
21
21
|
errors: nil
|
22
|
-
}
|
22
|
+
}
|
23
23
|
end
|
24
24
|
|
25
25
|
# In your base API controller:
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Faceted
|
2
|
+
|
3
|
+
module HasObject
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def fields
|
8
|
+
@fields ||= [:id]
|
9
|
+
end
|
10
|
+
|
11
|
+
def materialize(objects=[])
|
12
|
+
objects.compact.inject([]) do |a, object|
|
13
|
+
instance = self.new
|
14
|
+
instance.send(:object=, object)
|
15
|
+
instance.send(:initialize_with_object)
|
16
|
+
a << instance
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
|
22
|
+
# Instance methods =======================================================
|
23
|
+
|
24
|
+
def initialize(args={})
|
25
|
+
self.id = args[:id]
|
26
|
+
initialize_with_object
|
27
|
+
! args.empty? && args.symbolize_keys.delete_if{|k,v| v.nil?}.each{|k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") && ! v.blank? }
|
28
|
+
self.errors = []
|
29
|
+
self.success = true
|
30
|
+
end
|
31
|
+
|
32
|
+
def save
|
33
|
+
return false unless schema_fields
|
34
|
+
schema_fields.each{ |k| object.send("#{k}=", self.send(k)) if object.respond_to?("#{k}=") }
|
35
|
+
self.success = object.save
|
36
|
+
self.errors = object.errors && object.errors.full_messages
|
37
|
+
self.success
|
38
|
+
end
|
39
|
+
|
40
|
+
def schema_fields
|
41
|
+
self.class.fields
|
42
|
+
end
|
43
|
+
|
44
|
+
def to_hash
|
45
|
+
schema_fields.inject({}) {|h,k| h[k] = self.send(k); h}
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def initialize_with_object
|
51
|
+
return unless object
|
52
|
+
schema_fields.each{ |k| self.send("#{k}=", object.send(k)) if self.respond_to?("#{k}=") }
|
53
|
+
end
|
54
|
+
|
55
|
+
def object
|
56
|
+
return unless self.class.klass
|
57
|
+
@object ||= self.id ? self.class.klass.find(self.id) : self.class.klass.new
|
58
|
+
end
|
59
|
+
|
60
|
+
def object=(obj)
|
61
|
+
@object = obj
|
62
|
+
self.id = obj.id
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
module Faceted
|
2
|
+
|
3
|
+
module Interface
|
4
|
+
|
5
|
+
include Faceted::HasObject
|
6
|
+
|
7
|
+
# Class methods ===========================================================
|
8
|
+
|
9
|
+
def self.included(base)
|
10
|
+
base.extend ActiveModel::Naming
|
11
|
+
base.extend ClassMethods
|
12
|
+
base.extend Faceted::Model::ModelClassMethods
|
13
|
+
base.send(:attr_accessor, :id)
|
14
|
+
base.send(:attr_accessor, :errors)
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
|
19
|
+
def klass
|
20
|
+
@wraps
|
21
|
+
end
|
22
|
+
|
23
|
+
def wraps(name, args={})
|
24
|
+
class_name = args[:class_name] || name.to_s.classify
|
25
|
+
@wraps = eval(class_name)
|
26
|
+
define_method :"#{class_name.downcase}" do
|
27
|
+
object
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def where(args)
|
32
|
+
materialize(klass.where(args))
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module Faceted
|
2
|
+
|
3
|
+
module Model
|
4
|
+
|
5
|
+
require 'json'
|
6
|
+
require 'active_support/core_ext/hash'
|
7
|
+
|
8
|
+
# Class methods ============================================================
|
9
|
+
|
10
|
+
module ModelClassMethods
|
11
|
+
|
12
|
+
def build_association_from(field)
|
13
|
+
bare_name = field.gsub(/_id$/, '')
|
14
|
+
if field =~ /_id$/
|
15
|
+
klass = eval "#{scope}#{bare_name.classify}"
|
16
|
+
define_method :"#{bare_name}" do
|
17
|
+
klass.new(:id => self.send(field))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def create(params={})
|
23
|
+
obj = self.new(params)
|
24
|
+
obj.save
|
25
|
+
obj
|
26
|
+
end
|
27
|
+
|
28
|
+
def field(name, args={})
|
29
|
+
fields << name
|
30
|
+
define_method :"#{name}" do
|
31
|
+
instance_variable_get("@#{name}") || args[:default]
|
32
|
+
end
|
33
|
+
define_method :"#{name}=" do |val|
|
34
|
+
instance_variable_set("@#{name}", val)
|
35
|
+
end
|
36
|
+
build_association_from(name.to_s) if name.to_s.include?("id") && ! args[:skip_association]
|
37
|
+
end
|
38
|
+
|
39
|
+
def fields
|
40
|
+
@fields ||= [:id]
|
41
|
+
end
|
42
|
+
|
43
|
+
def materialize(objects=[])
|
44
|
+
objects.compact.inject([]) do |a, object|
|
45
|
+
interface = self.new
|
46
|
+
interface.send(:object=, object)
|
47
|
+
interface.send(:initialize_with_object)
|
48
|
+
a << interface
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def scope
|
53
|
+
parent.to_s == "Object" ? "::" : "#{parent.to_s}::"
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
data/lib/faceted/presenter.rb
CHANGED
@@ -2,14 +2,14 @@ module Faceted
|
|
2
2
|
|
3
3
|
module Presenter
|
4
4
|
|
5
|
-
|
6
|
-
require 'active_support/core_ext/hash'
|
5
|
+
include Faceted::HasObject
|
7
6
|
|
8
7
|
# Class methods ===========================================================
|
9
8
|
|
10
9
|
def self.included(base)
|
11
10
|
base.extend ActiveModel::Naming
|
12
11
|
base.extend ClassMethods
|
12
|
+
base.extend Faceted::Model::ModelClassMethods
|
13
13
|
base.send(:attr_accessor, :id)
|
14
14
|
base.send(:attr_accessor, :errors)
|
15
15
|
base.send(:attr_accessor, :success)
|
@@ -17,52 +17,7 @@ module Faceted
|
|
17
17
|
|
18
18
|
module ClassMethods
|
19
19
|
|
20
|
-
def
|
21
|
-
bare_name = field.gsub(/_id$/, '')
|
22
|
-
if field =~ /_id$/
|
23
|
-
klass = eval "#{scope}#{bare_name.classify}"
|
24
|
-
define_method :"#{bare_name}" do
|
25
|
-
klass.new(:id => self.send(field))
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
def create(params={})
|
31
|
-
obj = self.new(params)
|
32
|
-
obj.save
|
33
|
-
obj
|
34
|
-
end
|
35
|
-
|
36
|
-
def field(name, args={})
|
37
|
-
|
38
|
-
fields << name
|
39
|
-
|
40
|
-
define_method :"#{name}" do
|
41
|
-
instance_variable_get("@#{name}") || args[:default]
|
42
|
-
end
|
43
|
-
|
44
|
-
define_method :"#{name}=" do |val|
|
45
|
-
instance_variable_set("@#{name}", val)
|
46
|
-
end
|
47
|
-
|
48
|
-
build_association_from(name.to_s) if name.to_s.include?("id") && ! args[:skip_association]
|
49
|
-
|
50
|
-
end
|
51
|
-
|
52
|
-
def fields
|
53
|
-
@fields ||= [:id]
|
54
|
-
end
|
55
|
-
|
56
|
-
def materialize(objects=[])
|
57
|
-
objects.compact.inject([]) do |a, object|
|
58
|
-
presenter = self.new
|
59
|
-
presenter.send(:object=, object)
|
60
|
-
presenter.send(:initialize_with_object)
|
61
|
-
a << presenter
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
def presented_class
|
20
|
+
def klass
|
66
21
|
@presents
|
67
22
|
end
|
68
23
|
|
@@ -74,58 +29,12 @@ module Faceted
|
|
74
29
|
end
|
75
30
|
end
|
76
31
|
|
77
|
-
def scope
|
78
|
-
parent.to_s == "Object" ? "::" : "#{parent.to_s}::"
|
79
|
-
end
|
80
|
-
|
81
32
|
def where(args)
|
82
|
-
materialize(
|
33
|
+
materialize(klass.where(args))
|
83
34
|
end
|
84
35
|
|
85
36
|
end
|
86
37
|
|
87
|
-
# Instance methods =======================================================
|
88
|
-
|
89
|
-
def initialize(args={})
|
90
|
-
self.id = args[:id]
|
91
|
-
initialize_with_object
|
92
|
-
! args.empty? && args.symbolize_keys.delete_if{|k,v| v.nil?}.each{|k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") && ! v.blank? }
|
93
|
-
self.errors = []
|
94
|
-
self.success = true
|
95
|
-
end
|
96
|
-
|
97
|
-
def save
|
98
|
-
schema_fields.each{ |k| object.send("#{k}=", self.send(k)) if object.respond_to?("#{k}=") }
|
99
|
-
self.success = object.save
|
100
|
-
self.errors = object.errors && object.errors.full_messages
|
101
|
-
self.success
|
102
|
-
end
|
103
|
-
|
104
|
-
def to_hash
|
105
|
-
schema_fields.inject({}) {|h,k| h[k] = self.send(k); h}
|
106
|
-
end
|
107
|
-
|
108
|
-
private
|
109
|
-
|
110
|
-
def initialize_with_object
|
111
|
-
return unless object
|
112
|
-
schema_fields.each{ |k| self.send("#{k}=", object.send(k)) if self.respond_to?("#{k}=") }
|
113
|
-
end
|
114
|
-
|
115
|
-
def object
|
116
|
-
return unless self.class.presented_class
|
117
|
-
@object ||= self.id ? self.class.presented_class.find(self.id) : self.class.presented_class.new
|
118
|
-
end
|
119
|
-
|
120
|
-
def object=(obj)
|
121
|
-
@object = obj
|
122
|
-
self.id = obj.id
|
123
|
-
end
|
124
|
-
|
125
|
-
def schema_fields
|
126
|
-
self.class.fields
|
127
|
-
end
|
128
|
-
|
129
38
|
end
|
130
39
|
|
131
40
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faceted
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -147,20 +147,21 @@ extensions: []
|
|
147
147
|
extra_rdoc_files:
|
148
148
|
- LICENSE.txt
|
149
149
|
- README.md
|
150
|
-
- README.rdoc
|
151
150
|
files:
|
152
151
|
- .rspec
|
153
152
|
- Gemfile
|
154
153
|
- Gemfile.lock
|
155
154
|
- LICENSE.txt
|
156
155
|
- README.md
|
157
|
-
- README.rdoc
|
158
156
|
- Rakefile
|
159
157
|
- VERSION
|
160
158
|
- faceted.gemspec
|
161
159
|
- lib/faceted.rb
|
162
160
|
- lib/faceted/collector.rb
|
163
161
|
- lib/faceted/controller.rb
|
162
|
+
- lib/faceted/has_object.rb
|
163
|
+
- lib/faceted/interface.rb
|
164
|
+
- lib/faceted/model.rb
|
164
165
|
- lib/faceted/presenter.rb
|
165
166
|
- spec/collector_spec.rb
|
166
167
|
- spec/presenter_spec.rb
|
@@ -180,7 +181,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
180
181
|
version: '0'
|
181
182
|
segments:
|
182
183
|
- 0
|
183
|
-
hash:
|
184
|
+
hash: -2908557323860835992
|
184
185
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
185
186
|
none: false
|
186
187
|
requirements:
|
data/README.rdoc
DELETED
@@ -1,21 +0,0 @@
|
|
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
|
-
|