duck_record 0.0.9.1 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +13 -13
  3. data/lib/duck_record/associations/association.rb +15 -15
  4. data/lib/duck_record/associations/collection_association.rb +8 -8
  5. data/lib/duck_record/associations/collection_proxy.rb +1 -1
  6. data/lib/duck_record/associations/has_many_association.rb +0 -1
  7. data/lib/duck_record/associations/singular_association.rb +6 -6
  8. data/lib/duck_record/attribute/user_provided_default.rb +2 -2
  9. data/lib/duck_record/attribute.rb +70 -70
  10. data/lib/duck_record/attribute_assignment.rb +74 -74
  11. data/lib/duck_record/attribute_methods/before_type_cast.rb +9 -9
  12. data/lib/duck_record/attribute_methods/dirty.rb +29 -29
  13. data/lib/duck_record/attribute_methods/read.rb +27 -27
  14. data/lib/duck_record/attribute_methods/write.rb +20 -20
  15. data/lib/duck_record/attribute_methods.rb +8 -8
  16. data/lib/duck_record/attribute_mutation_tracker.rb +4 -4
  17. data/lib/duck_record/attribute_set/yaml_encoder.rb +5 -5
  18. data/lib/duck_record/attribute_set.rb +5 -5
  19. data/lib/duck_record/attributes.rb +14 -14
  20. data/lib/duck_record/base.rb +18 -18
  21. data/lib/duck_record/callbacks.rb +1 -1
  22. data/lib/duck_record/core.rb +35 -35
  23. data/lib/duck_record/inheritance.rb +25 -25
  24. data/lib/duck_record/model_schema.rb +10 -11
  25. data/lib/duck_record/nested_attributes.rb +149 -149
  26. data/lib/duck_record/reflection.rb +14 -14
  27. data/lib/duck_record/type/array.rb +6 -6
  28. data/lib/duck_record/type/registry.rb +21 -21
  29. data/lib/duck_record/type/serialized.rb +8 -8
  30. data/lib/duck_record/type/time.rb +0 -1
  31. data/lib/duck_record/type/unsigned_integer.rb +6 -6
  32. data/lib/duck_record/type.rb +16 -14
  33. data/lib/duck_record/validations/uniqueness_on_real_record.rb +45 -45
  34. data/lib/duck_record/validations.rb +6 -6
  35. data/lib/duck_record/version.rb +1 -1
  36. data/lib/duck_record.rb +7 -7
  37. metadata +7 -6
@@ -12,9 +12,9 @@ module DuckRecord
12
12
  end
13
13
 
14
14
  @finder_class = if options[:class_name].present?
15
- @finder_class = options[:class_name].safe_constantize
15
+ options[:class_name].safe_constantize
16
16
  elsif options[:class].present? && options[:class].is_a?(Class)
17
- @finder_class = options[:class]
17
+ options[:class]
18
18
  else
19
19
  nil
20
20
  end
@@ -59,58 +59,58 @@ module DuckRecord
59
59
 
60
60
  protected
61
61
 
62
- def build_relation(klass, table, attribute, value) #:nodoc:
63
- if reflection = klass._reflect_on_association(attribute)
64
- attribute = reflection.foreign_key
65
- value = value.attributes[reflection.klass.primary_key] unless value.nil?
66
- end
67
-
68
- # the attribute may be an aliased attribute
69
- if klass.attribute_alias?(attribute)
70
- attribute = klass.attribute_alias(attribute)
71
- end
62
+ def build_relation(klass, table, attribute, value) #:nodoc:
63
+ if reflection = klass._reflect_on_association(attribute)
64
+ attribute = reflection.foreign_key
65
+ value = value.attributes[reflection.klass.primary_key] unless value.nil?
66
+ end
72
67
 
73
- attribute_name = attribute.to_s
68
+ # the attribute may be an aliased attribute
69
+ if klass.attribute_alias?(attribute)
70
+ attribute = klass.attribute_alias(attribute)
71
+ end
74
72
 
75
- column = klass.columns_hash[attribute_name]
76
- cast_type = klass.type_for_attribute(attribute_name)
77
- value = cast_type.serialize(value)
78
- value = klass.connection.type_cast(value)
73
+ attribute_name = attribute.to_s
79
74
 
80
- comparison = if !options[:case_sensitive] && !value.nil?
81
- # will use SQL LOWER function before comparison, unless it detects a case insensitive collation
82
- klass.connection.case_insensitive_comparison(table, attribute, column, value)
83
- else
84
- klass.connection.case_sensitive_comparison(table, attribute, column, value)
85
- end
86
- if value.nil?
87
- klass.unscoped.where(comparison)
88
- else
89
- bind = ActiveRecord::Relation::QueryAttribute.new(attribute_name, value, ActiveRecord::Type::Value.new)
90
- klass.unscoped.where(comparison, bind)
91
- end
92
- rescue RangeError
93
- klass.none
94
- end
75
+ column = klass.columns_hash[attribute_name]
76
+ cast_type = klass.type_for_attribute(attribute_name)
77
+ value = cast_type.serialize(value)
78
+ value = klass.connection.type_cast(value)
95
79
 
96
- def scope_relation(record, table, relation)
97
- Array(options[:scope]).each do |scope_item|
98
- scope_value = if record.class._reflect_on_association(scope_item)
99
- record.association(scope_item).reader
80
+ comparison = if !options[:case_sensitive] && !value.nil?
81
+ # will use SQL LOWER function before comparison, unless it detects a case insensitive collation
82
+ klass.connection.case_insensitive_comparison(table, attribute, column, value)
100
83
  else
101
- record._read_attribute(scope_item)
84
+ klass.connection.case_sensitive_comparison(table, attribute, column, value)
102
85
  end
103
- relation = relation.where(scope_item => scope_value)
86
+ if value.nil?
87
+ klass.unscoped.where(comparison)
88
+ else
89
+ bind = ActiveRecord::Relation::QueryAttribute.new(attribute_name, value, ActiveRecord::Type::Value.new)
90
+ klass.unscoped.where(comparison, bind)
91
+ end
92
+ rescue RangeError
93
+ klass.none
104
94
  end
105
95
 
106
- relation
107
- end
96
+ def scope_relation(record, table, relation)
97
+ Array(options[:scope]).each do |scope_item|
98
+ scope_value = if record.class._reflect_on_association(scope_item)
99
+ record.association(scope_item).reader
100
+ else
101
+ record._read_attribute(scope_item)
102
+ end
103
+ relation = relation.where(scope_item => scope_value)
104
+ end
108
105
 
109
- def map_enum_attribute(klass, attribute, value)
110
- mapping = klass.defined_enums[attribute.to_s]
111
- value = mapping[value] if value && mapping
112
- value
113
- end
106
+ relation
107
+ end
108
+
109
+ def map_enum_attribute(klass, attribute, value)
110
+ mapping = klass.defined_enums[attribute.to_s]
111
+ value = mapping[value] if value && mapping
112
+ value
113
+ end
114
114
  end
115
115
 
116
116
  module ClassMethods
@@ -31,12 +31,12 @@ module DuckRecord
31
31
 
32
32
  private
33
33
 
34
- def default_validation_context
35
- :default
36
- end
34
+ def default_validation_context
35
+ :default
36
+ end
37
37
 
38
- def perform_validations(options = {})
39
- options[:validate] == false || valid?(options[:context])
40
- end
38
+ def perform_validations(options = {})
39
+ options[:validate] == false || valid?(options[:context])
40
+ end
41
41
  end
42
42
  end
@@ -1,3 +1,3 @@
1
1
  module DuckRecord
2
- VERSION = '0.0.9.1'
2
+ VERSION = "0.0.10"
3
3
  end
data/lib/duck_record.rb CHANGED
@@ -1,9 +1,9 @@
1
- require 'active_support'
2
- require 'active_support/rails'
3
- require 'active_model'
1
+ require "active_support"
2
+ require "active_support/rails"
3
+ require "active_model"
4
4
 
5
- require 'duck_record/type'
6
- require 'duck_record/attribute_set'
5
+ require "duck_record/type"
6
+ require "duck_record/attribute_set"
7
7
 
8
8
  module DuckRecord
9
9
  extend ActiveSupport::Autoload
@@ -22,7 +22,7 @@ module DuckRecord
22
22
  autoload :Validations
23
23
 
24
24
  eager_autoload do
25
- autoload :DuckRecordError, 'duck_record/errors'
25
+ autoload :DuckRecordError, "duck_record/errors"
26
26
 
27
27
  autoload :Associations
28
28
  autoload :AttributeAssignment
@@ -50,5 +50,5 @@ module DuckRecord
50
50
  end
51
51
 
52
52
  ActiveSupport.on_load(:i18n) do
53
- I18n.load_path << File.dirname(__FILE__) + '/duck_record/locale/en.yml'
53
+ I18n.load_path << File.dirname(__FILE__) + "/duck_record/locale/en.yml"
54
54
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: duck_record
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9.1
4
+ version: 0.0.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - jasl
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-20 00:00:00.000000000 Z
11
+ date: 2017-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -66,10 +66,11 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
- description: "It looks like Active Record and quacks like Active Record, but it can't
70
- do persistence or querying,\n it's Duck Record! \n Actually it's extract from
71
- Active Record.\n Used for creating virtual models like ActiveType or ModelAttribute
72
- does."
69
+ description: |-
70
+ It looks like Active Record and quacks like Active Record, but it can't do persistence or querying,
71
+ it's Duck Record!
72
+ Actually it's extract from Active Record.
73
+ Used for creating virtual models like ActiveType or ModelAttribute does.
73
74
  email:
74
75
  - jasl9187@hotmail.com
75
76
  executables: []