flowlink_data 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 69925cf26513fecb4c9d171e95fdbeff97f83073
4
- data.tar.gz: 606610ac873e22d40503da6c6481e1bcc2e73640
3
+ metadata.gz: 69d8e2a66fab063ec164838c1cf7ad92f3964397
4
+ data.tar.gz: 36ca7c3a8bd7bcee4ecf24962d22e4a2cb95825d
5
5
  SHA512:
6
- metadata.gz: 062f10a60be77181135c17995fa6d4041f99f8b2fdb1d4155e232b3a35a65d0a0a7a4777be7199670fed660c511c2180adeb48c8c47aeec70453dbe0bbbf9f09
7
- data.tar.gz: 37ce125cd1402571358075770623f4b96ad41b8240a13a5a3d832702a8945d1794d6c9be7526862017ab4365eaffa1dddbc58ff2690fac3d00c0acefc2fc2de1
6
+ metadata.gz: 2a7a5217646e72f552e4568e122a9fa33cfd0db5c823afa198c9ccc777d1cfe31de3f2b2cd2a807ee8055bb7633bbcd544ddddcd20c7336b07962dba12378a2f
7
+ data.tar.gz: aaa29d3eeb819ddc98ca89795c8fb082c99f680c360942bdf57603659fef670d6df2d49d6f20b55d41481925b1b79493905737f45c83794e376a763a71aba37e
@@ -0,0 +1,58 @@
1
+ module Flowlink
2
+ # Represents a property of a domain object. For example, a price in a product.
3
+ # If you need to change how one of these is handled in a specific product,
4
+ # then you can use it as a #new argument for a class which inherits from
5
+ # Flowlink::ObjectBase, and invokes super in .initialize
6
+ class FieldMethod
7
+ attr_reader :method_name, :args, :block
8
+
9
+ def self.multi_new(methods)
10
+ methods.map do |m|
11
+ m = [m].flatten
12
+ FieldMethod.new(m.shift, m)
13
+ end
14
+ end
15
+
16
+ def self.merge(overrides, original)
17
+ overrides.inject(original) { |a, e| e.merge(a) }
18
+ end
19
+
20
+ def initialize(method_name, *args)
21
+ @method_name = method_name.to_sym
22
+ @args = args.to_a.flatten
23
+ @block, @args = @args.partition { |arg| arg.is_a? Proc }
24
+ @block = @block[0]
25
+ end
26
+
27
+ def ==(other)
28
+ return false unless other.is_a?(self.class)
29
+ to_a == other.to_a
30
+ end
31
+
32
+ def merge(list) # rename to #override, #hard_merge, or add #override alias?
33
+ # This will put itself into a list of other FieldMethods and overwrite
34
+ # an existing FM with the same name
35
+ list.delete_if { |o_fm| o_fm.method_name == method_name }
36
+ list << self
37
+ end
38
+
39
+ def to_a
40
+ [method_name] + args + (@block.nil? ? [] : [@block])
41
+ end
42
+
43
+ def send_to(sendable)
44
+ # we can't splat procs, so this is necessary
45
+ # TODO: use #to_a and reduce cases/enforce SRP on regular arg assembler
46
+ case
47
+ when block && args.empty?
48
+ sendable.send(method_name, &block)
49
+ when block && !args.empty?
50
+ sendable.send(method_name, *args, &block)
51
+ when !block && args.empty?
52
+ sendable.send(method_name)
53
+ when !block && !args.empty?
54
+ sendable.send(method_name, *args)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -1,62 +1,24 @@
1
- class FieldMethod
2
- attr_reader :method_name, :args, :block
3
-
4
- def self.multi_new(methods)
5
- methods.map do |m|
6
- m = [m].flatten
7
- FieldMethod.new(m.shift, m)
8
- end
9
- end
10
-
11
- def self.merge(overrides, original)
12
- overrides.inject(original) { |a, e| e.merge(a) }
13
- end
14
-
15
- def initialize(method_name, *args)
16
- @method_name = method_name.to_sym
17
- @args = args.to_a.flatten
18
- @block, @args = @args.partition { |arg| arg.is_a? Proc }
19
- @block = @block[0]
20
- end
21
-
22
- def merge(list)
23
- # This will put itself into a list of other FieldMethods and overwrite
24
- # an existing FM with the same name
25
- list.delete_if { |o_fm| o_fm.method_name == method_name }
26
- list << self
27
- end
28
-
29
- def to_a
30
- [method_name] + args
31
- end
32
-
33
- def send_to(sendable)
34
- # can't just splat because procs have to be treated with kids gloves >:/
35
- # TODO: use #to_a and reduce cases/enforce SRP on regular arg assembley
36
- case
37
- when !block && !args.empty?
38
- sendable.send(method_name, *args)
39
- when block && args.empty?
40
- sendable.send(method_name, &block)
41
- when block && !args.empty?
42
- sendable.send(method_name, *args, &block)
43
- when !block && args.empty?
44
- sendable.send(method_name)
45
- end
46
- end
47
- end
1
+ require_relative '../field_method'
48
2
 
49
3
  module Flowlink
50
4
  class ObjectBase
51
- # Base class for any Flowlink objects. Child classes should implement
5
+ # Base class for any Flowlink objects. Children should implement
52
6
  # self.fields internally.
53
7
 
54
- def to_hash(*overrides)
55
- overrides = FieldMethod.multi_new(overrides)
56
- defaults = FieldMethod.multi_new(fields)
57
- f_methods = FieldMethod.merge(overrides, defaults)
8
+ attr_accessor :f_methods
9
+
10
+ def initialize(*overrides)
11
+ unless overrides.flatten!.all? { |arg| arg.is_a?(FieldMethod) }
12
+ # using array arguments isn't supported anymore
13
+ fail ArgumentError, 'Arguments need to be FieldMethods'
14
+ end
15
+
16
+ defaults = FieldMethod.multi_new(fields)
17
+ @f_methods = FieldMethod.merge(overrides, defaults)
18
+ end
58
19
 
59
- Hash[f_methods.map { |fm| [fm.method_name.to_s, fm.send_to(self)] }]
20
+ def to_hash
21
+ Hash[@f_methods.map { |fm| [fm.method_name.to_s, fm.send_to(self)] }]
60
22
  end
61
23
 
62
24
  alias to_message to_hash
@@ -1,3 +1,3 @@
1
1
  module Flowlink
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flowlink_data
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cooper LeBrun
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-22 00:00:00.000000000 Z
11
+ date: 2016-07-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  A framework for getting Flowlink objects from other sources. For example:
@@ -25,6 +25,7 @@ extensions: []
25
25
  extra_rdoc_files: []
26
26
  files:
27
27
  - lib/flowlink_data.rb
28
+ - lib/flowlink_data/field_method.rb
28
29
  - lib/flowlink_data/objectbase.rb
29
30
  - lib/flowlink_data/product.rb
30
31
  - lib/flowlink_data/version.rb