attribute_inheritance 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in attribute_inheritance.gemspec
4
+ gemspec
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "attribute_inheritance/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "attribute_inheritance"
7
+ s.version = AttributeInheritance::VERSION
8
+ s.authors = ["Semyon Perepelitsa"]
9
+ s.email = ["sema@sema.in"]
10
+ s.homepage = ""
11
+ s.summary = %q{Inherit attributes from model's associations}
12
+ # s.description = %q{TODO: Write a gem description}
13
+
14
+ s.rubyforge_project = "attribute_inheritance"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "activesupport"
22
+
23
+ # specify any dependencies here; for example:
24
+ # s.add_development_dependency "rspec"
25
+ # s.add_runtime_dependency "rest-client"
26
+ end
@@ -0,0 +1,3 @@
1
+ require "attribute_inheritance/model_additions"
2
+ require "attribute_inheritance/railtie" if defined? Rails
3
+ require "attribute_inheritance/mongoid" if defined? Mongoid
@@ -0,0 +1,16 @@
1
+ require "active_support/core_ext/module/aliasing"
2
+
3
+ module AttributeInheritance
4
+ module ModelAdditions
5
+ def attr_inherited(*attributes, options)
6
+ accessor = options.fetch(:from)
7
+ define_method :"#{accessor}_with_setting_defaults=" do |object|
8
+ attributes.each do |attr|
9
+ send(attr) || send("#{attr}=", object.send(attr))
10
+ end if object
11
+ send(:"#{accessor}_without_setting_defaults=", object)
12
+ end
13
+ alias_method_chain :"#{accessor}=", :setting_defaults
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Mongoid
2
+ module Document
3
+ module ClassMethods
4
+ include AttributeInheritance::ModelAdditions
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,11 @@
1
+ module AttributeInheritance
2
+ class Railtie < ::Rails::Railtie
3
+
4
+ initializer 'attribute_inheritance.model_additions' do
5
+ ActiveSupport.on_load :active_record do
6
+ extend ModelAdditions
7
+ end
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module AttributeInheritance
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require 'minitest/autorun'
2
+ $:.unshift(File.expand_path('../../lib', __FILE__))
3
+ require "attribute_inheritance"
4
+
5
+ class Person
6
+ extend AttributeInheritance::ModelAdditions
7
+ attr_accessor :last_name, :habits, :parent
8
+ attr_inherited :last_name, :habits, :from => :parent
9
+ end
10
+
11
+ class AttributeInheritanceTest < MiniTest::Unit::TestCase
12
+ def test_accessors_are_not_affected
13
+ person = Person.new
14
+ assert_nil person.last_name
15
+ person.last_name = "Pupkine"
16
+ assert_equal "Pupkine", person.last_name
17
+ end
18
+
19
+ def test_only_nil_attributes_are_inherited
20
+ parent = Person.new
21
+ parent.last_name = "Pupkine"
22
+ parent.habits = "Bad ones"
23
+
24
+ person = Person.new
25
+ person.habits = "Good ones"
26
+
27
+ person.parent = parent
28
+ assert_equal "Pupkine", person.last_name
29
+ assert_equal "Good ones", person.habits
30
+ end
31
+
32
+ def test_setting_nil_parent
33
+ person = Person.new
34
+ person.last_name = "Pupkine"
35
+ person.parent = nil
36
+ assert_equal "Pupkine", person.last_name
37
+ end
38
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_inheritance
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Semyon Perepelitsa
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-12-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70153489840720 !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: *70153489840720
25
+ description:
26
+ email:
27
+ - sema@sema.in
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files: []
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - attribute_inheritance.gemspec
36
+ - lib/attribute_inheritance.rb
37
+ - lib/attribute_inheritance/model_additions.rb
38
+ - lib/attribute_inheritance/mongoid.rb
39
+ - lib/attribute_inheritance/railtie.rb
40
+ - lib/attribute_inheritance/version.rb
41
+ - test/attribute_inheritance_test.rb
42
+ homepage: ''
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ requirements: []
61
+ rubyforge_project: attribute_inheritance
62
+ rubygems_version: 1.8.11
63
+ signing_key:
64
+ specification_version: 3
65
+ summary: Inherit attributes from model's associations
66
+ test_files:
67
+ - test/attribute_inheritance_test.rb
68
+ has_rdoc: