cistern 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -8,6 +8,7 @@ group :test do
8
8
  gem "guard-rspec"
9
9
  gem "rake"
10
10
  gem 'rb-fsevent', '~> 0.9.1'
11
+ gem 'pry-nav'
11
12
  end
12
13
 
13
14
  group :formatters do
data/TODO.md ADDED
@@ -0,0 +1,2 @@
1
+ ## Cistern::Model
2
+ * #update would be helpful
@@ -161,13 +161,7 @@ module Cistern::Attributes
161
161
  protected
162
162
 
163
163
  def missing_attributes(args)
164
- missing = []
165
- for arg in [:connection] | args
166
- unless send("#{arg}") || attributes.has_key?(arg)
167
- missing << arg
168
- end
169
- end
170
- missing
164
+ ([:connection] | args).select{|arg| send("#{arg}").nil?}
171
165
  end
172
166
  end
173
167
  end
@@ -4,9 +4,7 @@ class Cistern::Collection < Array
4
4
 
5
5
  %w[reject select slice].each do |method|
6
6
  define_method(method) do |*args, &block|
7
- unless @loaded
8
- lazy_load
9
- end
7
+ lazy_load unless @loaded
10
8
  data = super(*args, &block)
11
9
  self.clone.clear.concat(data)
12
10
  end
@@ -14,9 +12,7 @@ class Cistern::Collection < Array
14
12
 
15
13
  %w[first last].each do |method|
16
14
  define_method(method) do
17
- unless @loaded
18
- lazy_load
19
- end
15
+ lazy_load unless @loaded
20
16
  super()
21
17
  end
22
18
  end
@@ -35,8 +31,7 @@ class Cistern::Collection < Array
35
31
  end
36
32
 
37
33
  def create(attributes={})
38
- model = self.new(attributes)
39
- model.save
34
+ self.new(attributes).save
40
35
  end
41
36
 
42
37
  def get(identity)
@@ -59,10 +54,10 @@ class Cistern::Collection < Array
59
54
  raise(ArgumentError.new("Initialization parameters must be an attributes hash, got #{attributes.class} #{attributes.inspect}"))
60
55
  end
61
56
  model.new(
62
- attributes.merge(
57
+ {
63
58
  :collection => self,
64
- :connection => connection
65
- )
59
+ :connection => connection,
60
+ }.merge(attributes)
66
61
  )
67
62
  end
68
63
 
@@ -80,6 +75,11 @@ class Cistern::Collection < Array
80
75
  self
81
76
  end
82
77
 
78
+ def inspect
79
+ lazy_load unless @loaded
80
+ Cistern.formatter.call(self)
81
+ end
82
+
83
83
  private
84
84
 
85
85
  def lazy_load
File without changes
@@ -2,6 +2,14 @@ require 'formatador'
2
2
 
3
3
  module Cistern::Formatter::Formatador
4
4
  def self.call(model)
5
+ case model
6
+ when Cistern::Collection then collection_inspect(model)
7
+ when Cistern::Model then model_inspect(model)
8
+ else model.inspect
9
+ end
10
+ end
11
+
12
+ def self.model_inspect(model)
5
13
  Thread.current[:formatador] ||= Formatador.new
6
14
  data = "#{Thread.current[:formatador].indentation}<#{model.class.name}"
7
15
  Thread.current[:formatador].indent do
@@ -14,20 +22,20 @@ module Cistern::Formatter::Formatador
14
22
  data
15
23
  end
16
24
 
17
- def inspect
25
+ def self.collection_inspect(collection)
18
26
  Thread.current[:formatador] ||= Formatador.new
19
- data = "#{Thread.current[:formatador].indentation}<#{self.class.name}\n"
27
+ data = "#{Thread.current[:formatador].indentation}<#{collection.class.name}\n"
20
28
  Thread.current[:formatador].indent do
21
- unless self.class.attributes.empty?
29
+ unless collection.class.attributes.empty?
22
30
  data << "#{Thread.current[:formatador].indentation}"
23
- data << self.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
31
+ data << collection.class.attributes.map {|attribute| "#{attribute}=#{send(attribute).inspect}"}.join(",\n#{Thread.current[:formatador].indentation}")
24
32
  data << "\n"
25
33
  end
26
34
  data << "#{Thread.current[:formatador].indentation}["
27
- unless self.empty?
35
+ unless collection.empty?
28
36
  data << "\n"
29
37
  Thread.current[:formatador].indent do
30
- data << self.map {|member| member.inspect}.join(",\n")
38
+ data << collection.map {|member| member.inspect}.join(",\n")
31
39
  data << "\n"
32
40
  end
33
41
  data << Thread.current[:formatador].indentation
@@ -1,5 +1,5 @@
1
1
  module Cistern::Formatter
2
- autoload :AwesomePrint, 'cistern/formatters/awesome_print'
3
- autoload :Default, 'cistern/formatters/default'
4
- autoload :Formatador, 'cistern/formatters/formatador'
2
+ autoload :AwesomePrint, 'cistern/formatter/awesome_print'
3
+ autoload :Default, 'cistern/formatter/default'
4
+ autoload :Formatador, 'cistern/formatter/formatador'
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module Cistern
2
- VERSION = "0.1.4"
2
+ VERSION = "0.2.0"
3
3
  end
data/spec/cistern_spec.rb CHANGED
@@ -1,34 +1,64 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "inspection engine" do
3
+ describe "#inspect" do
4
4
  class Inspector < Cistern::Model
5
5
  identity :id
6
6
  attribute :name
7
7
  end
8
+ class Inspectors < Cistern::Collection
9
+ model Inspector
8
10
 
9
- after(:all) do
10
- Cistern.formatter= Cistern::Formatter::Default
11
+ def all
12
+ self.load([{id: 1, name: "2"},{id: 3, name: "4"}])
13
+ end
11
14
  end
12
15
 
13
- it "should default to default formatter" do
14
- Cistern.formatter.should == Cistern::Formatter::Default
16
+ before(:all) do
17
+ Cistern.formatter = Cistern::Formatter::Default
15
18
  end
16
19
 
17
- it "should use default" do
18
- Inspector.new(id: 1, name: "name").inspect.should match /#<Inspector:0x[0-9a-f]+ attributes={id:1,name:\"name\"}/
19
- end
20
+ describe "Cistern::Model" do
21
+ it "should use default" do
22
+ Cistern.formatter = Cistern::Formatter::Default
20
23
 
21
- it "should use awesome_print" do
22
- defined?(AwesomePrint).should be_false # don't require if not used
23
- Cistern.formatter= Cistern::Formatter::AwesomePrint
24
+ Inspector.new(id: 1, name: "name").inspect.should match /#<Inspector:0x[0-9a-f]+ attributes={id:1,name:\"name\"}/
25
+ end
24
26
 
25
- Inspector.new(id: 1, name: "name").inspect.match /(?x-mi:\#<Inspector:0x[0-9a-f]+>\ {\n\ \ \ \ \ \ :id\x1B\[0;37m\ =>\ \x1B\[0m\x1B\[1;34m1\x1B\[0m,\n\ \ \ \ :name\x1B\[0;37m\ =>\ \x1B\[0m\x1B\[0;33m"name"\x1B\[0m\n})/
26
- end
27
+ it "should use awesome_print" do
28
+ Cistern.formatter = Cistern::Formatter::AwesomePrint
29
+
30
+ Inspector.new(id: 1, name: "name").inspect.match /(?x-mi:\#<Inspector:0x[0-9a-f]+>\ {\n\ \ \ \ \ \ :id\x1B\[0;37m\ =>\ \x1B\[0m\x1B\[1;34m1\x1B\[0m,\n\ \ \ \ :name\x1B\[0;37m\ =>\ \x1B\[0m\x1B\[0;33m"name"\x1B\[0m\n})/
31
+ end
27
32
 
28
- it "should use formatador" do
29
- defined?(Formatador).should be_false # don't require if not used
30
- Cistern.formatter= Cistern::Formatter::Formatador
33
+ it "should use formatador" do
34
+ Cistern.formatter = Cistern::Formatter::Formatador
35
+
36
+ Inspector.new(id: 1, name: "name").inspect.should == %q{ <Inspector
37
+ id=1,
38
+ name="name"
39
+ >}
40
+ end
41
+ end
31
42
 
32
- Inspector.new(id: 1, name: "name").inspect.should == " <Inspector\n id=1,\n name=\"name\"\n >"
43
+ describe "Cistern::Collection" do
44
+ it "should use default" do
45
+ Inspectors.new.all.inspect.should == %q{ <Inspectors
46
+ [
47
+ <Inspector
48
+ id=1,
49
+ name="2"
50
+ >,
51
+ <Inspector
52
+ id=3,
53
+ name="4"
54
+ >
55
+ ]
56
+ >}
57
+ end
58
+ it "should use awesome_print" do
59
+ Cistern.formatter = Cistern::Formatter::AwesomePrint
60
+ Inspectors.new.all.inspect.should match(/^\[.*\]$/m) # close enough
61
+ end
62
+ it "should use formatador"
33
63
  end
34
64
  end
data/spec/model_spec.rb CHANGED
@@ -30,6 +30,10 @@ describe "Cistern::Model" do
30
30
  attribute :floater, type: :float
31
31
  attribute :butternut, type: :integer, aliases: "squash", squash: "id"
32
32
  attribute :custom, parser: lambda{|v, opts| "X!#{v}"}
33
+
34
+ def save
35
+ requires :flag
36
+ end
33
37
  end
34
38
 
35
39
  it "should parse string" do
@@ -75,5 +79,15 @@ describe "Cistern::Model" do
75
79
  it "should slice out unaccounted for attributes" do
76
80
  TypeSpec.new({"something" => {"id" => "12"}}).attributes.keys.should_not include("something")
77
81
  end
82
+
83
+ describe "#requires" do
84
+ it "should raise if attribute not provided" do
85
+ lambda { TypeSpec.new({"connection" => "fake", "something" => {"id" => "12"}}).save }.should raise_exception(ArgumentError)
86
+ end
87
+
88
+ it "should raise if attribute is provided and is nil" do
89
+ lambda { TypeSpec.new({"connection" => "fake", "custom" => nil}).save }.should raise_exception(ArgumentError)
90
+ end
91
+ end
78
92
  end
79
93
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cistern
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-30 00:00:00.000000000 Z
12
+ date: 2013-01-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: API client framework extracted from Fog
15
15
  email:
@@ -24,14 +24,15 @@ files:
24
24
  - LICENSE
25
25
  - README.md
26
26
  - Rakefile
27
+ - TODO.md
27
28
  - cistern.gemspec
28
29
  - lib/cistern.rb
29
30
  - lib/cistern/attributes.rb
30
31
  - lib/cistern/collection.rb
31
32
  - lib/cistern/formatter.rb
32
- - lib/cistern/formatters/awesome_print.rb
33
- - lib/cistern/formatters/default.rb
34
- - lib/cistern/formatters/formatador.rb
33
+ - lib/cistern/formatter/awesome_print.rb
34
+ - lib/cistern/formatter/default.rb
35
+ - lib/cistern/formatter/formatador.rb
35
36
  - lib/cistern/hash.rb
36
37
  - lib/cistern/mock.rb
37
38
  - lib/cistern/model.rb
@@ -49,17 +50,17 @@ rdoc_options: []
49
50
  require_paths:
50
51
  - lib
51
52
  required_ruby_version: !ruby/object:Gem::Requirement
52
- none: false
53
53
  requirements:
54
54
  - - ! '>='
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
- required_rubygems_version: !ruby/object:Gem::Requirement
58
57
  none: false
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  requirements:
60
60
  - - ! '>='
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
+ none: false
63
64
  requirements: []
64
65
  rubyforge_project:
65
66
  rubygems_version: 1.8.24
@@ -71,3 +72,4 @@ test_files:
71
72
  - spec/collection_spec.rb
72
73
  - spec/model_spec.rb
73
74
  - spec/spec_helper.rb
75
+ has_rdoc: