Class: Flowlink::FieldMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/flowlink_data/field_method.rb

Overview

Represents a property of a domain object. For example, a price in a product. If you need to change how one of these is handled in a specific product, then you can use it as a #new argument for a class which inherits from Flow:ObjectBase, and invokes super in .initialize

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method_name, *args) ⇒ FieldMethod

Returns a new instance of FieldMethod.



20
21
22
23
24
25
# File 'lib/flowlink_data/field_method.rb', line 20

def initialize(method_name, *args)
  @method_name  = method_name.to_sym
  @args         = args.to_a.flatten
  @block, @args = @args.partition { |arg| arg.is_a? Proc }
  @block        = @block[0]
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/flowlink_data/field_method.rb', line 7

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/flowlink_data/field_method.rb', line 7

def block
  @block
end

#method_nameObject (readonly)

Returns the value of attribute method_name.



7
8
9
# File 'lib/flowlink_data/field_method.rb', line 7

def method_name
  @method_name
end

Class Method Details

.merge(overrides, original) ⇒ Object



16
17
18
# File 'lib/flowlink_data/field_method.rb', line 16

def self.merge(overrides, original)
  overrides.inject(original) { |a, e| e.merge(a) }
end

.multi_new(methods) ⇒ Object



9
10
11
12
13
14
# File 'lib/flowlink_data/field_method.rb', line 9

def self.multi_new(methods)
  methods.map do |m|
    m = [m].flatten
    FieldMethod.new(m.shift, m)
  end
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
# File 'lib/flowlink_data/field_method.rb', line 27

def ==(other)
  return false unless other.is_a?(self.class)
  to_a == other.to_a
end

#merge(list) ⇒ Object

rename to #override, #hard_merge, or add #override alias?



32
33
34
35
36
37
# File 'lib/flowlink_data/field_method.rb', line 32

def merge(list) # rename to #override, #hard_merge, or add #override alias?
  # This will put itself into a list of other FieldMethods and overwrite
  # an existing FM with the same name
  list.delete_if { |o_fm| o_fm.method_name == method_name }
  list << self
end

#send_to(sendable) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/flowlink_data/field_method.rb', line 43

def send_to(sendable)
  # we can't splat procs, so this is necessary
  # TODO: use #to_a and reduce cases/enforce SRP on regular arg assembler
  case
  when block && args.empty?
    sendable.send(method_name, &block)
  when block && !args.empty?
    sendable.send(method_name, *args, &block)
  when !block && args.empty?
    sendable.send(method_name)
  when !block && !args.empty?
    sendable.send(method_name, *args)
  end
end

#to_aObject



39
40
41
# File 'lib/flowlink_data/field_method.rb', line 39

def to_a
  [method_name] + args + (@block.nil? ? [] : [@block])
end