Skip to content
Browse files

Code cleanup, Vim marks, etc.

  • Loading branch information...
1 parent ba5bbd4 commit 2b55d1ca593dcb2506076936d529eb1050b543d6 @remiprev committed Aug 27, 2012
View
2 lib/her/model.rb
@@ -29,10 +29,8 @@ module Model
included do
extend Her::Model::Base
extend Her::Model::HTTP
- extend Her::Model::ORM::ClassMethods
extend Her::Model::Relationships
extend Her::Model::Hooks
- extend Her::Model::Paths::ClassMethods
# Define default settings
base_path = self.name.split("::").last.underscore.pluralize
View
1 lib/her/model/hooks.rb
@@ -71,7 +71,6 @@ def hooks # {{{
end # }}}
private
-
# @private
def set_hook(time, name, action) # {{{
(self.hooks["#{time}_#{name}".to_sym] ||= []) << action
View
52 lib/her/model/introspection.rb
@@ -2,32 +2,6 @@ module Her
module Model
module Introspection
extend ActiveSupport::Concern
-
- module ClassMethods
- # Finds a class at the same level as this one or at the global level.
- def nearby_class(name)
- sibling_class(name) || name.constantize rescue nil
- end
-
- protected
- # Looks for a class at the same level as this one with the given name.
- # @private
- def sibling_class(name)
- if mod = self.containing_module
- "#{mod.name}::#{name}".constantize rescue nil
- else
- name.constantize rescue nil
- end
- end
-
- # If available, returns the containing Module for this class.
- # @private
- def containing_module # {{{
- return unless self.name =~ /::/
- self.name.split("::")[0..-2].join("::").constantize
- end # }}}
- end
-
# Inspect an element, returns it for introspection.
#
# @example
@@ -52,6 +26,32 @@ def attribute_for_inspect(value) # {{{
value.inspect
end
end # }}}
+
+ module ClassMethods
+ # Finds a class at the same level as this one or at the global level.
+ # @private
+ def nearby_class(name) # {{{
+ sibling_class(name) || name.constantize rescue nil
+ end # }}}
+
+ protected
+ # Looks for a class at the same level as this one with the given name.
+ # @private
+ def sibling_class(name) # {{{
+ if mod = self.containing_module
+ "#{mod.name}::#{name}".constantize rescue nil
+ else
+ name.constantize rescue nil
+ end
+ end # }}}
+
+ # If available, returns the containing Module for this class.
+ # @private
+ def containing_module # {{{
+ return unless self.name =~ /::/
+ self.name.split("::")[0..-2].join("::").constantize
+ end # }}}
+ end
end
end
end
View
1 lib/her/model/orm.rb
@@ -2,6 +2,7 @@ module Her
module Model
# This module adds ORM-like capabilities to the model
module ORM
+ extend ActiveSupport::Concern
attr_accessor :data, :metadata, :errors
# Initialize a new object with data received from an HTTP request
View
1 lib/her/model/paths.rb
@@ -1,6 +1,7 @@
module Her
module Model
module Paths
+ extend ActiveSupport::Concern
# Return a path based on the collection path and a resource data
#
# @example
View
2 lib/her/model/relationships.rb
@@ -131,7 +131,7 @@ def belongs_to(name, attrs={}) # {{{
klass = self.class.nearby_class(attrs[:class_name])
@data[name] ||= klass.get_resource("#{klass.build_request_path(:id => @data[attrs[:foreign_key].to_sym])}")
end
- end
+ end # }}}
# @private
def relationship_accessor(type, attrs) # {{{
View
4 spec/middleware/accept_json_spec.rb
@@ -2,9 +2,9 @@
require "spec_helper"
describe Her::Middleware::AcceptJSON do
- it "adds an Accept header" do
+ it "adds an Accept header" do # {{{
described_class.new.add_header({}).tap do |headers|
headers["Accept"].should == "application/json"
end
- end
+ end # }}}
end
View
8 spec/middleware/first_level_parse_json_spec.rb
@@ -5,21 +5,21 @@
subject { described_class.new }
let(:body) { "{\"id\": 1, \"name\": \"Tobias Fünke\", \"errors\": 2, \"metadata\": 3}" }
- it "parses body as json" do
+ it "parses body as json" do # {{{
subject.parse(body).tap do |json|
json[:data].should == { :id => 1, :name => "Tobias Fünke" }
json[:errors].should == 2
json[:metadata].should == 3
end
- end
+ end # }}}
- it "parses :body key as json in the env hash" do
+ it "parses :body key as json in the env hash" do # {{{
env = { :body => body }
subject.on_complete(env)
env[:body].tap do |json|
json[:data].should == { :id => 1, :name => "Tobias Fünke" }
json[:errors].should == 2
json[:metadata].should == 3
end
- end
+ end # }}}
end
View
8 spec/middleware/second_level_parse_json_spec.rb
@@ -5,21 +5,21 @@
subject { described_class.new }
let(:body) { "{\"data\": 1, \"errors\": 2, \"metadata\": 3}" }
- it "parses body as json" do
+ it "parses body as json" do # {{{
subject.parse(body).tap do |json|
json[:data].should == 1
json[:errors].should == 2
json[:metadata].should == 3
end
- end
+ end # }}}
- it "parses :body key as json in the env hash" do
+ it "parses :body key as json in the env hash" do # {{{
env = { :body => body }
subject.on_complete(env)
env[:body].tap do |json|
json[:data].should == 1
json[:errors].should == 2
json[:metadata].should == 3
end
- end
+ end # }}}
end
View
7 spec/model/hooks_spec.rb
@@ -108,17 +108,14 @@
end
context "inheriting hooks from a superclass" do
- before do
- end
-
- it "copies hooks to the subclass" do
+ it "copies hooks to the subclass" do # {{{
Foo::User.before_save :set_internal_id
Foo::User.after_create { |record| record.internal_id = 42 }
subclass = Class.new(Foo::User)
subclass.hooks.object_id.should_not == Foo::User.hooks.object_id
subclass.hooks[:before_save].should == [:set_internal_id]
subclass.hooks[:after_create].length.should == 1
- end
+ end # }}}
end
end
View
8 spec/model/http_spec.rb
@@ -65,7 +65,7 @@
Foo::Comment.her_api.base_uri.should == "https://api2.example.com"
end # }}}
- it "binds a a model to it's superclass' her_api" do
+ it "binds a a model to it's superclass' her_api" do # {{{
api = Her::API.new
api.setup :url => "http://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -78,9 +78,9 @@
Foo::Subclass = Class.new(Foo::Superclass)
Foo::Subclass.her_api.should == Foo::Superclass.her_api
- end
+ end # }}}
- it "allows subclasses to change her_api without changing the parent class' her_api" do
+ it "allows subclasses to change her_api without changing the parent class' her_api" do # {{{
api1 = Her::API.new
api1.setup :url => "http://api.example.com" do |builder|
builder.use Her::Middleware::FirstLevelParseJSON
@@ -102,7 +102,7 @@
end
Foo::Subclass.her_api.should_not == Foo::Superclass.her_api
- end
+ end # }}}
end
context "making HTTP requests" do
View
12 spec/model/introspection_spec.rb
@@ -32,20 +32,20 @@
end
describe "#nearby_class" do
- context "for a class inside of a module" do
- before do
+ context "for a class inside of a module" do # {{{
+ before do # {{{
spawn_model "Foo::User"
spawn_model "Foo::AccessRecord"
spawn_model "AccessRecord"
spawn_model "Log"
- end
+ end # }}}
- it "returns a sibling class, if found" do
+ it "returns a sibling class, if found" do # {{{
Foo::User.nearby_class("AccessRecord").should == Foo::AccessRecord
AccessRecord.nearby_class("Log").should == Log
Foo::User.nearby_class("Log").should == Log
Foo::User.nearby_class("X").should be_nil
- end
- end
+ end # }}}
+ end # }}}
end
end
View
7 spec/model/relationships_spec.rb
@@ -62,16 +62,13 @@
end # }}}
context "inheriting relationships from a superclass" do
- before do
- end
-
- it "copies relationships to the subclass" do
+ it "copies relationships to the subclass" do # {{{
Foo::User.has_many :comments, :class_name => "Post"
subclass = Class.new(Foo::User)
subclass.relationships.object_id.should_not == Foo::User.relationships.object_id
subclass.relationships[:has_many].length.should == 1
subclass.relationships[:has_many].first[:class_name].should == "Post"
- end
+ end # }}}
end
end

0 comments on commit 2b55d1c

Please sign in to comment.
Something went wrong with that request. Please try again.