faceted 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -18,10 +18,11 @@ Let's say that you have an ActiveRecord model called Musician, and you want to e
18
18
  presents :musician
19
19
  field :name
20
20
  field :genre
21
+ field :instrument, :default => 'guitar'
21
22
  end
22
23
  end
23
24
 
24
- That's actually all you have to do. The `presents` method maps your Musician presenter to a root-level class called `Musician`, and the `field` methods map to attributes *or* methods on the associated AR Musician instance.
25
+ That's actually all you have to do. The `presents` method maps your Musician presenter to a root-level class called `Musician`, and the `field` methods map to attributes *or* methods on the associated AR Musician instance. If a default is set for a field, that default value will be stored when the presenter is used to create a record, unless overridden.
25
26
 
26
27
  What's that, you say? How is the appropriate AR Musican record associated? Simple. Invoke an instance of the `MyApi::Musician` passing in an `:id` parameter, and it just works:
27
28
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.0.5
data/faceted.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "faceted"
8
- s.version = "1.0.4"
8
+ s.version = "1.0.5"
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"]
12
- s.date = "2012-10-12"
12
+ s.date = "2012-11-16"
13
13
  s.description = "Faceted provides set of tools, patterns, and modules for use in API implementations."
14
14
  s.email = "corey@trunkclub.com"
15
15
  s.extra_rdoc_files = [
@@ -22,14 +22,17 @@ module Faceted
22
22
  # Instance methods =======================================================
23
23
 
24
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? }
25
+ unless args.empty?
26
+ self.id = args[:id]
27
+ initialize_with_object
28
+ args.symbolize_keys.delete_if{|k,v| v.nil?}.each{|k,v| self.send("#{k}=", v) if self.respond_to?("#{k}=") && ! v.nil? }
29
+ end
28
30
  self.errors = []
29
31
  self.success = true
30
32
  end
31
33
 
32
34
  def reinitialize_with_object(obj)
35
+ obj.reload
33
36
  schema_fields.each{ |k| self.send("#{k}=", obj.send(k)) if obj.respond_to?(k) && self.send(:settable_field?, k) }
34
37
  end
35
38
 
@@ -54,7 +57,7 @@ module Faceted
54
57
 
55
58
  def initialize_with_object
56
59
  return unless object
57
- schema_fields.each{ |k| self.send("#{k}=", object.send(k)) if object.respond_to?(k) && self.respond_to?("#{k}=") }
60
+ schema_fields.each{|k| self.send("#{k}=", object.send(k)) if object.respond_to?(k) && self.respond_to?("#{k}=") }
58
61
  end
59
62
 
60
63
  def object
data/lib/faceted/model.rb CHANGED
@@ -29,7 +29,8 @@ module Faceted
29
29
  def field(name, args={})
30
30
  fields << name
31
31
  define_method :"#{name}" do
32
- instance_variable_get("@#{name}") || args[:default]
32
+ val = instance_variable_get("@#{name}")
33
+ val.nil? ? args[:default] : val
33
34
  end
34
35
  unless args[:read_only]
35
36
  define_method :"#{name}=" do |val|
@@ -1,15 +1,17 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  class Musician # pretend that this is an AR model
4
- attr_accessor :id, :name, :rating, :errors, :birthplace_id
4
+ attr_accessor :id, :name, :rating, :errors, :birthplace_id, :alive
5
5
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
6
- def attributes; {:id => self.id, :name => self.name, :rating => self.rating}; end
6
+ def attributes; {:id => self.id, :name => self.name, :rating => self.rating, :alive => self.alive}; end
7
+ def reload; self; end
7
8
  end
8
9
 
9
10
  class Birthplace # another make-believe AR model
10
11
  attr_accessor :id, :city, :state
11
12
  def initialize(params={}); params.each{|k,v| self.send("#{k}=",v) if self.respond_to?(k)}; end
12
13
  def attributes; {:id => self.id, :city => self.city, :state => self.state}; end
14
+ def reload; self; end
13
15
  end
14
16
 
15
17
  module MyApi
@@ -27,13 +29,14 @@ module MyApi
27
29
  field :name
28
30
  field :rating
29
31
  field :birthplace_id
32
+ field :alive
30
33
  end
31
34
 
32
35
  describe Musician do
33
36
 
34
37
  before do
35
38
 
36
- @ar_musician = ::Musician.new(:id => 1, :name => 'Johnny Cash', :rating => 'Good')
39
+ @ar_musician = ::Musician.new(:id => 1, :name => 'Johnny Cash', :rating => 'Good', :alive => false)
37
40
  ::Musician.stub(:find) { @ar_musician }
38
41
 
39
42
  @ar_birthplace = ::Birthplace.new(:id => 1, :city => 'Kingsland', :state => 'Arkansas')
@@ -43,9 +46,19 @@ module MyApi
43
46
 
44
47
  describe 'initialized with a presented object' do
45
48
 
46
- it 'inherits values from its AR counterpart' do
47
- musician = MyApi::Musician.new(:id => 1)
48
- musician.name.should == 'Johnny Cash'
49
+ describe 'inherits values from its AR counterpart' do
50
+
51
+ it 'normal values' do
52
+ musician = MyApi::Musician.new(:id => 1)
53
+ musician.name.should == 'Johnny Cash'
54
+ end
55
+
56
+ it 'boolean values' do
57
+ musician = MyApi::Musician.new(:id => 1)
58
+ musician.alive.should be_false
59
+ musician.alive.should_not be_nil
60
+ end
61
+
49
62
  end
50
63
 
51
64
  it 'overwrites values from its AR counterpart' do
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: 1.0.4
4
+ version: 1.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-12 00:00:00.000000000 Z
13
+ date: 2012-11-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -181,7 +181,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  segments:
183
183
  - 0
184
- hash: 1706870145372003928
184
+ hash: -3357536951405815517
185
185
  required_rubygems_version: !ruby/object:Gem::Requirement
186
186
  none: false
187
187
  requirements: