plain_old_model 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -20,27 +20,32 @@ Or install it yourself as:
20
20
  Example
21
21
  =======
22
22
 
23
- class Person < Activeservice::Base
24
- attr_accessor :name, :age, :book
23
+ class Person < PlainOldModel::Base
24
+ attr_accessor :name, :age, :book
25
25
 
26
- attr_reader :account_number
27
- attr_writer :address
26
+ attr_reader :account_number
27
+ attr_writer :address
28
28
 
29
- validates_presence_of :book
30
- end
29
+ validates_presence_of :book
30
+ end
31
31
 
32
- params = {"name" =>"testmeparams", "age" => "25", "book" =>["wewrwrwr", "werwrwrr"]}
32
+ params = {"name" =>"testmeparams", "age" => "25", "book" =>["wewrwrwr", "werwrwrr"]}
33
33
 
34
- params1 = {:name =>"testmeparams", :age => "25", :book => {:author =>"my name", :category => "fiction"}}
34
+ params1 = {:name =>"testmeparams", :age => "25", :book => {:author =>"my name", :category => "fiction"}}
35
35
 
36
- p = Person.new(params)
37
- p.book # ["wewrwrwr", "werwrwrr"]
36
+ p = Person.new(params)
37
+
38
+ p.book # ["wewrwrwr", "werwrwrr"]
38
39
 
39
- p.valid? #true
40
+ p.valid? #true
40
41
 
41
42
  OR
42
- p.assign_attributes(params11)
43
+
44
+ p = Person.new()
43
45
 
46
+ p.assign_attributes(params11)
47
+
48
+ =====================================================================
44
49
  p1 = Person.new(params1)
45
50
 
46
51
  p1.book # {:author =>"my name", :category => "fiction"}
data/Rakefile CHANGED
@@ -1,4 +1,6 @@
1
+ require 'bundler'
1
2
  require 'rspec/core/rake_task'
3
+ Bundler::GemHelper.install_tasks
2
4
 
3
5
  RSpec::Core::RakeTask.new('spec')
4
6
 
data/examples/book.rb CHANGED
@@ -1,4 +1,4 @@
1
- require_relative '../lib/activeservice/base'
1
+ require_relative '../lib/plain_old_model/base'
2
2
  class Book < PlainOldModel::Base
3
3
 
4
4
  attr_accessor :author, :category
data/examples/person.rb CHANGED
@@ -6,7 +6,7 @@ class Person < PlainOldModel::Base
6
6
  attr_reader :test
7
7
  attr_writer :testmewriter
8
8
 
9
- associated_class :book, :class_name => :book
9
+ has_one :book, :class_name => :book
10
10
 
11
11
  validates_presence_of :name ,:message => "The field is a required field"
12
12
  end
@@ -18,7 +18,6 @@ module PlainOldModel
18
18
  include ActiveModel::Serializers::JSON
19
19
  include ActiveModel::MassAssignmentSecurity
20
20
 
21
- # refactor this later to single method to gather all attributes
22
21
  def self.attr_accessor(*attrs)
23
22
  gather_attributes(attrs)
24
23
  super(*attrs)
@@ -43,36 +42,41 @@ module PlainOldModel
43
42
  end
44
43
 
45
44
  def initialize(attributes = nil, options = {})
46
- options = options[:new_record]= true
47
45
  assign_attributes(attributes, options) if attributes
48
46
  end
49
47
 
50
- #creation attributes ->create the class referred , and assign the variables
51
-
52
- def self.associated_class(klass, options={})
53
- @association = {}
48
+ def self.has_one(klass, options={})
54
49
  if options[:class_name]
55
- @association[klass] = options[:class_name].to_s.capitalize unless options[:class_name].nil?
50
+ association[klass] = options[:class_name].to_s.capitalize unless options[:class_name].nil?
56
51
  else
57
- @association[klass] = klass.to_s.capitalize
52
+ association[klass] = klass.to_s.capitalize
58
53
  end
59
- @association
60
54
  end
61
55
 
62
56
  def self.association
63
- @association
57
+ @association ||= {}
64
58
  end
65
59
 
66
60
  def assign_attributes(new_attributes, options = {})
67
61
  return unless new_attributes
62
+ association.each do |k,v|
63
+ if new_attributes.include?(k)
64
+ new_klass = k.to_s.capitalize.constantize.new(new_attributes[k])
65
+ send("#{k}=", new_klass)
66
+ new_attributes = new_attributes.delete_if {|key, value| key == k }
67
+ end
68
+ end
69
+ assignment(new_attributes, options)
70
+ end
68
71
 
69
- attributes = sanitize_attributes(new_attributes).stringify_keys
72
+ def assignment(attributes, options)
73
+ attributes = sanitize_attributes(attributes).stringify_keys
70
74
  multi_parameter_attributes = []
71
75
  @mass_assignment_options = options
72
76
 
73
77
  attributes.each do |k, v|
74
78
  if k.include?("(")
75
- multi_parameter_attributes << [ k, v ]
79
+ multi_parameter_attributes << [k, v]
76
80
  elsif respond_to?("#{k}=")
77
81
  send("#{k}=", v)
78
82
  else
@@ -99,6 +103,10 @@ module PlainOldModel
99
103
  self.class.attributes
100
104
  end
101
105
 
106
+ def association
107
+ self.class.association
108
+ end
109
+
102
110
  def mass_assignment_options
103
111
  @mass_assignment_options ||= {}
104
112
  end
@@ -1,3 +1,3 @@
1
1
  module PlainOldModel
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -7,7 +7,7 @@ Gem::Specification.new do |gem|
7
7
  gem.name = "plain_old_model"
8
8
  gem.version = PlainOldModel::VERSION
9
9
  gem.authors = ["Bhaskar Sundarraj"]
10
- gem.email = ["bskar.sundarraj@gmail.com"]
10
+ gem.email = ["bhaskar.sundarraj@gmail.com"]
11
11
  gem.description = %q{This gem is created to cater the projects which do not require a backend/database,
12
12
  but still need all the niceties offered by the ActiveModel}
13
13
  gem.summary = %q{This gem is created to cater the projects which do not require a backend/database,
@@ -15,23 +15,22 @@ describe PlainOldModel::Base do
15
15
  @person.attributes.count.should == 3
16
16
  @person.attributes.should == [:fname, :lname, :address]
17
17
  @address = Address.new
18
- @address.attributes.should == [:fname, :lname, :country, :read_test, :write_test, :me]
18
+ @address.attributes.should == [:fname, :lname, :country, :read_test, :write_test]
19
19
  end
20
20
  it "should accept the params and new up a class with variables initialized" do
21
- @person= Person.new({:fname => "first value", :lname => "second value"})
21
+ @person= Person.new({:fname => "first value", :lname => "second value", :address => {:fname => 'fname', :lname => 'lname'}})
22
22
  @person.fname.should == "first value"
23
23
  @person.lname.should == "second value"
24
+ @person.address.should == {:fname => 'fname', :lname => 'lname'}
24
25
  end
25
26
  it "should not assign value to the attr_reader attributes/ read only attribute" do
26
27
  @address = Address.new
27
- @address.assign_attributes({:fname => "first value", :lname => "second value", :country => 'India', :read_test => 'This should not be assigned'})
28
- @address.country.should == 'India'
28
+ @address.assign_attributes({:fname => "first value", :lname => "second value", :read_test => 'This should not be assigned'})
29
29
  @address.read_test.should == nil
30
30
  end
31
31
  it "should assign value to the attr_writer attributes" do
32
32
  @address = Address.new
33
- @address.assign_attributes({:fname => "first value", :lname => "second value", :country => 'India', :read_test => 'This should not be assigned',:write_test => "this shd be available"})
34
- @address.country.should == 'India'
33
+ @address.assign_attributes({:fname => "first value", :lname => "second value", :read_test => 'This should not be assigned',:write_test => "this shd be available"})
35
34
  @address.instance_variable_get(:@write_test).should == "this shd be available"
36
35
  end
37
36
  it "should assign_attributes to the class" do
@@ -45,17 +44,36 @@ describe PlainOldModel::Base do
45
44
  @person.valid?.should == true
46
45
  end
47
46
  it "should allow the class to use activemodel validations and errors" do
48
- @address = Address.new
49
- @address.assign_attributes({:fname => "first value", :lname => "second value"})
50
- @address.valid?.should == false
51
- @address.errors.should_not == nil
47
+ @person= Person.new({:lname => "second value"})
48
+ @person.valid?.should == false
49
+ @person.errors.should_not == nil
52
50
  end
53
51
  end
54
52
 
55
53
  describe "association" do
56
- #it "should create a new instance and assign_attributes to the has_one class" do
57
- # @person = Person.new({:fname => "first value", :lname => "second value",:address => {:fname => "test", :lname =>"testme",:country => "india"}})
58
- #end
54
+ it "should return empty hash for unassociated class" do
55
+ @person = Person.new()
56
+ @person.association.should == {}
57
+ end
58
+ it "should provide all the associations when the class has associations" do
59
+ @address = Address.new()
60
+ @address.association.should == {:country => "Country"}
61
+ end
62
+ it "should create a new instance and assign_attributes to the associated class" do
63
+ @address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India"}, :read_test => 'This should not be assigned',:write_test => "this shd be available"})
64
+ @address.country.class.should == Country
65
+ end
66
+ it "should create the nested class instance and assign_attributes to the associated nested class" do
67
+ @address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India", :continent => {:name => "asia"}}, :read_test => 'This should not be assigned',:write_test => "this shd be available"})
68
+ @address.country.continent.class.should == Continent
69
+ end
70
+ it "should create a new instance and assign_attributes to the associated class" do
71
+ @address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India", :continent => {:name => "asia", :desc => {:this => "is a test", :actual_desc => "is another test"}}}, :read_test => 'This should not be assigned',:write_test => "this shd be available"})
72
+ @address.country.continent.class.should == Continent
73
+ @address.country.continent.name.should == "asia"
74
+ @continent = @address.country.continent
75
+ @continent.name.should == "asia"
76
+ end
59
77
  end
60
78
  describe "usage of activemodel classes " do
61
79
  #it "should allow model's naming properties" do
@@ -70,15 +88,26 @@ end
70
88
 
71
89
  class Person < PlainOldModel::Base
72
90
  attr_accessor :fname, :lname, :address
73
-
91
+ #has_one :address
92
+ validates_presence_of :fname
74
93
  end
75
94
 
76
95
  class Address < PlainOldModel::Base
77
96
  attr_accessor :fname, :lname, :country
78
97
  attr_reader :read_test
79
- attr_writer :write_test, :me
98
+ attr_writer :write_test
99
+
100
+ has_one :country
101
+
102
+ end
80
103
 
81
- #belongs_to :person
82
- validates_presence_of :country
104
+ class Country < PlainOldModel::Base
105
+ attr_accessor :code, :name, :continent
83
106
 
107
+ has_one :continent
84
108
  end
109
+
110
+ class Continent < PlainOldModel::Base
111
+ attr_accessor :name, :desc
112
+ end
113
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plain_old_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-02-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70337828581720 !ruby/object:Gem::Requirement
16
+ requirement: &70167586077720 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70337828581720
24
+ version_requirements: *70167586077720
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &70337828581240 !ruby/object:Gem::Requirement
27
+ requirement: &70167586077280 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70337828581240
35
+ version_requirements: *70167586077280
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70337828580820 !ruby/object:Gem::Requirement
38
+ requirement: &70167586076860 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70337828580820
46
+ version_requirements: *70167586076860
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70337828580400 !ruby/object:Gem::Requirement
49
+ requirement: &70167586076440 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70337828580400
57
+ version_requirements: *70167586076440
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rspec-core
60
- requirement: &70337828579980 !ruby/object:Gem::Requirement
60
+ requirement: &70167586076020 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,11 +65,11 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70337828579980
68
+ version_requirements: *70167586076020
69
69
  description: ! "This gem is created to cater the projects which do not require a backend/database,\n
70
70
  \ but still need all the niceties offered by the ActiveModel"
71
71
  email:
72
- - bskar.sundarraj@gmail.com
72
+ - bhaskar.sundarraj@gmail.com
73
73
  executables: []
74
74
  extensions: []
75
75
  extra_rdoc_files: []