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.



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

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

#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



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)


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

def configurable?
  @configurable
end

#define_on(klass) ⇒ Object



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

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

#dissatisfied_by?(instance) ⇒ Boolean

Returns:

  • (Boolean)


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

def dissatisfied_by?(instance)
  value = instance.send(reader_method)
  @required && value.nil?
end

#instance_variableObject



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

def instance_variable
  "@#{name}"
end

#reader_methodObject



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

def reader_method
  name
end

#satisfied_by?(instance) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#writer_methodObject



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

def writer_method
  "#{name}="
end