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
- #get(target) ⇒ Object
-
#initialize(name, options) ⇒ Parameter
constructor
A new instance of Parameter.
- #instance_variable ⇒ Object
- #read_from(instance) ⇒ Object
- #reader_method ⇒ Object
- #satisfied_by?(instance) ⇒ Boolean
- #set(target, value) ⇒ Object
- #writer_method ⇒ Object
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, ) @name = name @default = [:default] @required = [:required] || false @transform = [:transform] || ->(x) { x } @configurable = [:configurable].nil? ? true : !![:configurable] end |
Instance Attribute Details
#configurable ⇒ Object (readonly)
Returns the value of attribute configurable.
8 9 10 |
# File 'lib/rake_factory/parameter.rb', line 8 def configurable @configurable end |
#default ⇒ Object
Returns the value of attribute default.
7 8 9 |
# File 'lib/rake_factory/parameter.rb', line 7 def default @default end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/rake_factory/parameter.rb', line 8 def name @name end |
#required ⇒ Object (readonly)
Returns the value of attribute required.
8 9 10 |
# File 'lib/rake_factory/parameter.rb', line 8 def required @required end |
#transform ⇒ Object (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
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
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_variable ⇒ Object
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_method ⇒ Object
23 24 25 |
# File 'lib/rake_factory/parameter.rb', line 23 def reader_method name end |
#satisfied_by?(instance) ⇒ 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_method ⇒ Object
19 20 21 |
# File 'lib/rake_factory/parameter.rb', line 19 def writer_method "#{name}=" end |