material 0.2.13 → 0.2.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1454938dad2b9de0d58ab66a8f6fcb91bd9b349019b895624f8b50c7ad954037
4
- data.tar.gz: a95337ac41988659ebaffea332c71bbbf974b2ed59e470736615161a9a0b3eb7
3
+ metadata.gz: 5becf0ac24dac90be88a65b0e301857e730a808b7c2df45f2dbc05c541fb67b4
4
+ data.tar.gz: 2ae72ede1ae9ee8f2c617ff3d3b5e6f28f7d8627eca2e5436d691bc8f4963f09
5
5
  SHA512:
6
- metadata.gz: 63685fd0367a5a8f654ff3f4300c2bbec302c8a838bdf5831d0571be87a340ea742659a9c000a7a6f6da44d2c7cb9e9ae737b17c65daa833b4f95c3f5cb4a983
7
- data.tar.gz: b528e28c21240bd6d13b332022fc31c7b5e413937ded0e6ba86ea856b065a1d438cb2636b109a00bfeaefb2c4d5871b88605818f75c4f5d1fa1a57113bf550d9
6
+ metadata.gz: fe93d86cf18b763c7b9eab3224e6ed4b3965e2450eb0ba928ee10063839d9c082946b95e5738f5adbb9bfa89564f04dd20eed839e30a0d856912da272f114242
7
+ data.tar.gz: a1f897b4683713ea9a90c6bbf80bd130a3a92c753eb814c841c4c7c5e56b4f791fa7df7d0e025f2002cc514f1b50242fb2cfd176570578f8adfc913e5f9b0dff
data/lib/material/base.rb CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  module Material
4
4
  class Base < Spicerack::AttributeObject
5
+ extend ActiveSupport::NumberHelper
6
+
5
7
  include Material::Components
6
8
  include Material::Core
7
9
  include Material::Display
@@ -9,6 +11,7 @@ module Material
9
11
  include Material::Site
10
12
  include Material::For
11
13
  include Material::Format
14
+ include Material::Attributes
12
15
 
13
16
  register_component :list_item_style
14
17
 
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Material
4
+ module Attributes
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ delegate :head_attributes, :tail_attributes, to: :class
9
+
10
+ memoize :attribute_values
11
+ memoize :attribute_types
12
+ memoize :formatted_attributes
13
+ memoize :sorted_attribute_names
14
+ memoize :relationship_attributes
15
+ end
16
+
17
+ class_methods do
18
+ def head_attributes
19
+ %w[id]
20
+ end
21
+
22
+ def tail_attributes
23
+ %w[created_at updated_at]
24
+ end
25
+ end
26
+
27
+ def human_attribute_value(attribute)
28
+ formatted_attributes.fetch(attribute)
29
+ end
30
+
31
+ def attribute_values
32
+ attribute_names.each_with_object({}) { |attribute, hash| hash[attribute] = public_send(attribute.to_sym) }
33
+ end
34
+
35
+ def attribute_types
36
+ attribute_names.each_with_object({}) { |attribute, hash| hash[attribute] = type_for_attribute(attribute) }
37
+ end
38
+
39
+ def formatted_attributes
40
+ attribute_types.each_with_object({}) do |(attribute, attribute_type), hash|
41
+ hash[attribute] = format_by_type(attribute_values[attribute], type: attribute_type)
42
+ end
43
+ end
44
+
45
+ def sorted_attribute_names
46
+ [
47
+ (head_attributes & attribute_names),
48
+ (attribute_names - head_attributes - tail_attributes).sort,
49
+ (tail_attributes & attribute_names),
50
+ ].flatten
51
+ end
52
+
53
+ def relationship_attributes
54
+ source_class.reflect_on_all_associations.map(&:foreign_key)
55
+ end
56
+ end
57
+ end
@@ -9,6 +9,8 @@ module Material
9
9
  attr_reader :source
10
10
 
11
11
  delegate :name, to: :class, prefix: true
12
+ delegate :class, to: :source, prefix: true
13
+ delegate :attribute_names, :human_attribute_name, :type_for_attribute, to: :source_class
12
14
 
13
15
  delegate_missing_to :source
14
16
  end
@@ -6,18 +6,34 @@ module Material
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  included do
9
- delegate :format_date, :format_time, :format_datetime, to: :class
9
+ delegate :format_date, :format_time, :format_number, :format_by_type, to: :class
10
10
  end
11
11
 
12
12
  class_methods do
13
13
  def format_date(date)
14
- date.to_s(:long)
14
+ date.to_date.to_s(:long)
15
15
  end
16
16
 
17
- def format_datetime(datetime)
18
- datetime.to_s(:long)
17
+ def format_time(time)
18
+ time.to_s(:long)
19
+ end
20
+
21
+ def format_number(number)
22
+ number_to_delimited(number)
23
+ end
24
+
25
+ def format_by_type(value, type:)
26
+ case type.to_sym
27
+ when :date
28
+ format_date(value)
29
+ when :datetime, :timestamp
30
+ format_time(value)
31
+ when :decimal, :float, :integer
32
+ format_number(value)
33
+ else
34
+ value
35
+ end
19
36
  end
20
- alias_method :format_time, :format_datetime
21
37
  end
22
38
  end
23
39
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Material
4
- VERSION = "0.2.13"
4
+ VERSION = "0.2.14"
5
5
  end
data/lib/material.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "active_support"
4
4
  require "active_support/core_ext"
5
+ require "active_support/number_helper"
5
6
 
6
7
  require "spicerack"
7
8
 
@@ -15,6 +16,7 @@ require_relative "material/concerns/pagination"
15
16
  require_relative "material/concerns/mount"
16
17
  require_relative "material/concerns/collection"
17
18
  require_relative "material/concerns/format"
19
+ require_relative "material/concerns/attributes"
18
20
 
19
21
  require "material/version"
20
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: material
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Garside
@@ -208,6 +208,7 @@ files:
208
208
  - README.md
209
209
  - lib/material.rb
210
210
  - lib/material/base.rb
211
+ - lib/material/concerns/attributes.rb
211
212
  - lib/material/concerns/collection.rb
212
213
  - lib/material/concerns/components.rb
213
214
  - lib/material/concerns/core.rb