Class: NsOptions::Option
- Inherits:
-
Object
- Object
- NsOptions::Option
- Defined in:
- lib/ns-options/option.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#rules ⇒ Object
Returns the value of attribute rules.
-
#type_class ⇒ Object
Returns the value of attribute type_class.
-
#value ⇒ Object
if reading a lazy_proc, call the proc and return its coerced return val otherwise, just return the stored value.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(*args) ⇒ Option
constructor
A new instance of Option.
- #is_set? ⇒ Boolean
- #required? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Option
Returns a new instance of Option.
21 22 23 24 |
# File 'lib/ns-options/option.rb', line 21 def initialize(*args) self.rules, self.type_class, self.name = self.class.args(*args) self.value = self.rules[:default] end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/ns-options/option.rb', line 4 def name @name end |
#rules ⇒ Object
Returns the value of attribute rules.
4 5 6 |
# File 'lib/ns-options/option.rb', line 4 def rules @rules end |
#type_class ⇒ Object
Returns the value of attribute type_class.
4 5 6 |
# File 'lib/ns-options/option.rb', line 4 def type_class @type_class end |
#value ⇒ Object
if reading a lazy_proc, call the proc and return its coerced return val otherwise, just return the stored value
28 29 30 |
# File 'lib/ns-options/option.rb', line 28 def value @value end |
Class Method Details
.args(*args) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/ns-options/option.rb', line 12 def self.args(*args) [ self.rules(args.last.kind_of?(::Hash) ? args.pop : {}), # if a nil type_class is given, just use Object # this makes the option accept any value with no type coercion (args[1] || Object), args[0].to_s ] end |
.rules(rules) ⇒ Object
6 7 8 9 10 |
# File 'lib/ns-options/option.rb', line 6 def self.rules(rules) (rules || {}).tap do |r| r[:args] = (r[:args] ? [*r[:args]] : []) end end |
Instance Method Details
#==(other) ⇒ Object
52 53 54 55 56 |
# File 'lib/ns-options/option.rb', line 52 def ==(other) [ :name, :type_class, :rules, :value ].inject(true) do |bool, attribute| bool && (self.send(attribute) == other.send(attribute)) end end |
#is_set? ⇒ Boolean
44 45 46 |
# File 'lib/ns-options/option.rb', line 44 def is_set? self.value.respond_to?(:is_set?) ? self.value.is_set? : !self.value.nil? end |
#required? ⇒ Boolean
48 49 50 |
# File 'lib/ns-options/option.rb', line 48 def required? !!self.rules[:required] || !!self.rules[:require] end |