plain_old_model 0.0.4 → 0.1.0

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.
@@ -1,4 +1,5 @@
1
1
  require "plain_old_model/version"
2
+ require 'plain_old_model/base'
2
3
 
3
4
  module PlainOldModel
4
5
  # Your code goes here...
@@ -0,0 +1,66 @@
1
+ require 'active_support/all'
2
+
3
+ module PlainOldModel
4
+ module AttributeAssignment
5
+ module ClassMethods
6
+ def has_one(attr_name, options={})
7
+ if options[:class_name]
8
+ associations[attr_name] = options[:class_name].to_s.capitalize unless options[:class_name].nil?
9
+ else
10
+ associations[attr_name] = attr_name.to_s.capitalize
11
+ end
12
+ attr_accessor attr_name
13
+ end
14
+
15
+ def associations
16
+ @associations ||= {}
17
+ end
18
+ end
19
+
20
+ def self.included(klass)
21
+ klass.extend ClassMethods
22
+ end
23
+
24
+ def assign_attributes(new_attributes, options = {})
25
+ return unless new_attributes
26
+ associations.each do |k, v|
27
+ if new_attributes.include?(k)
28
+ new_klass = k.to_s.camelcase.constantize.new(new_attributes[k])
29
+
30
+ respond_to?("#{k}=") ? (send("#{k}=", new_klass)) : (instance_variable_set("@#{k}".to_sym, new_klass))
31
+ new_attributes = new_attributes.delete_if { |key, value| key == k }
32
+ end
33
+ end
34
+ assignment(new_attributes, options)
35
+ end
36
+
37
+ def associations
38
+ self.class.associations
39
+ end
40
+
41
+ private
42
+ def sanitize_attributes(attributes)
43
+ sanitized_attributes = {}
44
+ attributes.each do |k, v|
45
+ if respond_to?("#{k}=") || respond_to?("#{k}")
46
+ sanitized_attributes[k]=v
47
+ end
48
+ end
49
+ sanitized_attributes
50
+ end
51
+
52
+ def assignment(attributes, options)
53
+ attributes = sanitize_attributes(attributes).stringify_keys
54
+
55
+ attributes.each do |k, v|
56
+ if respond_to?("#{k}=")
57
+ send("#{k}=", v)
58
+ elsif respond_to?("#{k}")
59
+ instance_variable_set("@#{k}".to_sym, v)
60
+ else
61
+ raise(Exception)
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -1,120 +1,25 @@
1
- require 'active_support/all'
2
- require 'active_model/validations'
3
1
  require 'active_model/naming'
4
2
  require 'active_model/translation'
3
+ require 'active_model/validations'
5
4
  require 'active_model/conversion'
6
- require 'active_model/attribute_methods'
7
- require 'active_model/serialization'
8
- require 'active_model/serializers/json'
9
- require 'active_model/mass_assignment_security'
10
- require 'active_support/inflector'
5
+ require 'plain_old_model/attribute_assignment'
6
+
11
7
  module PlainOldModel
12
8
  class Base
13
9
  extend ActiveModel::Naming
14
10
  include ActiveModel::Translation
15
11
  include ActiveModel::Validations
16
12
  include ActiveModel::Conversion
17
- include ActiveModel::AttributeMethods
18
- include ActiveModel::Serializers::JSON
19
- include ActiveModel::MassAssignmentSecurity
20
-
21
- def self.attr_accessor(*attrs)
22
- gather_attributes(attrs)
23
- super(*attrs)
24
- end
25
-
26
- def self.attr_writer(*attrs)
27
- gather_attributes(attrs)
28
- super(*attrs)
29
- end
30
-
31
- def self.attr_reader(*attrs)
32
- gather_attributes(attrs)
33
- super(*attrs)
34
- end
35
-
36
- def self.attributes
37
- @attributes ||= []
38
- end
39
-
40
- def self.gather_attributes(attrs)
41
- attributes.concat attrs
42
- end
13
+ include PlainOldModel::AttributeAssignment
43
14
 
44
15
  def initialize(attributes = nil, options = {})
45
16
  assign_attributes(attributes, options) if attributes
46
17
  end
47
18
 
48
- def self.has_one(klass, options={})
49
- if options[:class_name]
50
- association[klass] = options[:class_name].to_s.capitalize unless options[:class_name].nil?
51
- else
52
- association[klass] = klass.to_s.capitalize
53
- end
54
- end
55
-
56
- def self.association
57
- @association ||= {}
58
- end
59
-
60
- def assign_attributes(new_attributes, options = {})
61
- return unless new_attributes
62
- association.each do |k,v|
63
- if new_attributes.include?(k)
64
- new_klass = k.to_s.camelcase.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
71
-
72
- def assignment(attributes, options)
73
- attributes = sanitize_attributes(attributes).stringify_keys
74
- multi_parameter_attributes = []
75
- @mass_assignment_options = options
76
-
77
- attributes.each do |k, v|
78
- if k.include?("(")
79
- multi_parameter_attributes << [k, v]
80
- elsif respond_to?("#{k}=")
81
- send("#{k}=", v)
82
- else
83
- raise(Exception)
84
- end
85
- end
86
- end
87
-
88
- def sanitize_attributes(attributes)
89
- sanitized_attributes = {}
90
- attributes.each do |k,v|
91
- if respond_to?("#{k}=")
92
- sanitized_attributes[k]=v
93
- end
94
- end
95
- sanitized_attributes
96
- end
97
-
98
19
  def persisted?
99
20
  false
100
21
  end
101
22
 
102
- def attributes
103
- self.class.attributes
104
- end
105
-
106
- def association
107
- self.class.association
108
- end
109
-
110
- def mass_assignment_options
111
- @mass_assignment_options ||= {}
112
- end
113
-
114
- def mass_assignment_role
115
- mass_assignment_options || :default
116
- end
117
-
118
23
  end
119
24
 
120
25
  end
@@ -1,3 +1,3 @@
1
1
  module PlainOldModel
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -2,31 +2,17 @@ require 'spec_helper'
2
2
 
3
3
  describe PlainOldModel::Base do
4
4
 
5
-
6
- describe "gem version" do
7
- it "should return the current version of the gem" do
8
- PlainOldModel::VERSION.should == '0.0.4'
9
- end
10
- end
11
-
12
5
  describe " assign_attribute and new" do
13
- it "should return all the attributes of the class instance even if they are not initialized" do
14
- @person = Person.new
15
- @person.attributes.count.should == 3
16
- @person.attributes.should == [:fname, :lname, :address]
17
- @address = Address.new
18
- @address.attributes.should == [:fname, :lname, :country, :read_test, :write_test]
19
- end
20
6
  it "should accept the params and new up a class with variables initialized" do
21
7
  @person= Person.new({:fname => "first value", :lname => "second value", :address => {:fname => 'fname', :lname => 'lname'}})
22
8
  @person.fname.should == "first value"
23
9
  @person.lname.should == "second value"
24
10
  @person.address.should == {:fname => 'fname', :lname => 'lname'}
25
11
  end
26
- it "should not assign value to the attr_reader attributes/ read only attribute" do
12
+ it "should assign value to the attr_reader attributes/ read only attribute" do
27
13
  @address = Address.new
28
- @address.assign_attributes({:fname => "first value", :lname => "second value", :read_test => 'This should not be assigned'})
29
- @address.read_test.should == nil
14
+ @address.assign_attributes({:fname => "first value", :lname => "second value", :read_test => 'This should be assigned'})
15
+ @address.read_test.should == "This should be assigned"
30
16
  end
31
17
  it "should assign value to the attr_writer attributes" do
32
18
  @address = Address.new
@@ -50,14 +36,14 @@ describe PlainOldModel::Base do
50
36
  end
51
37
  end
52
38
 
53
- describe "association" do
39
+ describe "associations" do
54
40
  it "should return empty hash for unassociated class" do
55
41
  @person = Person.new()
56
- @person.association.should == {}
42
+ @person.associations.should == {}
57
43
  end
58
44
  it "should provide all the associations when the class has associations" do
59
45
  @address = Address.new()
60
- @address.association.should == {:country => "Country"}
46
+ @address.associations.should == {:country => "Country"}
61
47
  end
62
48
  it "should create a new instance and assign_attributes to the associated class" do
63
49
  @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"})
@@ -74,14 +60,12 @@ describe PlainOldModel::Base do
74
60
  @continent = @address.country.continent
75
61
  @continent.name.should == "asia"
76
62
  end
63
+ it "should assign values to the read only attributes" do
64
+ @address = Address.new({:fname => "first value", :lname => "second value", :country => {:code => "In", :name => "India"}, :read_test => 'This should be assigned',:write_test => "this shd be available"})
65
+ @address.read_test.should == "This should be assigned"
66
+ end
77
67
  end
78
- describe "usage of activemodel classes " do
79
- #it "should allow model's naming properties" do
80
- # @address = Address.new
81
- # Address.model_name.should == "Address"
82
- # @address.to_model.should == @addess
83
- #end
84
- end
68
+
85
69
 
86
70
  end
87
71
 
@@ -93,7 +77,7 @@ class Person < PlainOldModel::Base
93
77
  end
94
78
 
95
79
  class Address < PlainOldModel::Base
96
- attr_accessor :fname, :lname, :country
80
+ attr_accessor :fname, :lname
97
81
  attr_reader :read_test
98
82
  attr_writer :write_test
99
83
 
@@ -102,7 +86,7 @@ class Address < PlainOldModel::Base
102
86
  end
103
87
 
104
88
  class Country < PlainOldModel::Base
105
- attr_accessor :code, :name, :continent
89
+ attr_accessor :code, :name
106
90
 
107
91
  has_one :continent
108
92
  end
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.4
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2013-02-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
17
- requirement: &70168181150940 !ruby/object:Gem::Requirement
17
+ requirement: &70282593825980 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70168181150940
25
+ version_requirements: *70282593825980
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: activemodel
28
- requirement: &70168181150360 !ruby/object:Gem::Requirement
28
+ requirement: &70282593825520 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70168181150360
36
+ version_requirements: *70282593825520
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rake
39
- requirement: &70168181149920 !ruby/object:Gem::Requirement
39
+ requirement: &70282593825100 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *70168181149920
47
+ version_requirements: *70282593825100
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &70168181149500 !ruby/object:Gem::Requirement
50
+ requirement: &70282593824660 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70168181149500
58
+ version_requirements: *70282593824660
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: rspec-core
61
- requirement: &70168181149040 !ruby/object:Gem::Requirement
61
+ requirement: &70282593824180 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ! '>='
@@ -66,7 +66,7 @@ dependencies:
66
66
  version: '0'
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *70168181149040
69
+ version_requirements: *70282593824180
70
70
  description: ! "This gem is created to cater the projects which do not require a backend/database,\n
71
71
  \ but still need some of the niceties offered by the ActiveRecord"
72
72
  email:
@@ -85,6 +85,7 @@ files:
85
85
  - examples/book.rb
86
86
  - examples/person.rb
87
87
  - lib/plain_old_model.rb
88
+ - lib/plain_old_model/attribute_assignment.rb
88
89
  - lib/plain_old_model/base.rb
89
90
  - lib/plain_old_model/version.rb
90
91
  - plain_old_model.gemspec