lazy_lazer 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/lazy_lazer/internal_model.rb +9 -4
- data/lib/lazy_lazer/key_metadata.rb +24 -26
- data/lib/lazy_lazer/key_metadata_store.rb +44 -0
- data/lib/lazy_lazer/version.rb +1 -1
- data/lib/lazy_lazer.rb +18 -9
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1f995a4141541e28c93522484e53a3a0e7d36048
|
4
|
+
data.tar.gz: 27aee5cf991dcf61f80fd915c328769a7c3d0889
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd15e803ab225318bd4d93664e6756096814605bf084f2b4e7de84f10a63e3656a2c3e637d01f109c6bb962dfff8b7cded01259698c6385dd2c18fbac256521a
|
7
|
+
data.tar.gz: 3826025a30d3dce79d0a2b44dc067e7c59e01bfecf68704192713c39d078b9a1a8e4673bf797b0ba3f1787bf0739e355edac2d3427811cfe0f5c546e7ccfc7c5
|
@@ -4,7 +4,7 @@ module LazyLazer
|
|
4
4
|
# A delegator for internal operations.
|
5
5
|
class InternalModel
|
6
6
|
# Create an internal model with a reference to a public model.
|
7
|
-
# @param key_metadata [
|
7
|
+
# @param key_metadata [KeyMetadataStore] a reference to a metadata store
|
8
8
|
# @param parent [LazyLazer] a reference to a LazyLazer model
|
9
9
|
def initialize(key_metadata, parent)
|
10
10
|
@key_metadata = key_metadata
|
@@ -17,12 +17,17 @@ module LazyLazer
|
|
17
17
|
# @raise RequiredAttribute if a required attribute is missing
|
18
18
|
# @return [void]
|
19
19
|
def verify_required!
|
20
|
-
@key_metadata.each do |key_name
|
21
|
-
next if
|
20
|
+
@key_metadata.required_properties.each do |key_name|
|
21
|
+
next if @source_hash.key?(@key_metadata.get(key_name).source_key)
|
22
22
|
raise RequiredAttribute, "#{@parent} requires `#{key_name}`"
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
26
|
+
# @return [Array] the identity properties
|
27
|
+
def identity_properties
|
28
|
+
@key_metadata.identity_properties
|
29
|
+
end
|
30
|
+
|
26
31
|
# Converts all unconverted keys and packages them as a hash.
|
27
32
|
# @return [Hash] the converted hash
|
28
33
|
def parent_to_h
|
@@ -70,7 +75,7 @@ module LazyLazer
|
|
70
75
|
# @return [KeyMetadata] the key metadata, if found
|
71
76
|
# @raise MissingAttribute if the key metadata wasn't found
|
72
77
|
def ensure_metadata_exists(key_name)
|
73
|
-
return @key_metadata
|
78
|
+
return @key_metadata.get(key_name) if @key_metadata.contains?(key_name)
|
74
79
|
raise MissingAttribute, "`#{key_name}` isn't defined for #{@parent}"
|
75
80
|
end
|
76
81
|
|
@@ -6,40 +6,33 @@ module LazyLazer
|
|
6
6
|
# Simple PORO for key metadata. Yay value objects!
|
7
7
|
class KeyMetadata
|
8
8
|
# @return [Symbol] the key to fetch the value from
|
9
|
-
|
9
|
+
attr_accessor :source_key
|
10
|
+
|
11
|
+
# @return [Boolean] whether the key must exist when creating the model
|
12
|
+
attr_accessor :required
|
13
|
+
|
14
|
+
# @return [Boolean] whether the key is used for equality comparision
|
15
|
+
attr_accessor :identity
|
16
|
+
|
17
|
+
# @return [Boolean] whether the key must exist when loaded
|
18
|
+
attr_accessor :runtime_required
|
10
19
|
|
11
20
|
# @return [Proc, Object] the default value or generator
|
12
|
-
|
21
|
+
attr_accessor :default
|
13
22
|
|
14
23
|
# @return [Proc, Symbol, nil] the method or proc that transforms the return value
|
15
|
-
|
24
|
+
attr_accessor :transform
|
16
25
|
|
17
26
|
# Load attributes from a {LazyLazer::ClassMethods#property} method signature.
|
18
27
|
# @see LazyLazer::ClassMethods#property
|
19
|
-
def
|
28
|
+
def initialize(key_name, *boolean_options, **options)
|
20
29
|
boolean_options.each_with_object(options) { |sym, hsh| hsh[sym] = true }
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
)
|
28
|
-
end
|
29
|
-
|
30
|
-
# Create a new KeyMetadata value object with all the properties.
|
31
|
-
# @param source_key [Symbol] the key to fetch the value from
|
32
|
-
# @param required [Boolean] whether the key must exist when creating the model
|
33
|
-
# @param runtime_required [Boolean] whether the key must exist when loaded
|
34
|
-
# @param default [Proc, Object] the default value or generator
|
35
|
-
# @param transform [Proc, Symbol, nil] the method or proc that transforms the return value
|
36
|
-
def initialize(source_key:, required:, runtime_required:, default:, transform:)
|
37
|
-
@source_key = source_key
|
38
|
-
@required = required
|
39
|
-
@runtime_required = runtime_required
|
40
|
-
@default = default
|
41
|
-
@transform = transform
|
42
|
-
freeze
|
30
|
+
self.source_key = options[:from] || key_name
|
31
|
+
self.required = !!options[:required]
|
32
|
+
self.identity = !!options[:identity]
|
33
|
+
self.runtime_required = !options.key?(:default) && !options[:nil]
|
34
|
+
self.transform = options[:with]
|
35
|
+
self.default = options[:default]
|
43
36
|
end
|
44
37
|
|
45
38
|
# @return [Boolean] whether the key must exist when creating the model
|
@@ -47,6 +40,11 @@ module LazyLazer
|
|
47
40
|
@required
|
48
41
|
end
|
49
42
|
|
43
|
+
# @return [Boolean] whether the key is used for equality comparision
|
44
|
+
def identity?
|
45
|
+
@identity
|
46
|
+
end
|
47
|
+
|
50
48
|
# @return [Boolean] whether the key must exist when loaded
|
51
49
|
def runtime_required?
|
52
50
|
@runtime_required
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module LazyLazer
|
4
|
+
# The key metadata collection class
|
5
|
+
class KeyMetadataStore
|
6
|
+
# @return [Array<Symbol>] the required properties
|
7
|
+
attr_reader :required_properties
|
8
|
+
|
9
|
+
# @return [Array<Symbol>] the identity properties
|
10
|
+
attr_reader :identity_properties
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@collection = {}
|
14
|
+
@required_properties = []
|
15
|
+
@identity_properties = []
|
16
|
+
end
|
17
|
+
|
18
|
+
# Add a KeyMetadata to the store.
|
19
|
+
# @param key [Symbol] the key
|
20
|
+
# @param meta [KeyMetadata] the key metadata
|
21
|
+
# @return [KeyMetadata] the provided meta
|
22
|
+
def add(key, meta)
|
23
|
+
@collection[key] = meta
|
24
|
+
@required_properties << key if meta.required?
|
25
|
+
@identity_properties << key if meta.identity?
|
26
|
+
meta
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Array<Symbol>] the keys in the store
|
30
|
+
def keys
|
31
|
+
@collection.keys
|
32
|
+
end
|
33
|
+
|
34
|
+
# @return [Boolean] whether the store contains the key
|
35
|
+
def contains?(key)
|
36
|
+
@collection.key?(key)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [KeyMetadata] fetch the metadata from the store
|
40
|
+
def get(key)
|
41
|
+
@collection.fetch(key)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
data/lib/lazy_lazer/version.rb
CHANGED
data/lib/lazy_lazer.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require_relative 'lazy_lazer/errors'
|
4
4
|
require_relative 'lazy_lazer/internal_model'
|
5
|
+
require_relative 'lazy_lazer/key_metadata_store'
|
5
6
|
require_relative 'lazy_lazer/key_metadata'
|
6
7
|
require_relative 'lazy_lazer/version'
|
7
8
|
|
@@ -17,7 +18,7 @@ module LazyLazer
|
|
17
18
|
def self.included(base)
|
18
19
|
base.extend(ClassMethods)
|
19
20
|
base.include(InstanceMethods)
|
20
|
-
base.instance_variable_set(:@
|
21
|
+
base.instance_variable_set(:@_lazer_metadata, KeyMetadataStore.new)
|
21
22
|
end
|
22
23
|
|
23
24
|
# The methods to extend the class with.
|
@@ -26,12 +27,7 @@ module LazyLazer
|
|
26
27
|
# @param klass [Class] the subclass
|
27
28
|
# @return [void]
|
28
29
|
def inherited(klass)
|
29
|
-
klass.instance_variable_set(:@
|
30
|
-
end
|
31
|
-
|
32
|
-
# @return [Hash<Symbol, Hash>] the lazer configuration for this model
|
33
|
-
def lazer_metadata
|
34
|
-
@lazer_metadata
|
30
|
+
klass.instance_variable_set(:@_lazer_metadata, @_lazer_metadata.dup)
|
35
31
|
end
|
36
32
|
|
37
33
|
# Define a property.
|
@@ -40,6 +36,7 @@ module LazyLazer
|
|
40
36
|
# @param options [Hash] the options to create the property with
|
41
37
|
# @option options [Boolean] :required (false) whether existence of this property should be
|
42
38
|
# checked on model creation
|
39
|
+
# @option options [Boolean] :identity (false) use this key for equality comparisions
|
43
40
|
# @option options [Boolean] :nil (false) shortcut for default: nil
|
44
41
|
# @option options [Object, Proc] :default the default value to return if not provided
|
45
42
|
# @option options [Symbol] :from (name) the key in the source object to get the property from
|
@@ -57,7 +54,7 @@ module LazyLazer
|
|
57
54
|
# end
|
58
55
|
def property(name, *bool_options, **options)
|
59
56
|
sym_name = name.to_sym
|
60
|
-
@
|
57
|
+
@_lazer_metadata.add(sym_name, KeyMetadata.new(sym_name, *bool_options, **options))
|
61
58
|
define_method(sym_name) { read_attribute(sym_name) }
|
62
59
|
end
|
63
60
|
end
|
@@ -69,13 +66,25 @@ module LazyLazer
|
|
69
66
|
# @return [void]
|
70
67
|
# @raise RequiredAttribute if an attribute marked as required wasn't found
|
71
68
|
def initialize(attributes = {})
|
72
|
-
@_lazer_model = InternalModel.new(self.class.
|
69
|
+
@_lazer_model = InternalModel.new(self.class.instance_variable_get(:@_lazer_metadata), self)
|
73
70
|
@_lazer_model.merge!(attributes)
|
74
71
|
@_lazer_model.verify_required!
|
75
72
|
@_lazer_writethrough = {}
|
76
73
|
@_lazer_loaded = false
|
77
74
|
end
|
78
75
|
|
76
|
+
# Equality check.
|
77
|
+
# @param other [Object] the other object
|
78
|
+
# @return [Boolean]
|
79
|
+
def ==(other)
|
80
|
+
return false if self.class != other.class
|
81
|
+
return super if @_lazer_model.identity_properties.empty?
|
82
|
+
@_lazer_model.identity_properties.each do |key_name|
|
83
|
+
return false if self[key_name] != other[key_name]
|
84
|
+
end
|
85
|
+
true
|
86
|
+
end
|
87
|
+
|
79
88
|
# Converts all the attributes that haven't been converted yet and returns the final hash.
|
80
89
|
# @return [Hash] a hash representation of the model
|
81
90
|
def to_h
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lazy_lazer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Avinash Dwarapu
|
@@ -132,6 +132,7 @@ files:
|
|
132
132
|
- lib/lazy_lazer/errors.rb
|
133
133
|
- lib/lazy_lazer/internal_model.rb
|
134
134
|
- lib/lazy_lazer/key_metadata.rb
|
135
|
+
- lib/lazy_lazer/key_metadata_store.rb
|
135
136
|
- lib/lazy_lazer/version.rb
|
136
137
|
- logo.png
|
137
138
|
homepage: https://github.com/avinashbot/lazy_lazer
|