plain_old_model 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,27 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea/.name
19
+ .idea/.rakeTasks
20
+ .idea/dictionaries/bsundarraj.xml
21
+ .idea/encodings.xml
22
+ .idea/misc.xml
23
+ .idea/modules.xml
24
+ .idea/plain_old_model.iml
25
+ .idea/scopes/scope_settings.xml
26
+ .idea/vcs.xml
27
+ .idea/workspace.xml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in plain_old_model.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bhaskar Sundarraj
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # PlainOldModel
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'plain_old_model'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install plain_old_model
18
+
19
+ ## Usage
20
+ Example
21
+ =======
22
+
23
+ class Person < Activeservice::Base
24
+ attr_accessor :name, :age, :book
25
+
26
+ attr_reader :account_number
27
+ attr_writer :address
28
+
29
+ validates_presence_of :book
30
+ end
31
+
32
+ params = {"name" =>"testmeparams", "age" => "25", "book" =>["wewrwrwr", "werwrwrr"]}
33
+
34
+ params1 = {:name =>"testmeparams", :age => "25", :book => {:author =>"my name", :category => "fiction"}}
35
+
36
+ p = Person.new(params)
37
+ p.book # ["wewrwrwr", "werwrwrr"]
38
+
39
+ p.valid? #true
40
+
41
+ OR
42
+ p.assign_attributes(params11)
43
+
44
+ p1 = Person.new(params1)
45
+
46
+ p1.book # {:author =>"my name", :category => "fiction"}
47
+
48
+ p.attributes #[:name, :age, :book, :account_number, :address]
49
+
50
+
51
+ TODO:
52
+
53
+ * Association(s)
54
+ * mass assignments
55
+ *
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rspec/core/rake_task'
2
+
3
+ RSpec::Core::RakeTask.new('spec')
4
+
5
+ # If you want to make this the default task
6
+ task :default => :spec
data/examples/book.rb ADDED
@@ -0,0 +1,6 @@
1
+ require_relative '../lib/activeservice/base'
2
+ class Book < PlainOldModel::Base
3
+
4
+ attr_accessor :author, :category
5
+
6
+ end
@@ -0,0 +1,45 @@
1
+ require_relative '../lib/plain_old_model/base'
2
+ require_relative'book'
3
+ class Person < PlainOldModel::Base
4
+ attr_accessor :name, :age, :book
5
+
6
+ attr_reader :test
7
+ attr_writer :testmewriter
8
+
9
+ associated_class :book, :class_name => :book
10
+
11
+ validates_presence_of :name ,:message => "The field is a required field"
12
+ end
13
+
14
+
15
+ #params = { :person =>{:name =>"testme", :age => "25"}, :commit=>"Create Person"}
16
+ params = {"name1" =>"testmeparams", "age" => "25", "book" =>["wewrwrwr", "werwrwrr"], :erre =>"wewrwrwrw"}
17
+ params1 = {:name1 =>"testmeparams", :age => "25", :book => {:author =>"my name", :category => "fiction"}}
18
+
19
+ params11 = {:name =>"testmeparamsnew", :age => "28", :book => {:author =>"my new name", :category => "science fiction"}}
20
+
21
+ #puts params.inspect
22
+ #params = params.with_indifferent_access
23
+ #puts params.inspect
24
+ #p = Person.new
25
+
26
+
27
+
28
+ p = Person.new(params1)
29
+ p.valid?
30
+ #@book = Book.new
31
+ p.assign_attributes(params11)
32
+ puts p.book
33
+
34
+ #@book.assign_attributes(p.book)
35
+ p1 = Person.new(params1)
36
+ puts p.attributes
37
+ puts p.attributes.inspect
38
+ puts p.errors.values.inspect
39
+ puts p.inspect
40
+ puts p1.inspect
41
+
42
+ p2 = Person.new
43
+
44
+ p2.attributes
45
+
@@ -0,0 +1,112 @@
1
+ require 'active_support/all'
2
+ require 'active_model/validations'
3
+ require 'active_model/naming'
4
+ require 'active_model/translation'
5
+ 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'
11
+ module PlainOldModel
12
+ class Base
13
+ extend ActiveModel::Naming
14
+ include ActiveModel::Translation
15
+ include ActiveModel::Validations
16
+ include ActiveModel::Conversion
17
+ include ActiveModel::AttributeMethods
18
+ include ActiveModel::Serializers::JSON
19
+ include ActiveModel::MassAssignmentSecurity
20
+
21
+ # refactor this later to single method to gather all attributes
22
+ def self.attr_accessor(*attrs)
23
+ gather_attributes(attrs)
24
+ super(*attrs)
25
+ end
26
+
27
+ def self.attr_writer(*attrs)
28
+ gather_attributes(attrs)
29
+ super(*attrs)
30
+ end
31
+
32
+ def self.attr_reader(*attrs)
33
+ gather_attributes(attrs)
34
+ super(*attrs)
35
+ end
36
+
37
+ def self.attributes
38
+ @attributes ||= []
39
+ end
40
+
41
+ def self.gather_attributes(attrs)
42
+ attributes.concat attrs
43
+ end
44
+
45
+ def initialize(attributes = nil, options = {})
46
+ options = options[:new_record]= true
47
+ assign_attributes(attributes, options) if attributes
48
+ end
49
+
50
+ #creation attributes ->create the class referred , and assign the variables
51
+
52
+ def self.associated_class(klass, options={})
53
+ @association = {}
54
+ if options[:class_name]
55
+ @association[klass] = options[:class_name].to_s.capitalize unless options[:class_name].nil?
56
+ else
57
+ @association[klass] = klass.to_s.capitalize
58
+ end
59
+ @association
60
+ end
61
+
62
+ def self.association
63
+ @association
64
+ end
65
+
66
+ def assign_attributes(new_attributes, options = {})
67
+ return unless new_attributes
68
+
69
+ attributes = sanitize_attributes(new_attributes).stringify_keys
70
+ multi_parameter_attributes = []
71
+ @mass_assignment_options = options
72
+
73
+ attributes.each do |k, v|
74
+ if k.include?("(")
75
+ multi_parameter_attributes << [ k, v ]
76
+ elsif respond_to?("#{k}=")
77
+ send("#{k}=", v)
78
+ else
79
+ raise(Exception)
80
+ end
81
+ end
82
+ end
83
+
84
+ def sanitize_attributes(attributes)
85
+ sanitized_attributes = {}
86
+ attributes.each do |k,v|
87
+ if respond_to?("#{k}=")
88
+ sanitized_attributes[k]=v
89
+ end
90
+ end
91
+ sanitized_attributes
92
+ end
93
+
94
+ def persisted?
95
+ false
96
+ end
97
+
98
+ def attributes
99
+ self.class.attributes
100
+ end
101
+
102
+ def mass_assignment_options
103
+ @mass_assignment_options ||= {}
104
+ end
105
+
106
+ def mass_assignment_role
107
+ mass_assignment_options || :default
108
+ end
109
+
110
+ end
111
+
112
+ end
@@ -0,0 +1,3 @@
1
+ module PlainOldModel
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "plain_old_model/version"
2
+
3
+ module PlainOldModel
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'plain_old_model/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "plain_old_model"
8
+ gem.version = PlainOldModel::VERSION
9
+ gem.authors = ["Bhaskar Sundarraj"]
10
+ gem.email = ["bskar.sundarraj@gmail.com"]
11
+ gem.description = %q{This gem is created to cater the projects which do not require a backend/database,
12
+ but still need all the niceties offered by the ActiveModel}
13
+ gem.summary = %q{This gem is created to cater the projects which do not require a backend/database,
14
+ but still need all the niceties offered by the ActiveModel}
15
+ gem.homepage = ""
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(spec|features)/})
19
+ gem.require_paths = ["lib","examples","spec"]
20
+ gem.add_dependency 'activesupport'
21
+ gem.add_dependency 'activemodel'
22
+ gem.add_dependency 'rake'
23
+ gem.add_dependency 'rspec'
24
+ gem.add_dependency 'rspec-core'
25
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe PlainOldModel::Base do
4
+
5
+
6
+ describe "gem version" do
7
+ it "should return the current version of the gem" do
8
+ PlainOldModel::VERSION.should == '0.0.1'
9
+ end
10
+ end
11
+
12
+ 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, :me]
19
+ end
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"})
22
+ @person.fname.should == "first value"
23
+ @person.lname.should == "second value"
24
+ end
25
+ it "should not assign value to the attr_reader attributes/ read only attribute" do
26
+ @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'
29
+ @address.read_test.should == nil
30
+ end
31
+ it "should assign value to the attr_writer attributes" do
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'
35
+ @address.instance_variable_get(:@write_test).should == "this shd be available"
36
+ end
37
+ it "should assign_attributes to the class" do
38
+ @person = Person.new
39
+ @person.assign_attributes({:fname => "first value", :lname => "second value"})
40
+ @person.fname.should == "first value"
41
+ @person.lname.should == "second value"
42
+ end
43
+ it "should eliminate the params that are not available in the class" do
44
+ @person= Person.new({:fname => "first value", :lname => "second value"})
45
+ @person.valid?.should == true
46
+ end
47
+ 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
52
+ end
53
+ end
54
+
55
+ 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
59
+ end
60
+ describe "usage of activemodel classes " do
61
+ #it "should allow model's naming properties" do
62
+ # @address = Address.new
63
+ # Address.model_name.should == "Address"
64
+ # @address.to_model.should == @addess
65
+ #end
66
+ end
67
+
68
+ end
69
+
70
+
71
+ class Person < PlainOldModel::Base
72
+ attr_accessor :fname, :lname, :address
73
+
74
+ end
75
+
76
+ class Address < PlainOldModel::Base
77
+ attr_accessor :fname, :lname, :country
78
+ attr_reader :read_test
79
+ attr_writer :write_test, :me
80
+
81
+ #belongs_to :person
82
+ validates_presence_of :country
83
+
84
+ end
@@ -0,0 +1,22 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ require 'plain_old_model'
8
+ require 'plain_old_model/base'
9
+ require 'plain_old_model/version'
10
+
11
+
12
+ RSpec.configure do |config|
13
+ config.treat_symbols_as_metadata_keys_with_true_values = true
14
+ config.run_all_when_everything_filtered = true
15
+ config.filter_run :focus
16
+
17
+ # Run specs in random order to surface order dependencies. If you find an
18
+ # order dependency and want to debug it, you can fix the order by providing
19
+ # the seed, which is printed after each run.
20
+ # --seed 1234
21
+ config.order = 'random'
22
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plain_old_model
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bhaskar Sundarraj
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70337828581720 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70337828581720
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &70337828581240 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70337828581240
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
38
+ requirement: &70337828580820 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70337828580820
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec
49
+ requirement: &70337828580400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *70337828580400
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec-core
60
+ requirement: &70337828579980 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: *70337828579980
69
+ description: ! "This gem is created to cater the projects which do not require a backend/database,\n
70
+ \ but still need all the niceties offered by the ActiveModel"
71
+ email:
72
+ - bskar.sundarraj@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - .rspec
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - examples/book.rb
84
+ - examples/person.rb
85
+ - lib/plain_old_model.rb
86
+ - lib/plain_old_model/base.rb
87
+ - lib/plain_old_model/version.rb
88
+ - plain_old_model.gemspec
89
+ - spec/lib/base_spec.rb
90
+ - spec/spec_helper.rb
91
+ homepage: ''
92
+ licenses: []
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ - examples
98
+ - spec
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ! '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 1.8.17
114
+ signing_key:
115
+ specification_version: 3
116
+ summary: This gem is created to cater the projects which do not require a backend/database,
117
+ but still need all the niceties offered by the ActiveModel
118
+ test_files:
119
+ - spec/lib/base_spec.rb
120
+ - spec/spec_helper.rb