activeservice 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 +9 -0
- data/.rvmrc +1 -0
- data/Gemfile +4 -0
- data/LICENSE.md +22 -0
- data/README.md +56 -0
- data/activeservice.gemspec +26 -0
- data/examples/books.rb +4 -0
- data/examples/person.rb +41 -0
- data/lib/activeservice.rb +14 -0
- data/lib/activeservice/base.rb +143 -0
- data/lib/activeservice/version.rb +3 -0
- data/spec/lib/base_spec.rb +85 -0
- data/spec/spec_helper.rb +5 -0
- metadata +117 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm use ruby-1.9.2-p290@localdev
|
data/Gemfile
ADDED
data/LICENSE.md
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,56 @@
|
|
1
|
+
# Activeservice
|
2
|
+
|
3
|
+
|
4
|
+
This gem is created for projects which do not require a backend/database,
|
5
|
+
but still need all the niceties offered by the ActiveModel
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'activeservice'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install activeservice
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Example
|
24
|
+
=======
|
25
|
+
|
26
|
+
class Person < Activeservice::Base
|
27
|
+
attr_accessor :name, :age, :books
|
28
|
+
|
29
|
+
attr_reader :account_number
|
30
|
+
attr_writer :address
|
31
|
+
end
|
32
|
+
|
33
|
+
params = {"name" =>"testmeparams", "age" => "25", "books" =>["wewrwrwr", "werwrwrr"]}
|
34
|
+
|
35
|
+
params1 = {:name =>"testmeparams", :age => "25", :books => {:author =>"my name", :category => "fiction"}}
|
36
|
+
|
37
|
+
p = Person.new(params)
|
38
|
+
|
39
|
+
p1 = Person.new(params1)
|
40
|
+
|
41
|
+
puts p.attributes
|
42
|
+
puts p.attributes.inspect
|
43
|
+
|
44
|
+
TODO:
|
45
|
+
|
46
|
+
* Association
|
47
|
+
* mass assignments should be added later....
|
48
|
+
*
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
1. Fork it
|
53
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
54
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
55
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
56
|
+
5. Create new Pull Request
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'activeservice/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "activeservice"
|
8
|
+
gem.version = Activeservice::VERSION
|
9
|
+
gem.authors = ["Bhaskar Sundarraj"]
|
10
|
+
gem.email = ["bhaskar.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
|
+
|
17
|
+
gem.files = `git ls-files`.split($/)
|
18
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
19
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
20
|
+
gem.require_paths = ["lib"]
|
21
|
+
gem.add_dependency 'activesupport'
|
22
|
+
gem.add_dependency 'activemodel'
|
23
|
+
gem.add_dependency 'test-unit'
|
24
|
+
gem.add_dependency 'rake'
|
25
|
+
gem.add_dependency 'rspec'
|
26
|
+
end
|
data/examples/books.rb
ADDED
data/examples/person.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative '../lib/activeservice/base'
|
2
|
+
|
3
|
+
class Person < Activeservice::Base
|
4
|
+
attr_accessor :name, :age, :books
|
5
|
+
|
6
|
+
attr_reader :test
|
7
|
+
attr_writer :testmewriter
|
8
|
+
|
9
|
+
has_one :books, :class_name => :books
|
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", "books" =>["wewrwrwr", "werwrwrr"], :erre =>"wewrwrwrw"}
|
17
|
+
params1 = {:name1 =>"testmeparams", :age => "25", :books => {:author =>"my name", :category => "fiction"}}
|
18
|
+
|
19
|
+
params11 = {:name =>"testmeparams", :age => "25", :books => {:author =>"my name", :category => "fiction"}}
|
20
|
+
|
21
|
+
#puts params.inspect
|
22
|
+
#params = params.with_indifferent_access
|
23
|
+
#puts params.inspect
|
24
|
+
#p = Person.new
|
25
|
+
|
26
|
+
|
27
|
+
p = Person.new(params)
|
28
|
+
p.valid?
|
29
|
+
|
30
|
+
p.assign_attributes(params11)
|
31
|
+
p1 = Person.new(params1)
|
32
|
+
puts p.attributes
|
33
|
+
puts p.attributes.inspect
|
34
|
+
puts p.errors.values.inspect
|
35
|
+
puts p.inspect
|
36
|
+
puts p1.inspect
|
37
|
+
|
38
|
+
p2 = Person.new
|
39
|
+
|
40
|
+
p2.attributes
|
41
|
+
|
@@ -0,0 +1,14 @@
|
|
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 Activeservice
|
12
|
+
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,143 @@
|
|
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 Activeservice
|
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
|
+
|
22
|
+
# refactor this later to single method to gather all attributes
|
23
|
+
def self.attr_accessor(*attrs)
|
24
|
+
get_attributes(attrs)
|
25
|
+
super(*attrs)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.attr_writer(*attrs)
|
29
|
+
get_attributes(attrs)
|
30
|
+
super(*attrs)
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.attr_reader(*attrs)
|
34
|
+
get_attributes(attrs)
|
35
|
+
super(*attrs)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.attributes
|
39
|
+
@attributes
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.get_attributes(attrs)
|
43
|
+
@attributes ||= []
|
44
|
+
@attributes.concat attrs
|
45
|
+
end
|
46
|
+
|
47
|
+
def initialize(attributes = nil, options = {})
|
48
|
+
assign_attributes(attributes, options) if attributes
|
49
|
+
end
|
50
|
+
|
51
|
+
def self.attr_refers_to_class(klass, options= {})
|
52
|
+
puts options.inspect
|
53
|
+
if options.nil?
|
54
|
+
klass.to_s.constantize
|
55
|
+
else
|
56
|
+
class_name = options[:class_name].to_s
|
57
|
+
class_name.constantize
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
#creation attributes ->create the class referred , and assign the variables
|
62
|
+
def self.has_one(klass, options={})
|
63
|
+
if !options[:class_name]
|
64
|
+
klass.to_s.capitalize.constantize
|
65
|
+
else
|
66
|
+
options[:class_name].nil? ? options[:class_name].to_s.constantize : "Class name not found"
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.belongs_to(klass, options={})
|
71
|
+
if !options[:class_name]
|
72
|
+
klass.to_s.constantize
|
73
|
+
else
|
74
|
+
options[:class_name].nil? ? options[:class_name].to_s.constantize : "Class name not found"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def assign_attributes(new_attributes, options = {})
|
79
|
+
return unless new_attributes
|
80
|
+
|
81
|
+
#attributes = new_attributes.stringify_keys
|
82
|
+
attributes = sanitize_attributes(new_attributes).stringify_keys
|
83
|
+
multi_parameter_attributes = []
|
84
|
+
@mass_assignment_options = options
|
85
|
+
|
86
|
+
attributes.each do |k, v|
|
87
|
+
if k.include?("(")
|
88
|
+
multi_parameter_attributes << [ k, v ]
|
89
|
+
elsif respond_to?("#{k}=")
|
90
|
+
send("#{k}=", v)
|
91
|
+
else
|
92
|
+
raise(Exception) #( "unknown attribute: #{k}")
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def sanitize_attributes(attributes)
|
98
|
+
sanitized_attributes = {}
|
99
|
+
attributes.each do |k,v|
|
100
|
+
if respond_to?("#{k}=")
|
101
|
+
sanitized_attributes[k]=v
|
102
|
+
end
|
103
|
+
end
|
104
|
+
sanitized_attributes
|
105
|
+
end
|
106
|
+
|
107
|
+
def persisted?
|
108
|
+
false
|
109
|
+
end
|
110
|
+
|
111
|
+
def attributes
|
112
|
+
self.class.attributes
|
113
|
+
end
|
114
|
+
|
115
|
+
def mass_assignment_options
|
116
|
+
@mass_assignment_options ||= {}
|
117
|
+
end
|
118
|
+
|
119
|
+
def mass_assignment_role
|
120
|
+
mass_assignment_options || :default
|
121
|
+
end
|
122
|
+
|
123
|
+
def initialize_attributes
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
#def creation_attributes
|
128
|
+
# attributes = {}
|
129
|
+
#
|
130
|
+
# if reflection.macro.in?([:has_one, :has_many]) && !options[:through]
|
131
|
+
# attributes[reflection.foreign_key] = owner[reflection.active_record_primary_key]
|
132
|
+
#
|
133
|
+
# if reflection.options[:as]
|
134
|
+
# attributes[reflection.type] = owner.class.base_class.name
|
135
|
+
# end
|
136
|
+
# end
|
137
|
+
#
|
138
|
+
# attributes
|
139
|
+
#end
|
140
|
+
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Activeservice::Base do
|
4
|
+
|
5
|
+
|
6
|
+
describe "gem version" do
|
7
|
+
it "should return the current version of the gem" do
|
8
|
+
Activeservice::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
|
+
puts @address.errors.inspect
|
52
|
+
@address.errors.should_not == nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
describe "association" do
|
57
|
+
it "should create a new instance and assign_attributes to the has_one class" do
|
58
|
+
@person = Person.new({:fname => "first value", :lname => "second value",:address => {:fname => "test", :lname =>"testme",:country => "india"}})
|
59
|
+
end
|
60
|
+
end
|
61
|
+
describe "usage of activemodel classes " do
|
62
|
+
#it "should allow model's naming properties" do
|
63
|
+
# @address = Address.new
|
64
|
+
# Address.model_name.should == "Address"
|
65
|
+
# @address.to_model.should == @addess
|
66
|
+
#end
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
class Person < Activeservice::Base
|
73
|
+
attr_accessor :fname, :lname, :address
|
74
|
+
#has_one :address,:class_name => :address
|
75
|
+
end
|
76
|
+
|
77
|
+
class Address < Activeservice::Base
|
78
|
+
attr_accessor :fname, :lname, :country
|
79
|
+
attr_reader :read_test
|
80
|
+
attr_writer :write_test, :me
|
81
|
+
|
82
|
+
#belongs_to :person
|
83
|
+
validates_presence_of :country
|
84
|
+
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activeservice
|
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-06 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: &70118642785800 !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: *70118642785800
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activemodel
|
27
|
+
requirement: &70118642785380 !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: *70118642785380
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: test-unit
|
38
|
+
requirement: &70118646512560 !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: *70118646512560
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: &70118646512140 !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: *70118646512140
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rspec
|
60
|
+
requirement: &70118646511720 !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: *70118646511720
|
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
|
+
- bhaskar.sundarraj@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .rvmrc
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.md
|
81
|
+
- README.md
|
82
|
+
- activeservice.gemspec
|
83
|
+
- examples/books.rb
|
84
|
+
- examples/person.rb
|
85
|
+
- lib/activeservice.rb
|
86
|
+
- lib/activeservice/base.rb
|
87
|
+
- lib/activeservice/version.rb
|
88
|
+
- spec/lib/base_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: ''
|
91
|
+
licenses: []
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.8.17
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: This gem is created to cater the projects which do not require a backend/database,
|
114
|
+
but still need all the niceties offered by the ActiveModel
|
115
|
+
test_files:
|
116
|
+
- spec/lib/base_spec.rb
|
117
|
+
- spec/spec_helper.rb
|