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



12
13
14
15
16
17
18
19
20
# File 'lib/rake_factory/parameter.rb', line 12

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

Instance Attribute Details

#configurableObject (readonly)

Returns the value of attribute configurable.



3
4
5
# File 'lib/rake_factory/parameter.rb', line 3

def configurable
  @configurable
end

#defaultObject

Returns the value of attribute default.



3
4
5
# File 'lib/rake_factory/parameter.rb', line 3

def default
  @default
end

#lazyObject (readonly)

Returns the value of attribute lazy.



3
4
5
# File 'lib/rake_factory/parameter.rb', line 3

def lazy
  @lazy
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rake_factory/parameter.rb', line 3

def name
  @name
end

#requiredObject (readonly)

Returns the value of attribute required.



3
4
5
# File 'lib/rake_factory/parameter.rb', line 3

def required
  @required
end

#transformObject (readonly)

Returns the value of attribute transform.



3
4
5
# File 'lib/rake_factory/parameter.rb', line 3

def transform
  @transform
end

Instance Method Details

#apply_default_to(instance) ⇒ Object



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

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

#configurable?Boolean



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

def configurable?
  @configurable
end

#define_on(klass) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rake_factory/parameter.rb', line 34

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

    define_method parameter.reader_method do
      value_resolver = lambda do |t|
        stored_value = instance_variable_get(parameter.instance_variable)
        resolved_value = stored_value.respond_to?(:call) ?
            stored_value.call(*[t].slice(0, stored_value.arity)) :
            stored_value
        transformed_value = parameter.transform.call(resolved_value)
        transformed_value
      end
      parameter.lazy ? value_resolver : value_resolver.call(self)
    end
  end
end

#dissatisfied_by?(instance) ⇒ Boolean



67
68
69
# File 'lib/rake_factory/parameter.rb', line 67

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

#instance_variableObject



30
31
32
# File 'lib/rake_factory/parameter.rb', line 30

def instance_variable
  "@#{name}"
end

#read_from(instance) ⇒ Object



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

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

#reader_methodObject



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

def reader_method
  name
end

#satisfied_by?(instance) ⇒ Boolean



71
72
73
# File 'lib/rake_factory/parameter.rb', line 71

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

#writer_methodObject



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

def writer_method
  "#{name}="
end