redis-carpenter 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 36fa517aa7da760815bbab42b65fba356ea4eaf3c910cb9e87747a37d41dad5f
4
+ data.tar.gz: 30abb4f3d5d5931bef970cb8b3d427aa194a1ad73b5b15579fad1c68c9638202
5
+ SHA512:
6
+ metadata.gz: 74856cb76b3a2bc12342d3c49c03ad6117ab5679c0dd68f7623cc6e41b761225f07cf4e0aaacb5fcb76e04cbebce13ba26eb6e7f8723d7391270b784424d9943
7
+ data.tar.gz: 89d5b4dd357f0d9bc7163ed7d34956e5eb8f7cb83b10d8e935436dbd4520f55a2e98beecb4e71a93967e39094c8190a49ed31750371572e4bca72ffc4d684d2a
data/README.md ADDED
@@ -0,0 +1,170 @@
1
+ # Usage
2
+
3
+ Include `Carpenter::Model` to your class and define any params and fields you need.
4
+
5
+ ```ruby
6
+ class Example
7
+ include Carpenter::Model
8
+
9
+ param :id
10
+
11
+ field :first, :integer
12
+ field :second
13
+ end
14
+ ```
15
+
16
+ After this, you can get and set values in redis via the model.
17
+
18
+ ```ruby
19
+ Example[1].first # => nil
20
+ Example[1].first = 10
21
+ Example[1].first # => 10
22
+ Example[2].first # => nil
23
+ ```
24
+
25
+ `field` can be one of:
26
+ - string
27
+ - integer
28
+ - float
29
+ - array
30
+ - json
31
+ - redis_list
32
+
33
+ When defining `field` you can pass some options:
34
+ - default (default: nil)
35
+ Use this to define a default value when getting if such a key does not exist in redis.
36
+
37
+ ```ruby
38
+ class Example
39
+ include Carpenter::Model
40
+
41
+ param :id
42
+
43
+ field :first, :integer, default: 100
44
+ field :second
45
+ end
46
+
47
+ Example[1].first # => 100
48
+ Example[1].first = 10
49
+ Example[1].first # => 10
50
+ Example[2].first # => 100
51
+
52
+ ```
53
+
54
+ - store (default: false)
55
+ Use this to toggle automatic updating of stored value ​​when getting.
56
+
57
+ ```ruby
58
+ class Example
59
+ include Carpenter::Model
60
+
61
+ param :id
62
+
63
+ field :first, :integer, store: true
64
+ field :second
65
+ end
66
+
67
+ ex = Example[1]
68
+ ex.first # => nil
69
+ ex.first = 10
70
+ ex.first # => 10
71
+ ex.first = 20
72
+ ex.first # => 10
73
+ ex.first.reload
74
+ ex.first # => 20
75
+
76
+ ```
77
+
78
+
79
+ # Processing pipeline
80
+
81
+ ## Step 1. Class definition
82
+ 1. Collect all params
83
+ 2. Define class key (use defined params)
84
+ 3. Collect all fields (use class key)
85
+
86
+ ### Class definition example:
87
+
88
+ ```ruby
89
+ class Example
90
+ include Carpenter::Model
91
+
92
+ param :id
93
+ param :id2
94
+
95
+ field :first
96
+ field :second
97
+ end
98
+ ```
99
+
100
+ ### After definition, we have:
101
+ 1. Params list of `[:id, :id2]`
102
+ 2. Key `Example` with params stab like `Example-{id}-{id2}`
103
+ 3. Fields list of `[:first, :second]`. They contain class key and look like `Example-{id}-{id2}:first`
104
+
105
+ ## Step 2. Object initializing
106
+ 1. Save given params
107
+ 2. Define object key (use class key and apply saved params)
108
+ 3. Transfer class fields to object (update present fields key to object key)
109
+
110
+ ### For past example:
111
+
112
+ ```ruby
113
+ Example[id: 1, id2: 5]
114
+ ```
115
+
116
+ ### After initializing, we have:
117
+ 1. Saved Params: `{ id: 1, id2: 5 }`
118
+ 2. Object Key with saved params: `Example-1-5`
119
+ 3. Cloned Fields list (`[:first, :second]`) with updated keys: `Example-1-5:first`
120
+
121
+ ### We can initialize Model with empty params:
122
+
123
+ ```ruby
124
+ Example[id2: 10] #=> This creates key like "Example-@-10"
125
+ ```
126
+
127
+ #### *Any empty params will be replaced to `@`
128
+
129
+ ### We also can initialize Model with positional arguments:
130
+ ```ruby
131
+ Example[1, 5] #=> "Example-1-5"
132
+ # OR
133
+ Example[nil, 10] #=> "Example-@-10"
134
+ # OR
135
+ Example[8] #=> "Example-8-@"
136
+ ```
137
+
138
+ #### In this case, params will be filled in order at definition
139
+
140
+
141
+
142
+ ## Type `redis_list` examples
143
+ ```ruby
144
+
145
+ class Example
146
+ include Carpenter::Model
147
+
148
+ param :id
149
+
150
+ field :redis_list, :redis_list
151
+ end
152
+
153
+ Example[1].redis_list #=> []
154
+ Example[1].redis_list.get #=> []
155
+
156
+ Example[1].redis_list = [1, 2, 3]
157
+ Example[1].redis_list.get #=> [1, 2, 3]
158
+ Example[1].redis_list[1] #=> 2
159
+ Example[1].redis_list[1..2] #=> [2, 3]
160
+ Example[1].redis_list[1...2] #=> [2]
161
+
162
+ Example[1].redis_list[1] = 10
163
+ Example[1].redis_list.get #=> [1, 10, 3]
164
+
165
+ Example[1].redis_list << 200
166
+ Example[1].redis_list.get #=> [1, 10, 3, 200]
167
+
168
+ Example[1].redis_list << { email: 'example@mail.ru' }
169
+ Example[1].redis_list.get #=> [1, 10, 3, 200, { email: 'example@mail.ru' }]
170
+ ```
@@ -0,0 +1,26 @@
1
+ module Carpenter::Model::Accessor
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ base.prepend(PrependMethods)
5
+ end
6
+
7
+ module ClassMethods
8
+ def [](*args, **params)
9
+ instance = new(carpenter_initializing: true)
10
+ instance.initialize_params(*args, **params)
11
+ instance.initialize_key
12
+ instance.initialize_fields
13
+ instance
14
+ end
15
+ end
16
+
17
+ module PrependMethods
18
+ def initialize(*args, **params, &block)
19
+ raise 'Default initialize not accessed' unless params[:carpenter_initializing]
20
+
21
+ params.delete(:carpenter_initializing)
22
+ super(*args, **params, &block)
23
+ end
24
+ end
25
+
26
+ end
@@ -0,0 +1,29 @@
1
+ class Carpenter::Model::AttributesContainer
2
+
3
+ def initialize(container = nil)
4
+ @attributes = {}
5
+ return if container.nil?
6
+
7
+ container.values.each do |value|
8
+ add_attribute(value.dup)
9
+ end
10
+ end
11
+
12
+ def [](name)
13
+ raise "Attribute with name #{name} not exists" unless @attributes[name]
14
+
15
+ @attributes[name]
16
+ end
17
+
18
+ def add_attribute(attribute)
19
+ name = attribute.name
20
+ raise "Attribute with name #{name} already exists" if @attributes[name]
21
+
22
+ @attributes[name] = attribute
23
+ attribute
24
+ end
25
+
26
+ def <<(attribute) = add_attribute(attribute)
27
+ def values = @attributes.values
28
+ def dup = self.class.new(self)
29
+ end
@@ -0,0 +1,22 @@
1
+ require 'carpenter/model/type_caster'
2
+ require 'carpenter/model/strategy'
3
+
4
+ class Carpenter::Model::Field
5
+ attr_reader :name, :options, :key, :type
6
+
7
+ def initialize(field_definition, key)
8
+ @name = field_definition.name
9
+ @type = field_definition.type
10
+ @type_caster = Carpenter::Model::TypeCaster[@type]
11
+ @key = [key, @name].join(':')
12
+ @definition = field_definition
13
+
14
+ options = field_definition.options
15
+ @force = options[:force].nil? ? false : options[:force]
16
+ @store = options[:store].nil? ? false : options[:store]
17
+ @default = options[:default]
18
+ @options = options
19
+
20
+ extend Carpenter::Model::Strategy[@type]
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ require 'carpenter/model/type_caster'
2
+
3
+ class Carpenter::Model::FieldDefinition
4
+ attr_reader :name, :options, :type
5
+
6
+ def initialize(name, type, **options)
7
+ @name = name
8
+ @type = type
9
+ @options = options
10
+ end
11
+
12
+ def dup = self.class.new(name, type, **options)
13
+ def to_s = @name.to_s
14
+ def inspect = @name.inspect
15
+
16
+ def create_instance(key) = Carpenter::Model::Field.new(self, key)
17
+ end
@@ -0,0 +1,48 @@
1
+ require 'carpenter/model/attributes_container'
2
+
3
+ module Carpenter::Model::Modules::Fields
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ def fields = @carpenter__model__fields.values
9
+
10
+ def initialize_fields
11
+ @carpenter__model__fields = Carpenter::Model::AttributesContainer.new
12
+
13
+ self.class.fields.each do |field_definition|
14
+ @carpenter__model__fields << field_definition.create_instance(key)
15
+ end
16
+ end
17
+
18
+ module ClassMethods
19
+ def self.extended(base)
20
+ base.instance_variable_set(:@carpenter__model__fields, Carpenter::Model::AttributesContainer.new)
21
+ end
22
+
23
+ def fields = @carpenter__model__fields.values
24
+
25
+ def inherited(klass)
26
+ klass.instance_variable_set(:@carpenter__model__fields, @carpenter__model__fields.dup)
27
+ super
28
+ end
29
+
30
+ def field(name, type = :string, **options)
31
+ carpenter__model__create_field(name, type, **options)
32
+ carpenter__model__define_field_getter(name)
33
+ carpenter__model__define_field_setter(name)
34
+ end
35
+
36
+ def carpenter__model__create_field(name, type, **options)
37
+ @carpenter__model__fields << Carpenter::Model::FieldDefinition.new(name, type, **options)
38
+ end
39
+
40
+ def carpenter__model__define_field_getter(name)
41
+ define_method(name) { @carpenter__model__fields[name] }
42
+ end
43
+
44
+ def carpenter__model__define_field_setter(name)
45
+ define_method("#{name}=") { |value| @carpenter__model__fields[name].set(value) }
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,30 @@
1
+ module Carpenter::Model::Modules::Key
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ def key = @carpenter__model__key
7
+
8
+ def initialize_key
9
+ @carpenter__model__raw_key = self.class.key.dup
10
+
11
+ key = @carpenter__model__raw_key
12
+ @carpenter__model__params.values.each do |v|
13
+ key = key.gsub("{#{v.name}}", v.value.nil? ? '@' : v.value.to_s)
14
+ end
15
+ @carpenter__model__key = key
16
+ end
17
+
18
+ module ClassMethods
19
+ def self.extended(base)
20
+ base.instance_variable_set(:@carpenter__model__raw_key, base.name)
21
+ end
22
+
23
+ def key = @carpenter__model__key ||= [@carpenter__model__raw_key, @carpenter__model__params.keys.map { |param| "{#{param}}" } ].join('-')
24
+
25
+ def inherited(klass)
26
+ klass.instance_variable_set(:@carpenter__model__raw_key, klass.name)
27
+ super
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,53 @@
1
+ module Carpenter::Model::Modules::Params
2
+ def self.included(base)
3
+ base.extend(ClassMethods)
4
+ end
5
+
6
+ def params = @carpenter__model__params.values
7
+
8
+ def initialize_params(*args, **params)
9
+ raise 'Ambiguous attributes' if args.any? && params.any?
10
+
11
+ @carpenter__model__params = {}
12
+ params = self.class.instance_variable_get(:@carpenter__model__params).keys.zip(args).to_h if params.empty?
13
+
14
+ self.class.instance_variable_get(:@carpenter__model__params).each do |param_name, param_value|
15
+ param = param_value.dup
16
+ param.value = params[param_name]
17
+ raise "Invalid param" unless param.valid?
18
+
19
+ @carpenter__model__params[param_name] = param
20
+ end
21
+ end
22
+
23
+ module ClassMethods
24
+ def self.extended(base)
25
+ base.instance_variable_set(:@carpenter__model__params, {})
26
+ end
27
+
28
+ def params = @carpenter__model__params.values
29
+
30
+ def inherited(klass)
31
+ klass.instance_variable_set(:@carpenter__model__params, @carpenter__model__params.dup)
32
+ super
33
+ end
34
+
35
+ def param(name, **options)
36
+ carpenter__model__create_param(name, **options)
37
+ carpenter__model__define_param_getter(name)
38
+ carpenter__model__define_param_setter(name)
39
+ end
40
+
41
+ def carpenter__model__create_param(name, **options)
42
+ @carpenter__model__params[name] = Carpenter::Model::Param.new(name, nil, **options)
43
+ end
44
+
45
+ def carpenter__model__define_param_getter(name)
46
+ define_method(name) { @carpenter__model__params[name] }
47
+ end
48
+
49
+ def carpenter__model__define_param_setter(name)
50
+ define_method("#{name}=") { |value| @carpenter__model__params[name].set(value) }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,5 @@
1
+ require 'carpenter/model/modules/key'
2
+ require 'carpenter/model/modules/params'
3
+ require 'carpenter/model/modules/fields'
4
+
5
+ module Carpenter::Model::Modules; end
@@ -0,0 +1,20 @@
1
+ class Carpenter::Model::Param
2
+ attr_reader :name, :options
3
+ attr_accessor :value
4
+
5
+ def initialize(name, value, **options)
6
+ @name = name
7
+ @value = value
8
+ @options = options
9
+ end
10
+
11
+ def dup
12
+ self.class.new(name, value, **options)
13
+ end
14
+
15
+ def valid?
16
+ return false if @options[:force] && @value.nil?
17
+
18
+ true
19
+ end
20
+ end
@@ -0,0 +1,22 @@
1
+ module Carpenter::Model::Strategy::Array
2
+ include Carpenter::Model::Strategy::Base
3
+
4
+ def [](index)
5
+ data = get
6
+ data[index]
7
+ end
8
+
9
+ def []=(index, value)
10
+ data = get
11
+ data[index] = value
12
+ set(data)
13
+ value
14
+ end
15
+
16
+ def <<(value)
17
+ data = get
18
+ data << value
19
+ set(data)
20
+ value
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ module Carpenter::Model::Strategy::Base
2
+
3
+ def value
4
+ if @store
5
+ @value ||= casted_value
6
+ else
7
+ casted_value
8
+ end
9
+ end
10
+
11
+ def raw_value = Carpenter::REDIS&.get(@key)
12
+ def casted_value = @type_caster.cast(raw_value) || @default
13
+
14
+ def reload = (@value = casted_value if @store)
15
+
16
+ def get = value
17
+
18
+ def set(value)
19
+ Carpenter::REDIS&.set(@key, @type_caster.stringify(value))
20
+ end
21
+
22
+ def dup = self.class.new(@definition, @key)
23
+ def empty? = (value = get) ? value.empty? : true
24
+ def to_s = get.to_s
25
+ def inspect = get.inspect
26
+ def coerce(other) = [other, get]
27
+ end
@@ -0,0 +1,15 @@
1
+ module Carpenter::Model::Strategy::JSON
2
+ include Carpenter::Model::Strategy::Base
3
+
4
+ def [](key)
5
+ data = get
6
+ data[key]
7
+ end
8
+
9
+ def []=(key, value)
10
+ data = get
11
+ data[key] = value
12
+ set(data)
13
+ value
14
+ end
15
+ end
@@ -0,0 +1,50 @@
1
+ module Carpenter::Model::Strategy::RedisList
2
+
3
+ def value(first, last)
4
+ if @store
5
+ @value ||= casted_value(first, last)
6
+ else
7
+ casted_value(first, last)
8
+ end
9
+ end
10
+
11
+ def raw_value(first, last) = Carpenter::REDIS&.lrange(@key, first, last)
12
+ def casted_value(first, last) = raw_value(first, last).map { |v| @type_caster.cast(v) } || @default
13
+
14
+ def reload(first = 0, last = -1) = (@value = casted_value(first, last) if @store)
15
+
16
+ def get(first = 0, last = -1) = value(first, last)
17
+
18
+ def [](range)
19
+ case range
20
+ when Integer
21
+ get(range, range).first
22
+ when Range
23
+ first = range.begin
24
+ offset = range.exclude_end? ? 1 : 0
25
+ last = range.end
26
+ last = last - offset if last
27
+ get(first, last)
28
+ else
29
+ raise "Unexpected type: #{range.class}"
30
+ end
31
+ end
32
+
33
+ def []=(index, value)
34
+ Carpenter::REDIS&.lset(@key, index, @type_caster.stringify(value))
35
+ end
36
+
37
+ def set(value)
38
+ value = value.is_a?(Array) ? value.map { |v| @type_caster.stringify(v) } : @type_caster.stringify(value)
39
+
40
+ Carpenter::REDIS&.del(@key)
41
+ Carpenter::REDIS&.rpush(@key, value)
42
+ end
43
+
44
+ def <<(value) = Carpenter::REDIS&.rpush(@key, @type_caster.stringify(value))
45
+ def dup = self.class.new(@definition, @key)
46
+ def empty? = (value = get) ? value.empty? : true
47
+ def to_s = get.to_s
48
+ def inspect = get.inspect
49
+ def coerce(other) = [other, get]
50
+ end
@@ -0,0 +1,27 @@
1
+ # require 'carpenter/model/strategy/string'
2
+ # require 'carpenter/model/strategy/integer'
3
+ require 'carpenter/model/strategy/base'
4
+ require 'carpenter/model/strategy/array'
5
+ require 'carpenter/model/strategy/json'
6
+ require 'carpenter/model/strategy/redis_list'
7
+
8
+ class Carpenter
9
+ module Model
10
+ module Strategy
11
+
12
+ STRATEGIES = {
13
+ base: Base,
14
+ string: Base,
15
+ integer: Base,
16
+ float: Base,
17
+ array: Array,
18
+ json: JSON,
19
+
20
+ redis_list: RedisList
21
+ }.freeze
22
+
23
+ def self.[](type) = type.is_a?(Symbol) ? STRATEGIES[type] || STRATEGIES[:base] : type
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,4 @@
1
+ class Carpenter::Model::TypeCaster::Array
2
+ def self.stringify(data) = data.to_json
3
+ def self.cast(data) = data ? JSON.parse(data, symbolize_names: true) : nil
4
+ end
@@ -0,0 +1,4 @@
1
+ class Carpenter::Model::TypeCaster::Float
2
+ def self.stringify(data) = data.to_s
3
+ def self.cast(data) = data ? data.to_f : nil
4
+ end
@@ -0,0 +1,4 @@
1
+ class Carpenter::Model::TypeCaster::Integer
2
+ def self.stringify(data) = data.to_s
3
+ def self.cast(data) = data ? data.to_i : nil
4
+ end
@@ -0,0 +1,4 @@
1
+ class Carpenter::Model::TypeCaster::JSON
2
+ def self.stringify(data) = data.to_json
3
+ def self.cast(data) = data ? JSON.parse(data, symbolize_names: true) : nil
4
+ end
@@ -0,0 +1,4 @@
1
+ class Carpenter::Model::TypeCaster::String
2
+ def self.stringify(data) = data.to_s
3
+ def self.cast(data) = data
4
+ end
@@ -0,0 +1,21 @@
1
+ require 'carpenter/model/type_caster/string'
2
+ require 'carpenter/model/type_caster/integer'
3
+ require 'carpenter/model/type_caster/float'
4
+ require 'carpenter/model/type_caster/array'
5
+ require 'carpenter/model/type_caster/json'
6
+
7
+ class Carpenter::Model::TypeCaster
8
+
9
+ CASTERS = {
10
+ base: String,
11
+ string: String,
12
+ integer: Integer,
13
+ float: Float,
14
+ array: Array,
15
+ json: JSON,
16
+
17
+ redis_list: JSON
18
+ }.freeze
19
+
20
+ def self.[](type) = type.is_a?(Symbol) ? CASTERS[type] || CASTERS[:base] : type
21
+ end
@@ -0,0 +1,27 @@
1
+ require 'carpenter/model/field_definition'
2
+ require 'carpenter/model/field'
3
+ require 'carpenter/model/param'
4
+
5
+ require 'carpenter/model/modules'
6
+ require 'carpenter/model/accessor'
7
+
8
+ module Carpenter::Model
9
+
10
+ def self.included(base)
11
+ base.extend(ClassMethods)
12
+
13
+ base.include(Modules::Params)
14
+ #=>
15
+ base.include(Modules::Key)
16
+ #=>
17
+ base.include(Modules::Fields)
18
+
19
+ base.include(Accessor)
20
+ end
21
+
22
+ module ClassMethods
23
+ def self.extended(base); end
24
+
25
+ end
26
+
27
+ end
@@ -0,0 +1,6 @@
1
+ class Carpenter
2
+ REDIS = nil
3
+
4
+ def self.setup_redis(**options) = const_set(:REDIS, Redis.new(**options))
5
+ def self.use_redis(redis) = const_set(:REDIS, redis)
6
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Carpenter
4
+ VERSION = '0.0.1'
5
+ end
data/lib/carpenter.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'json'
2
+
3
+ require 'struct_declaration'
4
+
5
+ require 'carpenter/version'
6
+ require 'carpenter/redis'
7
+
8
+ require 'carpenter/model'
9
+
10
+ class Carpenter; end
@@ -0,0 +1,31 @@
1
+ class Carpenter
2
+ module Model
3
+ class AttributesContainer; end
4
+
5
+ class ParamDefinition; end
6
+ class Param; end
7
+
8
+ class Field; end
9
+ module Accessor; end
10
+
11
+ module Modules
12
+ module Params; end
13
+ module Key; end
14
+ module Fields; end
15
+ end
16
+
17
+ class TypeCaster
18
+ class String; end
19
+ class Integer; end
20
+ class Float; end
21
+ class Array; end
22
+ class JSON; end
23
+ end
24
+
25
+ module Strategy
26
+ module Base; end
27
+ module Array; end
28
+ module JSON; end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,100 @@
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
+ # The generated `.rspec` file contains `--require spec_helper` which will cause
4
+ # this file to always be loaded, without a need to explicitly require it in any
5
+ # files.
6
+ #
7
+ # Given that it is always loaded, you are encouraged to keep this file as
8
+ # light-weight as possible. Requiring heavyweight dependencies from this file
9
+ # will add to the boot time of your test suite on EVERY test run, even for an
10
+ # individual file that may not need all of that loaded. Instead, consider making
11
+ # a separate helper file that requires the additional dependencies and performs
12
+ # the additional setup, and require it from the spec files that actually need
13
+ # it.
14
+ #
15
+ # See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
16
+ require 'rspec'
17
+
18
+ RSpec.configure do |config|
19
+ # rspec-expectations config goes here. You can use an alternate
20
+ # assertion/expectation library such as wrong or the stdlib/minitest
21
+ # assertions if you prefer.
22
+ config.expect_with :rspec do |expectations|
23
+ # This option will default to `true` in RSpec 4. It makes the `description`
24
+ # and `failure_message` of custom matchers include text for helper methods
25
+ # defined using `chain`, e.g.:
26
+ # be_bigger_than(2).and_smaller_than(4).description
27
+ # # => "be bigger than 2 and smaller than 4"
28
+ # ...rather than:
29
+ # # => "be bigger than 2"
30
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
31
+ end
32
+
33
+ # rspec-mocks config goes here. You can use an alternate test double
34
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
35
+ config.mock_with :rspec do |mocks|
36
+ # Prevents you from mocking or stubbing a method that does not exist on
37
+ # a real object. This is generally recommended, and will default to
38
+ # `true` in RSpec 4.
39
+ mocks.verify_partial_doubles = true
40
+ end
41
+
42
+ # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
43
+ # have no way to turn it off -- the option exists only for backwards
44
+ # compatibility in RSpec 3). It causes shared context metadata to be
45
+ # inherited by the metadata hash of host groups and examples, rather than
46
+ # triggering implicit auto-inclusion in groups with matching metadata.
47
+ config.shared_context_metadata_behavior = :apply_to_host_groups
48
+
49
+ # The settings below are suggested to provide a good initial experience
50
+ # with RSpec, but feel free to customize to your heart's content.
51
+ =begin
52
+ # This allows you to limit a spec run to individual examples or groups
53
+ # you care about by tagging them with `:focus` metadata. When nothing
54
+ # is tagged with `:focus`, all examples get run. RSpec also provides
55
+ # aliases for `it`, `describe`, and `context` that include `:focus`
56
+ # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
57
+ config.filter_run_when_matching :focus
58
+
59
+ # Allows RSpec to persist some state between runs in order to support
60
+ # the `--only-failures` and `--next-failure` CLI options. We recommend
61
+ # you configure your source control system to ignore this file.
62
+ config.example_status_persistence_file_path = "spec/examples.txt"
63
+
64
+ # Limits the available syntax to the non-monkey patched syntax that is
65
+ # recommended. For more details, see:
66
+ # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/
67
+ config.disable_monkey_patching!
68
+
69
+ # This setting enables warnings. It's recommended, but in some cases may
70
+ # be too noisy due to issues in dependencies.
71
+ config.warnings = true
72
+
73
+ # Many RSpec users commonly either run the entire suite or an individual
74
+ # file, and it's useful to allow more verbose output when running an
75
+ # individual spec file.
76
+ if config.files_to_run.one?
77
+ # Use the documentation formatter for detailed output,
78
+ # unless a formatter has already been configured
79
+ # (e.g. via a command-line flag).
80
+ config.default_formatter = "doc"
81
+ end
82
+
83
+ # Print the 10 slowest examples and example groups at the
84
+ # end of the spec run, to help surface which specs are running
85
+ # particularly slow.
86
+ config.profile_examples = 10
87
+
88
+ # Run specs in random order to surface order dependencies. If you find an
89
+ # order dependency and want to debug it, you can fix the order by providing
90
+ # the seed, which is printed after each run.
91
+ # --seed 1234
92
+ config.order = :random
93
+
94
+ # Seed global randomization in this process using the `--seed` CLI option.
95
+ # Setting this allows you to use `--seed` to deterministically reproduce
96
+ # test failures related to randomization by passing the same `--seed` value
97
+ # as the one that triggered the failure.
98
+ Kernel.srand config.seed
99
+ =end
100
+ end
@@ -0,0 +1,35 @@
1
+
2
+ class Example
3
+ include Carpenter::Model
4
+
5
+ param :id
6
+
7
+ field :array, :redis_list
8
+ end
9
+
10
+ RSpec.describe Carpenter do
11
+
12
+ context 'test redis_list type' do
13
+
14
+ it 'getter/setter' do
15
+ expect(Example[1].array.get).to eq([])
16
+
17
+ Example[1].array = [1, 2, 3]
18
+
19
+ expect(Example[1].array.get).to eq([1, 2, 3])
20
+ expect(Example[1].array[1]).to eq(2)
21
+ expect(Example[1].array[1..2]).to eq([2, 3])
22
+ expect(Example[1].array[1...2]).to eq([2])
23
+
24
+ Example[1].array[1] = 10
25
+ expect(Example[1].array.get).to eq([1, 10, 3])
26
+
27
+ expect { Example[1].array[:a] }.to raise_error(StandardError)
28
+ Example[1].array << 200
29
+ expect(Example[1].array.get).to eq([1, 10, 3, 200])
30
+ Example[1].array << {email: 'example@mail.ru'}
31
+ expect(Example[1].array.get).to eq([1, 10, 3, 200, {email: 'example@mail.ru'}])
32
+ end
33
+
34
+ end
35
+ end
data/spec/tests.rb ADDED
@@ -0,0 +1,172 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+
3
+ require './spec/spec_helper'
4
+ require 'carpenter'
5
+ require 'fakeredis'
6
+
7
+ Carpenter.setup_redis
8
+
9
+ class Res
10
+ include Carpenter::Model
11
+
12
+ param :id
13
+
14
+ field :first, :integer, force: true
15
+ field :second, :string, store: true
16
+ field :array, :array, default: []
17
+ field :json, :json, default: {}
18
+
19
+ end
20
+
21
+ class Res2 < Res
22
+
23
+ param :id2
24
+
25
+ field :last, :integer
26
+
27
+ end
28
+
29
+ RSpec.configure do |config|
30
+ config.before(:each) do
31
+ Carpenter::REDIS.flushdb
32
+ end
33
+ end
34
+
35
+ RSpec.describe Carpenter do
36
+ context 'test base accessors' do
37
+ it 'test keys' do
38
+ expect(Res.key).to eq('Res-{id}')
39
+ expect(Res2.key).to eq('Res2-{id}-{id2}')
40
+ end
41
+
42
+ it 'test params' do
43
+ expect(Res.params.map(&:name)).to eq([:id])
44
+ expect(Res2.params.map(&:name)).to eq(%i[id id2])
45
+ end
46
+
47
+ it 'test fields' do
48
+ expect(Res.fields.map(&:name)).to eq(%i[first second array json])
49
+ expect(Res2.fields.map(&:name)).to eq(%i[first second array json last])
50
+ end
51
+ end
52
+
53
+ context 'test instances' do
54
+ it 'test keys' do
55
+ expect(Res[id: 1].key).to eq('Res-1')
56
+ expect(Res[2].key).to eq('Res-2')
57
+ expect(Res[].key).to eq('Res-@')
58
+ expect(Res2[id: 2].key).to eq('Res2-2-@')
59
+ expect(Res2[id2: 2].key).to eq('Res2-@-2')
60
+ expect(Res2[0].key).to eq('Res2-0-@')
61
+ expect(Res2[8, 10].key).to eq('Res2-8-10')
62
+ expect(Res2[id: 3].key).to eq('Res2-3-@')
63
+ expect(Res2[id: 5, id2: 6].key).to eq('Res2-5-6')
64
+ end
65
+
66
+ it 'test params' do
67
+ expect(Res[1].params.map(&:name)).to eq([:id])
68
+ expect(Res2[10].params.map(&:name)).to eq(%i[id id2])
69
+ end
70
+
71
+ it 'test fields' do
72
+ expect(Res[1].fields.map(&:name)).to eq(%i[first second array json])
73
+ expect(Res2[10].fields.map(&:name)).to eq(%i[first second array json last])
74
+ end
75
+
76
+ it 'test field keys' do
77
+ expect(Res[1].first.key).to eq('Res-1:first')
78
+ expect(Res[10].second.key).to eq('Res-10:second')
79
+ expect(Res2[10].first.key).to eq('Res2-10-@:first')
80
+ expect(Res2[10, 1].last.key).to eq('Res2-10-1:last')
81
+ end
82
+
83
+ it 'test getters/setters' do
84
+ expect(Res[1].first.get).to eq(nil)
85
+ expect(Res[1].first.inspect).to eq('nil')
86
+ expect(Res[1].first.to_s).to eq('')
87
+
88
+ Res[1].first = 10
89
+
90
+ expect(Res[1].first.get).to eq(10)
91
+ expect(Res[1].first.inspect).to eq('10')
92
+ expect(Res[1].first.to_s).to eq('10')
93
+
94
+ expect(Res[1].second.get).to eq(nil)
95
+ expect(Res[2].first.get).to eq(nil)
96
+
97
+ Res[1].second.set(100)
98
+
99
+ expect(Res[1].second.get).to eq('100')
100
+ expect(Res[2].first.get).to eq(nil)
101
+ expect(Res[2].second.get).to eq(nil)
102
+
103
+ # =======
104
+
105
+ expect(Res2[1].first.get).to eq(nil)
106
+ expect(Res2[1].first.inspect).to eq('nil')
107
+ expect(Res2[1].first.to_s).to eq('')
108
+
109
+ Res2[1].first = 10
110
+
111
+ expect(Res2[1].first.get).to eq(10)
112
+ expect(Res2[1].first.inspect).to eq('10')
113
+ expect(Res2[1].first.to_s).to eq('10')
114
+
115
+ expect(Res2[1].second.get).to eq(nil)
116
+ expect(Res2[2].first.get).to eq(nil)
117
+
118
+ Res2[1].second.set(100)
119
+
120
+ expect(Res2[1].second.get).to eq('100')
121
+ expect(Res2[2].first.get).to eq(nil)
122
+ expect(Res2[2].second.get).to eq(nil)
123
+
124
+ end
125
+
126
+ it 'test options' do
127
+ expect(Res[id: 1].second.options.keys).to eq([:store])
128
+ expect(Res2[1, 2].last.options.keys).to eq([])
129
+ end
130
+
131
+ it 'test reload option' do
132
+ example = Res[id: 1]
133
+ expect(example.second.get).to eq(nil)
134
+
135
+ example.second = 100
136
+ expect(example.second.get).to eq('100')
137
+
138
+ example.second = 1000
139
+ expect(example.second.get).to eq('100')
140
+
141
+ example.second.reload
142
+ expect(example.second.get).to eq('1000')
143
+ end
144
+
145
+ it 'test array field' do
146
+ expect(Res[1].array.get).to eq([])
147
+ Res[1].array = [1, 2, 3]
148
+ expect(Res[1].array.get).to eq([1, 2, 3])
149
+ Res[1].array[1] = 10
150
+ expect(Res[1].array.get).to eq([1, 10, 3])
151
+ expect(Res[1].array[1]).to eq(10)
152
+ expect(Res[1].array[-1]).to eq(3)
153
+ expect(Res[1].array[10]).to eq(nil)
154
+ expect { Res[1].array[:a] }.to raise_error(TypeError)
155
+ Res[1].array << 200
156
+ expect(Res[1].array.get).to eq([1, 10, 3, 200])
157
+ end
158
+
159
+ it 'test json field' do
160
+ expect(Res[1].json.get).to eq({})
161
+ Res[1].json = {a: 1}
162
+ expect(Res[1].json.get).to eq({a: 1})
163
+ Res[1].json[:b] = 10
164
+ expect(Res[1].json.get).to eq({a: 1, b: 10})
165
+ expect(Res[1].json[:b]).to eq(10)
166
+ expect(Res[1].json[:ab]).to eq(nil)
167
+ expect(Res[1].json[0]).to eq(nil)
168
+ end
169
+ end
170
+ end
171
+
172
+ require './spec/test_redis_list'
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: redis-carpenter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Abvgdeyoj
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2025-04-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.7'
27
+ description: Simple ORM for Redis
28
+ email:
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - README.md
34
+ - lib/carpenter.rb
35
+ - lib/carpenter/model.rb
36
+ - lib/carpenter/model/accessor.rb
37
+ - lib/carpenter/model/attributes_container.rb
38
+ - lib/carpenter/model/field.rb
39
+ - lib/carpenter/model/field_definition.rb
40
+ - lib/carpenter/model/modules.rb
41
+ - lib/carpenter/model/modules/fields.rb
42
+ - lib/carpenter/model/modules/key.rb
43
+ - lib/carpenter/model/modules/params.rb
44
+ - lib/carpenter/model/param.rb
45
+ - lib/carpenter/model/strategy.rb
46
+ - lib/carpenter/model/strategy/array.rb
47
+ - lib/carpenter/model/strategy/base.rb
48
+ - lib/carpenter/model/strategy/json.rb
49
+ - lib/carpenter/model/strategy/redis_list.rb
50
+ - lib/carpenter/model/type_caster.rb
51
+ - lib/carpenter/model/type_caster/array.rb
52
+ - lib/carpenter/model/type_caster/float.rb
53
+ - lib/carpenter/model/type_caster/integer.rb
54
+ - lib/carpenter/model/type_caster/json.rb
55
+ - lib/carpenter/model/type_caster/string.rb
56
+ - lib/carpenter/redis.rb
57
+ - lib/carpenter/version.rb
58
+ - lib/struct_declaration.rb
59
+ - spec/spec_helper.rb
60
+ - spec/test_redis_list.rb
61
+ - spec/tests.rb
62
+ homepage:
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: 3.1.0
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 1.3.6
80
+ requirements: []
81
+ rubygems_version: 3.3.3
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: Simple ORM for Redis
85
+ test_files:
86
+ - spec/spec_helper.rb
87
+ - spec/test_redis_list.rb
88
+ - spec/tests.rb