Class: RakeFactory::Parameter

Inherits:
Object
  • Object
show all
Defined in:
lib/rake_factory/parameter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options) ⇒ Parameter

Returns a new instance of Parameter.



10
11
12
13
14
15
16
17
# File 'lib/rake_factory/parameter.rb', line 10

def initialize(name, options)
  @name = name
  @default = options[:default]
  @required = options[:required] || false
  @transform = options[:transform] || ->(x) { x }
  @configurable =
    options[:configurable].nil? ? true : !!options[:configurable]
end

Instance Attribute Details

#configurableObject (readonly)

Returns the value of attribute configurable.



8
9
10
# File 'lib/rake_factory/parameter.rb', line 8

def configurable
  @configurable
end

#defaultObject

Returns the value of attribute default.



7
8
9
# File 'lib/rake_factory/parameter.rb', line 7

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/rake_factory/parameter.rb', line 8

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



8
9
10
# File 'lib/rake_factory/parameter.rb', line 8

def required
  @required
end

#transformObject (readonly)

Returns the value of attribute transform.



8
9
10
# File 'lib/rake_factory/parameter.rb', line 8

def transform
  @transform
end

Instance Method Details

#apply_default_to(instance) ⇒ Object



44
45
46
# File 'lib/rake_factory/parameter.rb', line 44

def apply_default_to(instance)
  instance.send(writer_method, @default) unless @default.nil?
end

#configurable?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/rake_factory/parameter.rb', line 52

def configurable?
  @configurable
end

#define_on(klass) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rake_factory/parameter.rb', line 31

def define_on(klass)
  parameter = self
  klass.class_eval do
    define_method parameter.writer_method do |value|
      parameter.set(self, value)
    end

    define_method parameter.reader_method do
      parameter.get(self)
    end
  end
end

#dissatisfied_by?(instance) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/rake_factory/parameter.rb', line 56

def dissatisfied_by?(instance)
  @required && read_from(instance).nil?
end

#get(target) ⇒ Object



68
69
70
71
72
# File 'lib/rake_factory/parameter.rb', line 68

def get(target)
  stored_value = target.instance_variable_get(instance_variable)
  resolved_value = Values.resolve(stored_value).evaluate([target])
  transform.call(resolved_value)
end

#instance_variableObject



27
28
29
# File 'lib/rake_factory/parameter.rb', line 27

def instance_variable
  "@#{name}"
end

#read_from(instance) ⇒ Object



48
49
50
# File 'lib/rake_factory/parameter.rb', line 48

def read_from(instance)
  instance.send(reader_method)
end

#reader_methodObject



23
24
25
# File 'lib/rake_factory/parameter.rb', line 23

def reader_method
  name
end

#satisfied_by?(instance) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/rake_factory/parameter.rb', line 60

def satisfied_by?(instance)
  !dissatisfied_by?(instance)
end

#set(target, value) ⇒ Object



64
65
66
# File 'lib/rake_factory/parameter.rb', line 64

def set(target, value)
  target.instance_variable_set(instance_variable, value)
end

#writer_methodObject



19
20
21
# File 'lib/rake_factory/parameter.rb', line 19

def writer_method
  "#{name}="
end