Class: RakeFactory::Parameter
- Inherits:
-
Object
- Object
- RakeFactory::Parameter
- Defined in:
- lib/rake_factory/parameter.rb
Instance Attribute Summary collapse
-
#configurable ⇒ Object
readonly
Returns the value of attribute configurable.
-
#default ⇒ Object
Returns the value of attribute default.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#required ⇒ Object
readonly
Returns the value of attribute required.
-
#transform ⇒ Object
readonly
Returns the value of attribute transform.
Instance Method Summary collapse
- #apply_default_to(instance) ⇒ Object
- #configurable? ⇒ Boolean
- #define_on(klass) ⇒ Object
- #dissatisfied_by?(instance) ⇒ Boolean
-
#initialize(name, options) ⇒ Parameter
constructor
A new instance of Parameter.
- #instance_variable ⇒ Object
- #read_from(instance) ⇒ Object
- #reader_method ⇒ Object
- #satisfied_by?(instance) ⇒ Boolean
- #writer_method ⇒ Object
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, ) @name = name @default = [:default] || nil @required = [:required] || false @configurable = [:configurable].nil? ? true : !![:configurable] @transform = [:transform] || lambda { |x| x } end |
Instance Attribute Details
#configurable ⇒ Object (readonly)
Returns the value of attribute configurable.
3 4 5 |
# File 'lib/rake_factory/parameter.rb', line 3 def configurable @configurable end |
#default ⇒ Object
Returns the value of attribute default.
3 4 5 |
# File 'lib/rake_factory/parameter.rb', line 3 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/rake_factory/parameter.rb', line 3 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
3 4 5 |
# File 'lib/rake_factory/parameter.rb', line 3 def required @required end |
#transform ⇒ Object (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
50 51 52 |
# File 'lib/rake_factory/parameter.rb', line 50 def apply_default_to(instance) instance.send(writer_method, @default) unless @default.nil? end |
#configurable? ⇒ Boolean
58 59 60 |
# File 'lib/rake_factory/parameter.rb', line 58 def configurable? @configurable end |
#define_on(klass) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rake_factory/parameter.rb', line 32 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 stored_value = instance_variable_get(parameter.instance_variable) resolved_value = stored_value.respond_to?(:call) ? stored_value.call(*[self].slice(0, stored_value.arity)) : stored_value transformed_value = parameter.transform.call(resolved_value) transformed_value end end end |
#dissatisfied_by?(instance) ⇒ Boolean
62 63 64 |
# File 'lib/rake_factory/parameter.rb', line 62 def dissatisfied_by?(instance) @required && read_from(instance).nil? end |
#instance_variable ⇒ Object
28 29 30 |
# File 'lib/rake_factory/parameter.rb', line 28 def instance_variable "@#{name}" end |
#read_from(instance) ⇒ Object
54 55 56 |
# File 'lib/rake_factory/parameter.rb', line 54 def read_from(instance) instance.send(reader_method) end |
#reader_method ⇒ Object
24 25 26 |
# File 'lib/rake_factory/parameter.rb', line 24 def reader_method name end |
#satisfied_by?(instance) ⇒ Boolean
66 67 68 |
# File 'lib/rake_factory/parameter.rb', line 66 def satisfied_by?(instance) !dissatisfied_by?(instance) end |
#writer_method ⇒ Object
20 21 22 |
# File 'lib/rake_factory/parameter.rb', line 20 def writer_method "#{name}=" end |