Class: Hashstructor::Member
- Inherits:
-
Object
- Object
- Hashstructor::Member
- Defined in:
- lib/hashstructor/member.rb
Overview
A member within a Hashstructor class that should be populated by the hash passed into #initialize.
Constant Summary collapse
- VALID_MEMBER_TYPES =
The allowed #member_types for a
Member::normal- a normal value. raises error if hash or array given unless the:no_collectionsoption is set.:hash- a hash, with all keys symbolized unless the:string_keysoption is set.:array- an array:set- a set
Note that these types refer to the "collection status" of the member. A
:normalmember will happily accept any value unless a:custom_typeoption is passed. [ :normal, :hash, :set, :array ].freeze
- BOOL_PROC =
The procedure used in VALID_VALUE_TYPES for both
TrueClassandFalseClass. Proc.new do |v| case v when true true when false false else raise "'#{v}' not stringable" unless v.respond_to?(:to_s) && !v.nil? v = v.downcase case v.downcase when "true", "t", "on", "yes" true when "false", "f", "off", "no" false else raise HashstructorError, "unknown value when parsing boolean: #{v}" end end end
- VALID_VALUE_TYPES =
A hash of Class => Proc converters for value types. Any value type in this list, as well as any class that includes or prepends
Hashstructor, is valid for #value_type. This is intentionally not frozen; if you want to extend it for your own use cases, I'm not going to get in your way. { String => Proc.new do |v| v.to_s end, Symbol => Proc.new do |v| v.to_sym end, Integer => Proc.new do |v| Integer(v.to_s) end, Float => Proc.new do |v| Float(v.to_s) end, TrueClass => BOOL_PROC, FalseClass => BOOL_PROC, }
Instance Attribute Summary collapse
-
#attr_kind ⇒ (nil, :reader, :accessor)
readonly
Specifies what type of attr (nil,
:reader, or:accessor) that this member should define on its class. -
#member_type ⇒ Symbol
readonly
The type of the member; see VALID_MEMBER_TYPES.
-
#name ⇒ Symbol
readonly
The name of the member, in the parsed hash.
-
#options ⇒ Hash
readonly
A (frozen) set of all extra options passed to the member.
-
#required ⇒ Object
readonly
If true, attempting to construct this object without setting this member will raise an error.
-
#value_type ⇒ (nil, Class)
readonly
Determines the class that Hashstructor should attempt to coerce a given value into.
Instance Method Summary collapse
-
#initialize(klass, name, options) ⇒ Member
constructor
A new instance of Member.
-
#parse_single(value) ⇒ Object
Parses the passed-in value (always a single item; InstanceMethods#hashstruct handles the breaking down of arrays and hashes) and returns a value according to #value_type.
Constructor Details
#initialize(klass, name, options) ⇒ Member
Returns a new instance of Member.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/hashstructor/member.rb', line 99 def initialize(klass, name, ) member_type = [:member_type] || :normal .delete(:member_type) value_type = [:value_type] .delete(:value_type) required = !!([:required]) .delete(:required) attr_kind = [:attr_kind] .delete(:attr_kind) = .freeze raise HashstructorError, "'name' must respond to :to_sym." unless name.respond_to?(:to_sym) @name = name.to_sym raise HashstructorError, "'member_type' (#{member_type}) must be one of: [ #{VALID_MEMBER_TYPES.join(", ")} ]" \ unless VALID_MEMBER_TYPES.include?(member_type.to_sym) raise HashstructorError, "'member_type' must be :normal to use 'default_value'." \ if member_type != :normal && [:default_value] @member_type = member_type.to_sym raise HashstructorError, "'value_type' must be nil or a Class." \ unless value_type == nil || value_type.is_a?(Class) raise HashstructorError, "'value_type' class, #{value_type.name}, must be in " + "VALID_VALUE_TYPES ([ #{VALID_VALUE_TYPES.keys.map { |c| c.name}.join(", ")} ]) " + "or include or prepend Hashstructor." \ unless value_type == nil || VALID_VALUE_TYPES[value_type] != nil || value_type.ancestors.include?(Hashstructor) @value_type = value_type raise HashstructorError, "'required' must be true if 'default_value'" \ if !required && [:default_value] @required = required raise HashstructorError, "'validation' must be a Proc." unless [:validation].nil? || [:validation].is_a?(Proc) case attr_kind when nil # do nothing when :reader klass.send(:attr_reader, @name) when :accessor klass.send(:attr_accessor, @name) else raise HashstructorError, "Unrecognized attr_kind: #{attr_kind}" end @attr_kind = attr_kind end |
Instance Attribute Details
#attr_kind ⇒ (nil, :reader, :accessor) (readonly)
Specifies what type of attr (nil, :reader, or :accessor) that this
member should define on its class.
94 95 96 |
# File 'lib/hashstructor/member.rb', line 94 def attr_kind @attr_kind end |
#member_type ⇒ Symbol (readonly)
Returns the type of the member; see VALID_MEMBER_TYPES.
23 24 25 |
# File 'lib/hashstructor/member.rb', line 23 def member_type @member_type end |
#name ⇒ Symbol (readonly)
Returns the name of the member, in the parsed hash.
20 21 22 |
# File 'lib/hashstructor/member.rb', line 20 def name @name end |
#options ⇒ Hash (readonly)
Returns a (frozen) set of all extra options passed to the member.
97 98 99 |
# File 'lib/hashstructor/member.rb', line 97 def end |
#required ⇒ Object (readonly)
If true, attempting to construct this object without setting this member will raise an error.
88 89 90 |
# File 'lib/hashstructor/member.rb', line 88 def required @required end |
#value_type ⇒ (nil, Class) (readonly)
Determines the class that Hashstructor should attempt to coerce a
given value into. For example, Fixnum will attempt to coerce a
scalar value into a Fixnum.
The valid types for #value_type are:
StringSymbolIntegerFloatTrueClassorFalseClass(for booleans)- any class that includes or prepends
Hashstructor
83 84 85 |
# File 'lib/hashstructor/member.rb', line 83 def value_type @value_type end |
Instance Method Details
#parse_single(value) ⇒ Object
Parses the passed-in value (always a single item; InstanceMethods#hashstruct handles the breaking down of arrays and hashes) and returns a value according to #value_type.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/hashstructor/member.rb', line 153 def parse_single(value) retval = if value_type.nil? value elsif value_type.ancestors.include?(Hashstructor) raise HashstructorError, "No hash provided for building a Hashstructor object." unless value.is_a?(Hash) value_type.new(value) else VALID_VALUE_TYPES[value_type].call(value) end if [:validation] errors = [] [:validation].call(retval, errors) if !errors.empty? raise HashstructorError, "Validation failure for '#{name}': #{errors.join("; ")}" end end retval end |